MyBatis Generator实现MySQL分页插件

本文介绍了如何让MyBatis Generator生成的代码具备分页功能,通过RowBoundsPlugin和自定义limit分页插件的方式实现。讨论了RowBoundsPlugin的工作原理,并提供了自定义插件的实现思路和代码,最后分享了GitHub上的资源链接。
摘要由CSDN通过智能技术生成

MyBatis Generator是一个非常方便的代码生成工具,它能够根据表结构生成CRUD代码,可以满足大部分需求。但是唯一让人不爽的是,生成的代码中的数据库查询没有分页功能。本文介绍如何让MyBatis Generator生成的代码具有分页功能。

MyBatis Generator结合Maven的配置和使用

在实现分页之前,首先简单介绍MyBatis Generator如何使用。

MyBatis Generator配置文件

MyBatis Generator通常会有一个xml配置文件,用来指定连接的数据库、哪些表、如何生成代码。详情可以参考官方文档:http://www.mybatis.org/generator/configreference/xmlconfig.html 。下面给出一份简单的配置,文件命名为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="mysqlgenerator" targetRuntime="MyBatis3">

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/yourdb?useUnicode=true&amp;characterEncoding=UTF-8"
            userId="user" password="password" />

        <javaModelGenerator targetPackage="com.xxg.bean" targetProject="src/main/java" />
        <sqlMapGenerator targetPackage="com.xxg.mapper" targetProject="src/main/resources" />
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.xxg.mapper" targetProject="src/main/java" />

        <table tableName="table_a" />
        <table tableName="table_b" />
        <table tableName="table_c" />
        <table tableName="table_d" />

    </context>
</generatorConfiguration>

Maven配置

官网文档中提供了四种MyBatis Generator生成代码的运行方式:命令行、使用Ant、使用Maven、Java编码。本文采用Maven插件mybatis-generator-maven-plugin来运行MyBatis Generator,详细配置同样可以参考官方文档:http://www.mybatis.org/generator/running/runningWithMaven.html 。下面给出一份简单的pom.xml的配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin
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:自动生成批量删除数据的接口方法。 通过配置插件,可以快速生成常用的代码,提高开发效率。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值