Mario洁 发表于 2019-3-8 15:55:11

Spring Boot 整合MyBatis

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.sqlCREATE TABLE emp (
id       BIGINT PRIMARY KEY AUTO_INCREMENT,
name   VARCHAR(30),
birthday DATE,
salary   DOUBLE
);初始化数据data.sqlinsert 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);
    }
}

页: [1]
查看完整版本: Spring Boot 整合MyBatis