6、SpringBoot2之整合Mybatis
创建名为springboot_mybatis的新module,过程参考3.1节
6.1、引入相关依赖
注意:虽然本文使用的是 spring boot 2.7.18 和 MySQL 5.7 ,但是出于可移植性、可扩展性和兼容性方面的考虑,
druid 的启动器使用的是 spring boot 3 版本的,MySQL 的驱动使用的是 MySQL 8 版本的。
<!-- jdbc启动器的依赖(包括 jdbctemplate 和事务相关的依赖和配置)-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- druid启动器的依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-3-starter</artifactId>
<version>1.2.20</version>
</dependency>
<!-- MySQL驱动的依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<!-- Mybatis启动器的依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
6.2、创建实体类
package online.liaojy.pojo;
import java.io.Serializable;
/**
* @author liaojy
* @date 2023/12/22 - 6:18
*/
public class Employee implements Serializable {
private Integer empId;
private String empName;
private Integer age;
private String sex;
private String email;
public Employee() {
}
public Employee(Integer empId, String empName, Integer age, String sex, String email) {
this.empId = empId;
this.empName = empName;
this.age = age;
this.sex = sex;
this.email = email;
}
public Integer getEmpId() {
return empId;
}
public void setEmpId(Integer empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Employee{" +
"empId=" + empId +
", empName='" + empName + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
", email='" + email + '\'' +
'}';
}
}
6.3、创建mapper接口
package online.liaojy.mapper;
import online.liaojy.pojo.Employee;
import java.util.List;
/**
* @author liaojy
* @date 2023/12/22 - 6:27
*/
public interface EmployeeMapper {
List<Employee> getAllEmployee();
}
6.4、创建mapper映射文件
<?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="online.liaojy.mapper.EmployeeMapper">
<select id="getAllEmployee" resultType="employee">
select * from t_emp
</select>
</mapper>
6.5、配置druid和mybatis相关参数
# druid配置:
# 连接池类型
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# 数据库驱动名称
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据库url
spring.datasource.druid.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8
# 数据库用户名
spring.datasource.druid.username=root
# 数据库密码
spring.datasource.druid.password=root
# mybatis配置:
# 配置类型别名所对应的包
mybatis.type-aliases-package=online.liaojy.pojo
# 配置mapper映射文件所在的位置
mybatis.mapper-locations=classpath:/mappers/*.xml
# 配置数据库字段的下划线转实体类属性的驼峰
mybatis.configuration.map-underscore-to-camel-case=true
# 配置日志工具类
mybatis.configuration.log-impl=org.apache.ibatis.logging.slf4j.Slf4jImpl
6.6、使用注解扫描mapper接口
注意:@MapperScan 注解的作用是将指定位置的 mapper 接口生成对应的代理对象并加载进ioc容器中,
此外,在对应的 mapper 接口添加 @Mapper 注解也能实现同样的效果。
@MapperScan("online.liaojy.mapper")
6.7、使用mapper接口查询数据
注意:在编译阶段,idea 可能会提示(误报)找不到 EmployeeMapper 类型的 bean 。
@Autowired
private EmployeeMapper employeeMapper;
@RequestMapping("/getAllEmployee")
public List<Employee> getAllEmployee(){
List<Employee> employeeList = employeeMapper.getAllEmployee();
return employeeList;
}
6.8、测试效果
本文来自博客园,作者:Javaer1995,转载请注明原文链接:https://www.cnblogs.com/Javaer1995/p/17920789.html