MyBatis 的 Example
在 逆向工程 中,我们可以根据数据库的表自动生产 MyBatis 所需的 mapper.java、mapper.xml、po.java、poExample.java。前三个我们已经非常熟悉了,而未使用过 poExample 类,直到最近在项目中碰到了,在这里总结一下。
Example
Example类指定了如何构建一个动态的 where 子句. 表中的每个表的列可以被包括在 where 子句中。主要用于简化我们 sql 语句的编写。
Example类包含一个内部静态类 Criteria,而 Criteria中 的方法是定义 SQL 语句 where 后的查询条件。Criteria 对象可以通过 example 对象的 createCriteria 方法创建。
要生成 Example,需要注意下 generatorConfig.xml 里的配置:
1 | ini复制代码 <table tableName="user" domainObjectName="User"> |
Criteria 中的常用方法
案例
- 使用 criteria:
1 | ini复制代码UserExample example =new UserExample();//由逆向工程生成 |
- 不使用 criteria:
1 | ini复制代码UserExample example = new UserExample(); |
- and 和 or
1 | ini复制代码UserExample example = new UserExample(); |
本文转载自: 掘金