依赖声明:
# pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.14.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.40</version> </dependency> </dependencies>
|
| mybatis-spring-boot-starter:MyBatis 启动器。 |
数据库表:
1 2 3 4 5 6
| CREATE TABLE `employee` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID', `name` varchar(16) NOT NULL COMMENT '姓名', `age` int(11) NOT NULL COMMENT '性别', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
|
| MySQL 数据库脚本 |
实体类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package org.fanlychie.entity; public class Employee { private Long id; private String name; private Integer age; @Override public String toString() { return "Employee{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } }
|
Mapper 配置文件:
# src/main/resources/mapper/EmployeeMapper.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <?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="org.fanlychie.mapper.EmployeeMapper"> <insert id="save" useGeneratedKeys="true" keyProperty="id" parameterType="Employee"> INSERT INTO EMPLOYEE ( ID, NAME, AGE ) VALUES ( #{id}, #{name}, #{age} ) </insert> <select id="selectAll" resultMap="EmployeeResultMap"> SELECT * FROM EMPLOYEE </select> <resultMap id="EmployeeResultMap" type="Employee"> <id property="id" column="ID" /> <result property="name" column="NAME" /> <result property="age" column="AGE" /> </resultMap> </mapper>
|
Mapper 接口:
1 2 3 4 5 6 7 8 9
| package org.fanlychie.mapper; public interface EmployeeMapper { Long save(Employee employee); List<Employee> selectAll(); }
|
| 只需声明接口,不需要编写额外的实现类。 |
Spring Boot 配置文件:
# src/main/resources/application.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| spring: datasource: url: jdbc:mysql://127.0.0.1/test username: root password: root driver-class-name: com.mysql.jdbc.Driver tomcat: default-auto-commit: true mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: org.fanlychie.entity logging: level: root: WARN org.fanlychie.mapper: DEBUG
|
| mapper-locations:用来指定 MyBatis 的 Mapper 配置文件的位置。 |
| type-aliases-package:指定类型别名的包。多个用逗号(,)或分号(;)分隔。 指定的包下所有的类型在 Mapper 配置文件中可以使用类的简单类名(如:org.fanlychie.entity.Employee 可以简写为 Employee)。 |
启动类:
1 2 3 4 5 6 7 8 9
| @SpringBootApplication @MapperScan("org.fanlychie.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
|
| MyBatis 启动器默认会搜索所有 @Mapper 的注解。这意味着你所有的 Mapper 接口都需要标注 @Mapper 注解。显然这样不够方便。@MapperScan 注解可以用来指定要扫描的 Mapper 接口的包路径。 |
单元测试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| @RunWith(SpringRunner.class) @SpringBootTest public class ApplicationTest { @Autowired private EmployeeMapper employeeMapper; @Test public void testSave() { Employee employee = new Employee(); employee.setName("fanlychie"); employee.setAge(23); employeeMapper.save(employee); } @Test public void testSelectAll() { List<Employee> employees = employeeMapper.selectAll(); employees.forEach(System.out::println); } }
|
示例项目开发环境:Java-8、Maven-3、IntelliJ IDEA-2018、Spring Boot-1.5.14.RELEASE
完整示例项目链接:spring-boot-mybatis-sample
参考文档文献链接:mybatis-spring-boot-autoconfigure