SpringBoot集成MyBatisCommonMapper

1. pom配置

  1. 引入spring boot, mybatis-spring, tk.mybatis, h2测试数据库
<!--Spring boot parent-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.11.RELEASE</version>
    <relativePath/>
</parent>

<dependencies>
    <!--boot 测试包-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>

    <!--mybatis spring boot starter POMS-->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.3.2</version>
    </dependency>

    <!--tk mybatis common mapper starter POMS-->
    <dependency>
        <groupId>tk.mybatis</groupId>
        <artifactId>mapper-spring-boot-starter</artifactId>
        <version>1.2.3</version>
    </dependency>

    <!--h2 测试数据库-->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>

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<Country> {

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

4. mapper xml

<mapper namespace="org.ko.mybatis.mapper.CountryMapper">
    <select id="findAll" resultType="Country">
        select * from country
    </select>
</mapper>
  • 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<Country> 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<Country> 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<Country> 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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,366评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,521评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,689评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,925评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,942评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,727评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,447评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,349评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,820评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,990评论 3 337
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,127评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,812评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,471评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,017评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,142评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,388评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,066评论 2 355

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,668评论 18 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,823评论 6 342
  • springboot 概述 SpringBoot能够快速开发,简化部署,适用于微服务 参考嘟嘟大神SpringBo...
    一纸砚白阅读 5,424评论 2 20
  • 利用中午的时间画了两个小小石头画,大家有没有看出来她是谁呢? 哈哈,她就是新生代萌神小小表情帝甜馨哦,画得还挺像的...
    小崔喵喵哒石头画阅读 1,025评论 19 23
  • Mongoose 笔记 官网指南 参考链接 名词解释 Schema: 一种以文件形式存储的数据库模型骨架,不具备...
    Cause_XL阅读 347评论 0 0