mybatis-generator 分页和lombok插件

本文介绍了如何对Mybatis-Generator进行扩展,以实现在生成的Example类中添加分页属性和方法,以及集成Lombok自动注解功能,使代码更简洁。作者提供了详细的配置步骤和GitHub项目链接。
摘要由CSDN通过智能技术生成

背景

当前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库

后记

如果有更多拓展想法可联系本人,将为你实现

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_33080131

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值