MyBatis Generator实现分页插件

 新建工程,继承PluginAdapter

<dependencies>
    <dependency>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-core</artifactId>
        <version>1.3.5</version>
        <scope>provided</scope>
    </dependency>
</dependencies>
public class PaginationPlugin extends PluginAdapter {

    public boolean validate(List<String> list) {
        return true;
    }

    /**
     * 为每个Example类添加limitoffset属性已经setget方法
     */
    @Override
    public boolean modelExampleClassGenerated(TopLevelClass topLevelClass,
                                              IntrospectedTable introspectedTable) {

        PrimitiveTypeWrapper integerWrapper = FullyQualifiedJavaType.getIntInstance()
                .getPrimitiveTypeWrapper();

        Field limit = new Field();
        limit.setName("limit");
        limit.setVisibility(JavaVisibility.PRIVATE);
        limit.setType(integerWrapper);
        topLevelClass.addField(limit);

        Method setLimit = new Method();
        setLimit.setVisibility(JavaVisibility.PUBLIC);
        setLimit.setName("setLimit");
        setLimit.addParameter(new Parameter(integerWrapper, "limit"));
        setLimit.addBodyLine("this.limit = limit;");
        topLevelClass.addMethod(setLimit);

        Method getLimit = new Method();
        getLimit.setVisibility(JavaVisibility.PUBLIC);
        getLimit.setReturnType(integerWrapper);
        getLimit.setName("getLimit");
        getLimit.addBodyLine("return limit;");
        topLevelClass.addMethod(getLimit);

        Field offset = new Field();
        offset.setName("offset");
        offset.setVisibility(JavaVisibility.PRIVATE);
        offset.setType(integerWrapper);
        topLevelClass.addField(offset);

        Method setOffset = new Method();
        setOffset.setVisibility(JavaVisibility.PUBLIC);
        setOffset.setName("setOffset");
        setOffset.addParameter(new Parameter(integerWrapper, "offset"));
        setOffset.addBodyLine("this.offset = offset;");
        topLevelClass.addMethod(setOffset);

        Method getOffset = new Method();
        getOffset.setVisibility(JavaVisibility.PUBLIC);
        getOffset.setReturnType(integerWrapper);
        getOffset.setName("getOffset");
        getOffset.addBodyLine("return offset;");
        topLevelClass.addMethod(getOffset);

        return true;
    }

    /**
     * Mapper.xml selectByExample 添加 limit
     */
    @Override
    public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element,
                                                                     IntrospectedTable introspectedTable) {
        addPageElement(element);
        return true;
    }

    /**
     * Mapper.xml selectByExampleWithBLOBs 添加 limit
     */
    @Override
    public boolean sqlMapSelectByExampleWithBLOBsElementGenerated(XmlElement element,
                                                                  IntrospectedTable introspectedTable) {
        addPageElement(element);
        return true;
    }

    private void addPageElement(XmlElement element) {
        XmlElement ifLimitNotNullElement = new XmlElement("if");
        ifLimitNotNullElement.addAttribute(new Attribute("test", "limit != null"));

        XmlElement ifOffsetNotNullElement = new XmlElement("if");
        ifOffsetNotNullElement.addAttribute(new Attribute("test", "offset != null"));
        ifOffsetNotNullElement.addElement(new TextElement("limit ${offset}, ${limit}"));
        ifLimitNotNullElement.addElement(ifOffsetNotNullElement);

        XmlElement ifOffsetNullElement = new XmlElement("if");
        ifOffsetNullElement.addAttribute(new Attribute("test", "offset == null"));
        ifOffsetNullElement.addElement(new TextElement("limit ${limit}"));
        ifLimitNotNullElement.addElement(ifOffsetNullElement);

        element.addElement(ifLimitNotNullElement);
    }
}

在要用的模块引入插件

<build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.3.5</version>
            <configuration>
                <overwrite>true</overwrite>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>5.1.34</version>
                </dependency>
                <dependency>
                    <groupId>****</groupId>
                    <artifactId>near-mybatis-generator-extends</artifactId>
                    <version>1.0.0</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

generatorConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>

    <context id="mysql_systemcore" targetRuntime="MyBatis3">

        <plugin type="org.***.plugins.PaginationPlugin"/>

        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://192.168.1.45:3306/***"
                        userId="***"
                        password="***"/>

        <javaModelGenerator targetPackage="com.***.dal.dataobj"
                            targetProject="src/main/java">
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="sqlmap"
                         targetProject="src/main/resources/META-INF"/>

        <javaClientGenerator targetPackage="com.***.dal.mapper"
                             targetProject="src/main/java" type="XMLMAPPER"/>
        <!--
        <table tableName="usr_wechat_user"
               domainObjectName="WechatUser"
               enableDeleteByPrimaryKey="false"
               enableDeleteByExample="false"
               enableCountByExample="false"/>
        -->
        <table tableName="user_info"
               domainObjectName="UserInfoDO"/>

    </context>
</generatorConfiguration>


MyBatis Generator(MBG)是一个自动生成MyBatis框架代码的工具。MBG通过读取数据库表结构信息,生成基本的Mapper、Model和Example代码。 在使用MBG时,可以通过配置文件指定生成的代码的格式、包名、注释等信息。MBG支持通过插件扩展生成的代码的功能,例如自动生成分页查询代码、生成基于XML的批量操作代码等。 以下是MBG配置文件中的插件配置示例: ``` <generatorConfiguration> <!-- 插件配置 --> <context id="mysql" targetRuntime="MyBatis3"> <plugin type="org.mybatis.generator.plugins.SerializablePlugin" /> <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin" /> <plugin type="org.mybatis.generator.plugins.PaginationPlugin"> <property name="pageLimit" value="10" /> </plugin> <plugin type="org.mybatis.generator.plugins.BatchInsertPlugin" /> <plugin type="org.mybatis.generator.plugins.BatchUpdatePlugin" /> <plugin type="org.mybatis.generator.plugins.BatchDeletePlugin" /> <!-- 其他配置 --> </context> </generatorConfiguration> ``` 上述配置中,配置了如下插件: - SerializablePlugin:自动生成Serializable接口实现类,用于支持缓存等特性。 - RowBoundsPlugin:自动生成基于RowBounds的分页查询接口方法。 - PaginationPlugin:自动生成基于MySQL的分页查询接口方法,并设置每页显示条数为10。 - BatchInsertPlugin:自动生成批量插入数据的接口方法。 - BatchUpdatePlugin:自动生成批量更新数据的接口方法。 - BatchDeletePlugin:自动生成批量删除数据的接口方法。 通过配置插件,可以快速生成常用的代码,提高开发效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值