一、SpringBoot整合Mybatis【重点
】
1.1 xml方式整合Mybatis
xml方式在编写复杂SQL时,更适合
1.1.1 导入依赖。
<!-- mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- druid连接-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!-- mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
1.1.2 编写配置文件
// 准备实体类
@Data
public class Air implements Serializable {
private Integer id;
private Integer districtId;
private java.util.Date monitorTime;
private Integer pm10;
private Integer pm25;
private String monitoringStation;
private java.util.Date lastModifyTime;
}
// ================================================
@Data
public class District implements Serializable {
private Integer id;
private String name;
}
1.1.3 准备Mybatis
// 1. 接口
public interface AirMapper {
List<Air> findAll();
}
// 2. 在启动类中添加直接,扫描Mapper接口所在的包
@MapperScan(basePackages = "com.qf.firstspringboot.mapper")
// 3. 准备映射文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qf.firstspringboot.mapper.AirMapper">
<!-- List<Air> findAll();-->
<select id="findAll" resultType="Air">
select * from air
</select>
</mapper>
编写配置文件
# 指明mapper映射文件的位置
mybatis:
mapper-locations: classpath:mapper/*.xml
# 配置连接池Druid
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/qf-v7?serverTimezone=Asia/Shanghai
username: root
password: qf123456
type: com.alibaba.druid.pool.DruidDataSource
1.1.4 测试。
class AirMapperTest extends FirstSpringbootApplicationTests {
@Autowired
private AirMapper airMapper;
@Test
void findAll() {
List<Air> list = airMapper.findAll();
for (Air air : list) {
System.out.println(air);
}
}
}
1.2 注解方式整合Mybatis
注解方式在编写配置简单,简单SQL推荐使用
1.2.1 创建District的Mapper接口
public interface DistrictMapper {
List<District> findAll();
}
1.2.2 添加Mybatis注解
针对增删改查:@Insert,@Delete,@Update,@Select
还是需要在启动类中添加@MapperScan注解
@Select("select * from district")
List<District> findAll();
@Select("select * from district where id = #{id}")
District findOneById(@Param("id") Integer id);
1.2.3 添加配置
// yml文件
logging:
level:
com.qf.firstspringboot.mapper: DEBUG
1.2.4 测试,查看日志
class DistrictMapperTest extends FirstSpringbootApplicationTests {
@Autowired
private DistrictMapper mapper;
@Test
void findAll() {
List<District> list = mapper.findAll();
for (District district : list) {
System.out.println(district);
}
}
@Test
void findOneById() {
District district = mapper.findOneById(5);
System.out.println(district);
}
}
1.3 SpringBoot整合分页助手
1.3.1 导入依赖
<!-- pageHelper依赖-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
1.3.2 测试使用
@Test
public void findByPage(){
//1. 执行分页
PageHelper.startPage(1,5);
//2. 执行查询
List<Air> list = airMapper.findAll();
//3. 封装PageInfo对象
PageInfo<Air> pageInfo = new PageInfo<>(list);
//4. 输出
for (Air air : pageInfo.getList()) {
System.out.println(air);
}
}