4.0.0 和 4.0.1 存在两个严重的问题,一个是 3.x 版本时针对 devtools 的配置丢了,一个是 extra 中的 InsertListMapper 没有 @RegisterMapper 注解,由于这两个问题的存在,这里建议升级到 4.0.2 版本,这个版本不只是解决了两个严重的 BUG,还增加和完善了好多功能。 4.0.2 更新日志如下: @KeySql 注解增加 genId 方式,点击查看详细使用文档。 tk.mybatis.mapper.additional.insert.InsertListMapper 增加对 @KeySql 注解 genId 方法的支持。 tk.mybatis.mapper.additional.insert.InsertListMapper 增加 @RegisterMapper 注解,此问题应该是之前导致常见问题的主要原因。 增加 MetaObjectUtil 兼容 MyBatis 不同版本,该工具类来自分页插件 PageHelper。 完善乐观锁,增加 NextVersion 实例缓存。 乐观锁增加对 java.sql.Timestamp 的支持。 增加拆分项目后缺失的 spring-devtools.properties。 增加通用聚合查询方法 by liuchan 重构聚合查询,改为执行的时候进行拼接查询语句。 wrapKeyword 增加对表名关键字的处理。 增加 IdListMapper,完善 IdList 方法,增加测试。 @Id 列设置 set id = id 时,增加判断 updateable 属性,默认 true,设置 false 后,不添加 set id = id。 @KeySql 的 genId 是很关键的一个更新,有关该功能的详细用法可以点击查看一篇专门提供的文章。 新增的 IdList 方法如下: @tk.mybatis.mapper.annotation.RegisterMapper public interface SelectByIdListMapper<T, PK> { /** * 根据主键字符串进行查询,类中只有存在一个带有@Id注解的字段 * * @param idList * @return */ @SelectProvider(type = IdListProvider.class, method = "dynamicSQL") List<T> selectByIdList(@Param("idList") List<PK> idList); } @tk.mybatis.mapper.annotation.RegisterMapper public interface DeleteByIdListMapper<T, PK> { /** * 根据主键字符串进行删除,类中只有存在一个带有@Id注解的字段 * * @param idList * @return */ @DeleteProvider(type = IdListProvider.class, method = "dynamicSQL") int deleteByIdList(@Param("idList") List<PK> idList); } 用法: import tk.mybatis.mapper.additional.idlist.IdListMapper; public interface CountryMapper extends IdListMapper<Country, Long> { } 测试: @test public void testByIdList() { SqlSession sqlSession = getSqlSession(); try { CountryMapper mapper = sqlSession.getMapper(CountryMapper.class); List<Long> idList = Arrays.asList(1L, 2L, 3L); List<Country> countryList = mapper.selectByIdList(idList); Assert.assertEquals(3, countryList.size()); Assert.assertEquals(1L, (long)countryList.get(0).getId()); Assert.assertEquals(2L, (long)countryList.get(1).getId()); Assert.assertEquals(3L, (long)countryList.get(2).getId()); //删除 Assert.assertEquals(3, mapper.deleteByIdList(idList)); //查询结果0 Assert.assertEquals(0, mapper.selectByIdList(idList).size()); } finally { sqlSession.close(); } } 通用 Mapper Boot Starter 也同步更新到了 2.0.2 版本,推荐升级。 通用 Mapper 4.0.2 发布,修复严重 Bug下载地址