SpringBoot集成MyBatisCommonMapper

1. pom配置

  1. 引入spring boot, mybatis-spring, tk.mybatis, h2测试数据库


    org.springframework.boot
    spring-boot-starter-parent
    1.5.11.RELEASE
    



    
    
        org.springframework.boot
        spring-boot-starter-test
    

    
    
        org.mybatis
        mybatis-spring
        1.3.2
    

    
    
        tk.mybatis
        mapper-spring-boot-starter
        1.2.3
    

    
    
        com.h2database
        h2
        runtime
    

2. yml配置

spring:
  datasource:
    schema: import.sql #h2数据库初始化sql
mybatis:
  mapper-locations: classpath:mappers/*.xml #mapper xml path
  type-aliases-package: org.ko.mybatis.domain #开启mybatis alias
  configuration:
    map-underscore-to-camel-case: true #开启自动下划线转驼峰

mapper:
  not-empty: true #
  before: true #
  mappers: tk.mybatis.mapper.common.Mapper #配置的mapper

3. 定义领域对象和mapper接口

import org.apache.ibatis.type.JdbcType;
import tk.mybatis.mapper.annotation.ColumnType;

import javax.persistence.Id;
import java.io.Serializable;

public class Country implements Serializable {

    private static final long serialVersionUID = 6569081236403751407L;

    @Id
    @ColumnType(jdbcType = JdbcType.BIGINT)
    private Long id;

    private String countryCode;

    private String countryName;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCountryCode() {
        return countryCode;
    }

    public void setCountryCode(String countryCode) {
        this.countryCode = countryCode;
    }

    public String getCountryName() {
        return countryName;
    }

    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }
}
import org.ko.mybatis.domain.Country;

import java.util.List;

public interface CountryMapper extends tk.mybatis.mapper.common.Mapper {

    List findAll();
}
  • domain域对象主键要指定@Id注解
  • mapper接口集成tk.Mapper接口,泛型是域对象

4. mapper xml


    

  • xml中只需要实现自定义接口

5. 启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication
@MapperScan("org.ko.mybatis.mapper")
public class MyBatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyBatisApplication.class, args);
    }

}
  • 启动类中@MapperScan要使用tk.mybatis.spring.annotation.MapperScan重写注解,不能使用mybatis中的注解。

6. 测试类

import org.junit.Test;
import org.junit.runner.RunWith;
import org.ko.mybatis.domain.Country;
import org.ko.mybatis.mapper.CountryMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@SpringBootTest
@RunWith(SpringRunner.class)
public class MapperTest {

    private static final Logger _LOGGER = LoggerFactory.getLogger(MapperTest.class);

    @Autowired private CountryMapper countryMapper;

    @Test
    public void whenQueryCountrySuccess () {
        List countryList = countryMapper.selectAll();
        _LOGGER.info("country count: {}", countryList.size());
        assert countryList.size() > 0;
    }

    @Test
    public void whenInsertCountrySuccess () {
        //1. 插入
        Country country = new Country();
        country.setId(133337L);
        country.setCountryCode("DL");
        country.setCountryName("大连");
        int ret = countryMapper.insert(country);
        _LOGGER.info("insert country result: {}", ret);
        assert ret > 0;

        //2. 查询
        Country c = countryMapper.selectByPrimaryKey(133337L);
        _LOGGER.info("insert country name: {}", c.getCountryName());
    }

    @Test
    public void whenUpdateCountrySuccess () {
        //1. 更新
        Country country = new Country();
        country.setId(133337L);
        country.setCountryCode("DL");
        country.setCountryName("美丽的海滨城市大连");
        int ret = countryMapper.updateByPrimaryKeySelective(country);
        assert ret > 0;
        _LOGGER.info("update country result: {}", ret);

        //2. 查询
        Country c = countryMapper.selectByPrimaryKey(133337L);
        _LOGGER.info("update country name: {}", c.getCountryName());
    }

    @Test
    public void whenDeleteCountrySuccess () {
        //1. 删除
        int ret = countryMapper.deleteByPrimaryKey(1L);
        assert ret > 0;

        //2. 查询 183
        List countryList = countryMapper.selectAll();
        _LOGGER.info("country count: {}", countryList.size());
        assert countryList.size() == 182;
    }

    @Test
    public void whenSelectByExampleSuccess () {
        Example e = new Example(Country.class);
        e.createCriteria()
                .andLessThanOrEqualTo("id", 100L)
                .andGreaterThan("id", 50L);
        List countries = countryMapper.selectByExample(e);
        _LOGGER.info("select by example result count: {}", countries.size());
        assert countries.size() == 50;
    }
}

7.demo

github: https://github.com/uleetu/practice-drafts/tree/master/mybatis-common-mapper

你可能感兴趣的:(SpringBoot集成MyBatisCommonMapper)