2-1 使用IDEA创建MAVEN项目
1.创建项目:
若是Springboot项目,直接采用quickstart方式,创建的是一个以jar包方式对外提供统一输出的项目. 若不是springboot项目则采用第二种方式,创建的是一种可以以war包方式部署在tomcat或jboss这种web的j2ee容器里面.
2-2 引入SpringBoot依赖包实现简单的WEB项目
1.添加依赖:在springboot官网找
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2-3 Mybatis接入SpringBoot项目
1.使用springboot搭建web项目
@SpringBootApplication(scanBasePackages = {"com.seckillproject"})
@RestController
@MapperScan("com.seckillproject.dao")
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
@EnableAutoConfiguration:将app这个启动类变成自动化可以支持springboot配置的bean,并且开启整个项目基于springboot的自动化配置,包括启动内嵌的tomcat并加载默认名配置 @SpringBootApplication:与@EnableAutoConfiguration效果相同,另外可以指定需要扫描的根目录包,能够通过注解的方式自动发现springboot特定的注解(@component,@service,@controller,@Repository). @RestController:可以使用spring MVC的功能 @MapperScan:dao存放的地方 SpringApplication.run(App.class, args);:用于启动该springboot项目
2.Mybatis接入SpringBoot项目
引入mybatis相关依赖
<!--数据库:此connector是mysql的java client端-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
<!--数据库连接池:用于管理mysql连接-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.3</version>
</dependency>
<!--Mybatis依赖:mybatis对springboot的支持文件-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
在application.properties中引入mybatis配置,用来启动带mybatis数据库访问的springboot项目
server.port=8080
mybatis.mapper-locations=classpath:mapping/*.xml
spring.datasource.name=seckill
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/seckill?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
2-4 Mybatis自动生成器使用方式
1.配置mybatis自动生成工具:mybatis generator
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>mybatis generator</id>
<phase>package</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<verbose>true</verbose>
<overwrite>false</overwrite>
<configurationFile>
src/main/resources/mybatis-generator.xml
</configurationFile>
</configuration>
</plugin>
2.编写mybatis generator文件:可以在官网下载
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="DB2Tables" targetRuntime="MyBatis3">
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/seckill"
userId="root"
password="root">
</jdbcConnection>
<javaModelGenerator targetPackage="com.seckillproject.dataobject" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.seckillproject.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="user_info" domainObjectName="UserDO"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false" ></table>
</context>
</generatorConfiguration>
3.创建数据库
-utf-8不包含表情符号(数据库设计)
密码是加密的字符串,一般不和用户信息主表创建在一起.在企业级项目中,密码托管一般是另一个项目实现.至少在数据库级别,密码和用户信息需要分开存取
|