Spring Boot官方提供了Spring Data的支持,但是却没有提供MyBatis的支持。 因为,Spring Data是Spring旗下的子产品,而MyBatis却不是。 本文通过一个小例子来实现Spring Boot 与MyBatis的整合。 加入maven依赖 - <!-- mybatis整合spring boot的starter -->
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>1.3.0</version>
- </dependency>
- <dependency>
- <groupId>com.h2database</groupId>
- <artifactId>h2</artifactId>
- <scope>runtime</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
复制代码Spring Boot官方提供的starter是这样的:spring-boot-starter-* 而其他第三方提供的starter是这样的:*-spring-boot-starter 编写实体类 - public class Emp {
- private Long id;
- private String name;
- private Date birthday;
- private Double salary;
- public Emp() {
- }
- public Emp(String name, Date birthday, Double salary) {
- this.name = name;
- this.birthday = birthday;
- this.salary = salary;
- }
- //getter and setter
- }
复制代码编写持久层 - public interface EmpMapper {
- List<Emp> findAll();
- Emp findById(Long id);
- void save(Emp emp);
- void update(Emp emp);
- void delete(Long id);
- }
复制代码Spring Boot的启动程序 - @MapperScan("org.yun.mapper")
- @SpringBootApplication
- public class Application {
- public static void main(String[] args) {
- SpringApplication.run(Application.class, args);
- }
- }
复制代码@MapperScan指定要扫描的XxxMapper.java文件 在src/main/resources/下创建包 org.yun.mapper(与EmpMapper.java的包同名)
然后,建立EmpMapper.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="org.yun.mapper.EmpMapper">
- <sql id="baseSql">
- id,name,birthday,salary
- </sql>
- <select id="findAll" resultType="org.yun.domain.Emp">
- select
- <include refid="baseSql"/>
- from emp
- </select>
- <select id="findById" parameterType="long" resultType="org.yun.domain.Emp">
- select
- <include refid="baseSql"/>
- from emp
- where id = #{id}
- </select>
- <insert id="save" parameterType="org.yun.domain.Emp" keyProperty="id" useGeneratedKeys="true">
- insert into emp(name,birthday,salary)values(#{name},#{birthday},#{salary})
- </insert>
- <update id="update" parameterType="org.yun.domain.Emp">
- update emp set name = #{name},birthday = #{birthday},salary = #{salary}
- where id = #{id}
- </update>
- <delete id="delete" parameterType="long">
- delete from emp where id = #{id}
- </delete>
- </mapper>
复制代码编写application.properties - #指定实体类的名别包
- mybatis.type-aliases-package=org.yun.domain
- #指定XxxMapper.xml的位置
- #mybatis.mapper-locations=classpath:org/yun/mapper/*Mapper.xml
复制代码创建数据表schema.sql - CREATE TABLE emp (
- id BIGINT PRIMARY KEY AUTO_INCREMENT,
- name VARCHAR(30),
- birthday DATE,
- salary DOUBLE
- );
复制代码初始化数据data.sql - insert into emp(name,birthday,salary)values('admin','1999-09-09',9000);
复制代码将schema.sql、data.sql(名字固定)文件放置在src/main/resources下目录,Spring Boot启动的时候会自动执行。 最后编写单元测试 - import static org.junit.Assert.assertNotNull;
- import static org.junit.Assert.assertTrue;
- @RunWith(SpringRunner.class)
- @SpringBootTest
- public class ApplicationTests {
- @Autowired
- private EmpMapper empMapper;
- @Test
- public void contextLoads() {
- assertNotNull(empMapper);
- Emp emp1 = new Emp("tom", new Date(), 5000D);
- Emp emp2 = new Emp("bill", new Date(), 6000D);
- empMapper.save(emp1);
- empMapper.save(emp2);
- assertTrue(empMapper.findAll().size() == 3);
- Emp emp = empMapper.findById(2L);
- emp.setSalary(8000D);
- empMapper.update(emp);
- assertTrue(emp.getSalary() == 8000D);
- empMapper.delete(2L);
- assertTrue(empMapper.findAll().size() == 2);
- }
- }
复制代码
|