1. 新建maven项目 添加SpringBoot依赖
2. 在App.java后面添加@EnableAutoConfiguration标签 将其变成一个Spring bean 并且能够自动配置
点运行 在浏览器访问url即可 看到一个默认的错误页面 说明在不需要其他配置的情况下 tomcat容器已经启动
3. 添加
@RestController注解和
@RequestMapping("/")
public String home()方法
实现springMVC控制层的功能
4.SpringBoot在默认的classpath下寻找application.properties 加载里面的配置
5. pom文件中添加mybatis需要的maven包 jdbc驱动 连接池
6. 使用mybatis的自动生成器
使用mybatis自动生成器前在pom文件中的配置
<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>8.0.19</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>mybatis generator</id>
<phase>package</phase>
<goals>
<goal>
generate
</goal>
</goals>
</execution>
</executions>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
<configurationFile>
src/main/resources/mybatis-generator.xml
</configurationFile>
</configuration>
</plugin>
编写mybatis-generator.xml
<?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.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/miaosha?serverTimezone=UTC"
userId="root"
password="1234">
</jdbcConnection>
<!--生成dataobject类存放位置-->
<javaModelGenerator targetPackage="com.miaoshaproject.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.miaoshaproject.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
Example文件自动实现了复杂的curd操作 但开发中的需求一般不用其实现 配置避免生成
<table tableName="user_info" domainObjectName="UserDO" enableCountByExample="false"
enableDeleteByExample="false" enableUpdateByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"/>
<table tableName="user_password" domainObjectName="UserPasswordDO" enableCountByExample="false"
enableDeleteByExample="false" enableUpdateByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"/>
</context>
</generatorConfiguration>
7. 创建database和table 注意用户信息和用户密码分开保存
8.编写application.properties
server.port=8090
mybatis.mapper-locations=classpath:mapping/*.xml
spring.datasource.name=miaosha
spring.datasource.url=jdbc:mysql://localhost:3306/miaosha?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
//@EnableAutoConfiguration
//自动扫描 使得加载spring的一些特定的注解
@SpringBootApplication(scanBasePackages = {"com.miaoshaproject"})
@RestController
//设置dao存放的地方
@MapperScan("com.miaoshaproject.dao")
public class App
{
@Autowired
private UserDOMapper userDOMapper;
@RequestMapping("/")
public String home(){
UserDO userDO = userDOMapper.selectByPrimaryKey(1);
if (userDO == null){
return "用户不存在";
}else{
return userDO.getName();
}
// return "Hello world!";
}
public static void main( String[] args ) {
System.out.println( "Hello World!" );
SpringApplication.run(App.class,args);
}
}