快速了解这是什么:
-
全新的 ByExample 方法
List<Employee> employees = mapper.selectByExample() .where(id, isLessThan(10), and(employed, isEqualTo("foo"))) .or(occupation, isLike("b%")) .orderBy(id.descending()) .build() .execute(); // where (id < ? and employed = ?) or occupation like ? order by id DESC -
可构造任意 SQL
SelectStatementProvider selectStatement = select(id, firstName, lastName, birthDate, employed, occupation) .from(employee) .where(firstName, isEqualTo("Bob"), or(firstName, isEqualTo("Alice"))) .build() .render(RenderingStrategy.MYBATIS3); List<Employee> employees = mapper.selectMany(selectStatement);
这是一个新库的说,至少:
-
需要额外依赖
mybatis-dynamic-sql<dependency> <groupId>org.mybatis.dynamic-sql</groupId> <artifactId>mybatis-dynamic-sql</artifactId> <version>1.0.0</version> </dependency> -
Java 8 及以上
-
MyBatis 3.4.2 及以上
MyBatis Generator (>= 1.3.6) 也已经提供支持,只需要将 context 的 targetRuntime 属性更改为 MyBatis3DynamicSQL 即可生成新的动态 SQL。
<generatorConfiguration>
...
<context ... targetRuntime="MyBatis3DynamicSQL" ...>
...
</context>
</generatorConfiguration>
该模式下不再生成 XML,不再生成 Example 类。文档里也推荐:join 操作要用到的 resultMap 应该是 XML 文件中的唯一元素。
话说个人还是挺偏好 XML 的 ╮( ̄▽ ̄)╭ ,不知道各位老司机怎么看?
写了一篇文章简单介绍一下基本的使用 Mybatis Dynamic SQL - 重新定义 Mybatis 动态 SQL (〃ノωノ)