应该说使用Mybatis就一定离不开MyBatis Generator这款代码生成插件,而这款插件自身还提供了插件拓展功能用于强化插件本身,官方已经提供了一些拓展插件,本项目的目的也是通过该插件机制来强化Mybatis Generator本身,方便和减少我们平时的代码开发量。
因为插件是本人兴之所至所临时发布的项目(本人已近三年未做JAVA开发,代码水平请大家见谅),但基本插件都是在实际项目中经过检验的请大家放心使用,但因为项目目前主要数据库为MySQL,Mybatis实现使用Mapper.xml方式,所以代码生成时对于其他数据库和注解方式的支持未予考虑,请大家见谅。
插件列表:
- 查询单条数据插件
- MySQL分页插件
- 数据Model链式构建插件
- Example Criteria 增强插件(example,andIf)
- Example 目标包修改插件
Maven引用:
<dependency>
<groupId>com.itfsw</groupId>
<artifactId>mybatis-generator-plugin</artifactId>
<version>1.0.2</version>
</dependency>
对应表Mapper接口增加了方法
插件:
<!-- 查询单条数据插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.SelectOneByExamplePlugin"/>
使用:
public interface TbMapper {
/**
* 这是Mybatis Generator拓展插件生成的方法(请勿删除).
* This method corresponds to the database table tb
*
* @mbg.generated
* @author hewei
*/
Tb selectOneByExample(TbExample example);
}
对应表Example类增加了方法
插件:
<!-- MySQL分页插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.LimitPlugin"/>
使用:
public class TbExample {
/**
* 这是Mybatis Generator拓展插件生成的属性(请勿删除).
* This field corresponds to the database table tb
*
* @mbg.generated
* @author hewei
*/
protected Integer offset;
/**
* 这是Mybatis Generator拓展插件生成的属性(请勿删除).
* This field corresponds to the database table tb
*
* @mbg.generated
* @author hewei
*/
protected Integer rows;
/**
* 这是Mybatis Generator拓展插件生成的方法(请勿删除).
* This method corresponds to the database table rc_user_token
*
* @mbg.generated
* @author hewei
*/
public void limit(Integer rows) {
this.rows = rows;
}
/**
* 这是Mybatis Generator拓展插件生成的方法(请勿删除).
* This method corresponds to the database table rc_user_token
*
* @mbg.generated
* @author hewei
*/
public void limit(Integer offset, Integer rows) {
this.offset = offset;
this.rows = rows;
}
/**
* 这是Mybatis Generator拓展插件生成的方法(请勿删除).
* This method corresponds to the database table tb
*
* @mbg.generated
* @author hewei
*/
public void page(Integer page, Integer pageSize) {
this.offset = page * pageSize;
this.rows = pageSize;
}
// offset 和 rows 的getter&setter
// 修正了clear方法
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table tb
*
* @mbg.generated
*/
public void clear() {
oredCriteria.clear();
orderByClause = null;
distinct = false;
rows = null;
offset = null;
}
}
这个是仿jquery的链式调用强化了表的Model的赋值操作
插件:
<!-- 数据Model链式构建插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.ModelBuilderPlugin"/>
使用:
public class Test {
public static void main(String[] args) {
// 直接new表Model的内部Builder类,赋值后调用build()方法返回对象
Tb table = new Tb.Builder()
.field1("xx")
.field2("xx")
.field3("xx")
.field4("xx")
.build();
}
}
- 表Example增加Criteria的快速返回example()方法。
- Criteria链式调用增强,以前如果有按条件增加的查询语句会打乱链式查询构建,现在有了andIf(boolean ifAdd, CriteriaAdd add)方法可一直使用链式调用下去。
插件:
<!-- Example Criteria 增强插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.CriteriaBuilderPlugin"/>
使用:
public class Test {
public static void main(String[] args) {
// -----------------------------------example-----------------------------------
// 表Example.Criteria增加了工厂方法example()支持,使用后可链式构建查询条件使用example()返回Example对象
TbExample ex = new TbExample()
.createCriteria()
.andField1EqualTo(1)
.andField2EqualTo("xxx")
.example();
this.tbMapper.selectByExample(ex);
// -----------------------------------andIf-----------------------------------
// Criteria增强了链式调用,现在一些按条件增加的查询条件不会打乱链式调用了
// old
TbExample oldEx = new TbExample();
TbExample.Criteria criteria = oldEx
.createCriteria()
.andField1EqualTo(1)
.andField2EqualTo("xxx");
// 如果随机数大于0.5,附加Field3查询条件
if (Math.random() > 0.5){
criteria.andField3EqualTo(2)
.andField4EqualTo(new Date());
}
this.tbMapper.selectByExample(oldEx);
// new
TbExample newEx;
newEx = new TbExample()
.createCriteria()
.andField1EqualTo(1)
.andField2EqualTo("xxx")
// 如果随机数大于0.5,附加Field3查询条件
.andIf(Math.random() > 0.5, new TbExample.Criteria.CriteriaAdd() {
@Override
public TbExample.Criteria add(TbExample.Criteria add) {
return add.andField3EqualTo(2)
.andField4EqualTo(new Date());
}
})
// 当然最简洁的写法是采用java8的Lambda表达式,当然你的项目是Java8+
.andIf(Math.random() > 0.5, add -> add
.andField3EqualTo(2)
.andField4EqualTo(new Date())
)
.example();
this.tbMapper.selectByExample(newEx);
}
}
Mybatis Generator 插件默认把Model类和Example类都生成到一个包下,这样该包下类就会很多不方便区分,该插件目的就是把Example类独立到一个新包下,方便查看。
插件:
<!-- Example 目标包修改插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.ExampleTargetPlugin">
<!-- 修改Example类生成到目标包下 -->
<property name="targetPackage" value="com.itfsw.mybatis.generator.dao.example"/>
</plugin>