通过spring initializr创建maven工程springboot-mysql-redis,并且勾选相关依赖(web、数据库驱动、mybatis)
springboot-mysql-redis工程,做了上面的勾选,对应的pom.xml依赖如下↓
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
准备Bean对象类对应数据库信息:
(2)编写mapper接口以及映射文件
MyBatis的Dao:Dao接口、Dao接口对应的映射文件(xml)
编写UserMapper接口并添加@Mapper注解(标记该类是一个Mapper接口,可以被SpringBoot自动扫描)
#上面这个可以不做:在启动类上添加@MapperScan(basePackages = {"com.qf.dao"})Dao接口的包扫描批量操作也行
在配置文件中增加Mapper.xml文件扫描的地址:特别要注意的是配置里面的不是config-location(核心配置文件),而是mapper-locations
(3)编写service接口以及实现类
(4) 编写controller
(5)配置application.properties
#连接数据库
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spr