Mybatis 通用 Mapper 3.5.2 发布

栏目: 软件资讯 · 发布时间: 6年前

内容简介:Mybatis 通用 Mapper 3.5.2 已发布。MyBatis 通用 Mapper 极其方便的使用 MyBatis 单表的增删改查,支持单表操作,不支持通用的多表联合查询。通用 Mapper 可以极大的方便开发人员。可以随意的按照自己的需要选择...

Mybatis 通用 Mapper 3.5.2 已发布。MyBatis 通用 Mapper 极其方便的使用 MyBatis 单表的增删改查,支持单表操作,不支持通用的多表联合查询。通用 Mapper 可以极大的方便开发人员。可以随意的按照自己的需要选择通用方法,还可以很方便的开发自己的通用方法。

更新日志

1. delete 和 deleteByPrimaryKey 增加对乐观锁注解 @Version 的支持。

测试用例如下:

/**
 * 乐观锁删除
 */
@Test
public void testDeleteByPrimaryKeyAndVersion() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        CountryVersionMapper mapper = sqlSession.getMapper(CountryVersionMapper.class);
        //根据主键删除,没有指定版本时删除不了
        Assert.assertEquals(0, mapper.deleteByPrimaryKey(100));

        CountryVersion countryVersion = new CountryVersion();
        countryVersion.setId(100);
        countryVersion.setVersion(2);
        //版本不对的时候的时候删除不了
        Assert.assertEquals(0, mapper.deleteByPrimaryKey(countryVersion));

        countryVersion.setId(100);
        countryVersion.setVersion(1);
        //版本正确的时候可以真正删除
        Assert.assertEquals(1, mapper.deleteByPrimaryKey(countryVersion));
    } finally {
        sqlSession.rollback();
        sqlSession.close();
    }
}

日志如下:

DEBUG [main] - ==>  Preparing: DELETE FROM country WHERE id = ? AND version = ? 
DEBUG [main] - ==> Parameters: 100(Integer), 100(Integer)
DEBUG [main] - <==    Updates: 0
DEBUG [main] - ==>  Preparing: DELETE FROM country WHERE id = ? AND version = ? 
DEBUG [main] - ==> Parameters: 100(Integer), 2(Integer)
DEBUG [main] - <==    Updates: 0
DEBUG [main] - ==>  Preparing: DELETE FROM country WHERE id = ? AND version = ? 
DEBUG [main] - ==> Parameters: 100(Integer), 1(Integer)
DEBUG [main] - <==    Updates: 1

特别注意: 上面测试用例已经展示了增加乐观锁后的参数如何传递,当主键多个值或者使用乐观锁的时候就需要通过实体(Map也可以)传递多个参数值。和之前的 update 一样,需要自己对执行结果进行判断来判断是否执行成功。

2. 3.5.0 版本中的参数 annotationAsSimpleType 名字错了,现在改为 enumAsSimpleType,用于配置是否将枚举类型当成基本类型对待。

3. SimpleTypeUtil 增加对 java 8 中的日期类型的支持。

4. Example.Builder 增加类似 Weekend 中 Java8 方法引用的用法,该功能由 chinaerserver 提交(#pr207)

示例如下:

@Test
public void testWeekend() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        //普通方式
        List<Country> selectByExample = mapper.selectByExample(
                new Example.Builder(Country.class).where(Sqls.custom().andLike("countryname", "China")).build());
        //Java8 方式
        List<Country> selectByWeekendSql = mapper.selectByExample(new Example.Builder(Country.class)
                .where(WeekendSqls.<Country>custom().andLike(Country::getCountryname, "China")).build());
        // 判断两个结果数组内容是否相同
        Assert.assertArrayEquals(selectByExample.toArray(), selectByWeekendSql.toArray());
    } finally {
        sqlSession.close();
    }
}

5. 当项目中使用了自定义classloader的时候,可以通过设置classloader上下文的方式来使得自己的mapper class能够被找到(这里的修改参照了 mybatis 源码中的 ClassLoaderWrapper 类),by liyongjun1 #pr185

6. 重点提醒,3.5.0 中 useSimpleType 默认值改为 true,默认忽略复杂类型的字段,复杂类型不需要加 @Transient 注解,具体类型可以参考 SimpleTypeUtil 类。

在 SimpleTypeUtil 类中,由于一般的 JavaBean (尤其是 MyBatis)规范中,不能使用基本类型,这主要是由于基本类型有默认值,在一些动态 SQL 情况下(如所有 Selective 方法),无法判断基本类型的值是不是 null。 所以在这里的简单类型是不包含 byte,short,int,long,float,double,char,boolean 这八种基本类型的。

如果你要升级通用 Mapper 但是不想修改原来的基本类型,就设置 useSimpleType=false。

使用 Maven

<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper</artifactId>
    <version>3.5.2</version>
</dependency>

如果你使用 Spring Boot 可以直接引入:

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

【声明】文章转载自:开源中国社区 [http://www.oschina.net]


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web Scalability for Startup Engineers

Web Scalability for Startup Engineers

Artur Ejsmont / McGraw / 2015-6-23 / USD 34.81

Design and build scalable web applications quickly This is an invaluable roadmap for meeting the rapid demand to deliver scalable applications in a startup environment. With a focus on core concept......一起来看看 《Web Scalability for Startup Engineers》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

SHA 加密
SHA 加密

SHA 加密工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换