背景
当前mybatis-generator官方插件生成的example不支持分页参数,另外也没有继承lombok类,生成的类里有大量的getter和setter不够精简
分页实现目标
在生成的XxxExample中加入两个属性limit和offset,同时加上set和get方法。也就是需要生成以下代码:
private Integer limit;
private Integer offset;
public void setLimit(Integer limit) {
this.limit = limit;
}
public Integer getLimit() {
return limit;
}
public void setOffset(Integer offset) {
this.offset = offset;
}
public Integer getOffset() {
return offset;
}
XxxMapper.xml中在通过selectByExample查询时,添加limit:
<select id="selectByExample" parameterType="com.linlishi.XxxExample" resultMap="BaseResultMap">
...
<if test="limit != null">
<if test="offset != null">
limit ${offset}, ${limit}
</if>
<if test="offset == null">
limit ${limit}
</if>
</if>
</select>
lombok实现目标
在生成的DO类加上@Data和@NoArgsConstructor注解,引入lombok.Data和lombok.NoArgsConstructor,并且将数据库的列的comment自动添加到字段注释上
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
public class Article {
/**
* 标题
*/
private String title;
/**
* 子标题
*/
private String subTitle;
}
使用
在generator-configuration.xml 添加插件
<generatorConfiguration>
<context>
<plugin type="com.linlishi.mybatis.generator.plugin.MysqlLimitPlugin"></plugin>
<plugin type="com.linlishi.mybatis.generator.plugin.LombokPlugin"></plugin>
...
</context>
...
</generatorConfiguration>
pom.xml文件中增加依赖
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
...
<dependencies>
<dependency>
<groupId>com.linlishi</groupId>
<artifactId>mybatis-generator-plugin</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
代码已上传到https://github.com/cnlinlishi/mybatis-generator-plugin
可自主拉取,安装到本地maven库
后记
如果有更多拓展想法可联系本人,将为你实现