官方文档 http://mybatis.org/spring-boot-starter/
mybatis-spring-boot-starter起步依赖提供了集成mybatis的简便方式
添加maven依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
配置
配置数据库信息
spring:
datasource:
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test
mybatis中的相关配置
mybatis:
mapper-locations: classpath:com/lyp/mapper/*.xml # mapper映射文件路径
type-aliases-package: com.lyp.entity # 实体类支持使用别名
如果使用基于注解的mybatis配置方式,那么上面的配置不需要。
使用
使用方式与直接使用mybatis一样
数据库如下
drop table if exists `employees`;
CREATE TABLE `employees`
(
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`)
);
INSERT INTO employees VALUES(10001,'1953-09-02','Georgi','Facello','M','1986-06-26');
INSERT INTO employees VALUES(10002,'1964-06-02','Bezalel','Simmel','F','1985-11-21');
INSERT INTO employees VALUES(10003,'1959-12-03','Parto','Bamford','M','1986-08-28');
INSERT INTO employees VALUES(10004,'1954-05-01','Chirstian','Koblick','M','1986-12-01');
INSERT INTO employees VALUES(10005,'1955-01-21','Kyoichi','Maliniak','M','1989-09-12');
INSERT INTO employees VALUES(10006,'1953-04-20','Anneke','Preusig','F','1989-06-02');
INSERT INTO employees VALUES(10007,'1957-05-23','Tzvetan','Zielinski','F','1989-02-10');
INSERT INTO employees VALUES(10008,'1958-02-19','Saniya','Kalloufi','M','1994-09-15');
INSERT INTO employees VALUES(10009,'1952-04-19','Sumant','Peac','F','1985-02-18');
INSERT INTO employees VALUES(10010,'1963-06-01','Duangkaew','Piveteau','F','1989-08-24');
INSERT INTO employees VALUES(10011,'1953-11-07','Mary','Sluis','F','1990-01-22');
1.定义实体类
com.lyp.entity.Employee
@Data
public class Employee
{
private int number; // 员工编号
private Date birthday; // 出生日期
private String firstName;
private String lastName;
private String gender; // 性别
private Date hireDay; // 雇佣日期
}
1.定义Mapper接口,使用注解@Mapper
标记
com.lyp.mapper.EmployeeMapper
@Mapper
public interface EmployeeMapper
{
List<Employee> findAll();
}
3.配置mapper xml映射文件
注意:xml映射文件的路径要与对应的mapper接口包名对应,同时必须与配置中的
mapper-locations
相同
com/lyp/mapper/EmployeeMapper.xml
<?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.lyp.mapper.EmployeeMapper">
<select id="findAll" resultType="Employee">
select emp_no number, birth_date birthday, first_name firstName, last_name lastName, gender, hire_date hireDay
from employees;
</select>
</mapper>
对于类字段名与表列名不一致的情况,除了直接使用别名外,mybatis还可以使用 resultMap
指定转换规则
<mapper namespace="com.lyp.mapper.EmployeeMapper">
<resultMap id="employeeResultMap" type="employee">
<result property="number" column="emp_no"/>
<result property="birthday" column="birth_date"/>
<result property="firstName" column="first_name"/>
<result property="lastName" column="last_name"/>
<result property="gender" column="gender"/>
<result property="hireDay" column="hire_date"/>
</resultMap>
<select id="findAll" resultMap="employeeResultMap">
select * from employees;
</select>
</mapper>
4.执行crud
com.lyp.controller.MyController
@RestController
public class MyController
{
private final EmployeeMapper employeeMapper;
@Autowired
public MyController(EmployeeMapper employeeMapper)
{
this.employeeMapper = employeeMapper;
}
@GetMapping("/employees")
public Object showAllEmployees()
{
List<Employee> employees = employeeMapper.findAll();
return employees;
}
}