mybatis generator外掛系列--分頁外掛
阿新 • • 發佈:2018-12-10
1、首先定義分頁外掛
MysqlPagePlugin.java
package com.demo.mybatis.plugin; import org.mybatis.generator.api.CommentGenerator; import org.mybatis.generator.api.IntrospectedTable; import org.mybatis.generator.api.PluginAdapter; import org.mybatis.generator.api.dom.java.*; import org.mybatis.generator.api.dom.xml.Attribute; import org.mybatis.generator.api.dom.xml.TextElement; import org.mybatis.generator.api.dom.xml.XmlElement; import java.util.List; /** * <pre> * add pagination using mysql limit. * This class is only used in ibator code generator. * </pre> */ /** * mysql 分頁生成外掛 */ public class MysqlPagePlugin extends PluginAdapter { /** * 新增 分頁 開始行數 和結束行數 屬性 */ @Override public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { // add field, getter, setter for limit clause addProperty(topLevelClass, introspectedTable, "limitStart", FullyQualifiedJavaType.getIntInstance()); addProperty(topLevelClass, introspectedTable, "limitEnd", FullyQualifiedJavaType.getIntInstance()); addProperty(topLevelClass, introspectedTable, "groupByClause", FullyQualifiedJavaType.getStringInstance()); return super.modelExampleClassGenerated(topLevelClass, introspectedTable); } /** * 新增 對映 檔案配置 limit 的配置 */ @Override public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated( XmlElement element, IntrospectedTable introspectedTable) { // XmlElement isParameterPresenteElemen = (XmlElement) element.getElements(); //設定 if 判斷 節點 XmlElement limitElement = new XmlElement("if"); //$NON-NLS-1$ //給 節點新增 條件運算子 limitElement.addAttribute(new Attribute("test", "limitEnd > 0")); //$NON-NLS-1$ //$NON-NLS-2$ //如果條件成立 就進行分頁查詢 limitElement.addElement(new TextElement( "limit #{limitStart,jdbcType=INTEGER} , #{limitEnd,jdbcType=INTEGER}")); //新增節點到 配置檔案中 element.addElement(limitElement); XmlElement groupbyElement = new XmlElement("if"); //$NON-NLS-1$ //給 節點新增 條件運算子 groupbyElement.addAttribute(new Attribute("test", "groupByClause != null")); //$NON-NLS-1$ //$NON-NLS-2$ //如果條件成立 就進行分頁查詢 groupbyElement.addElement(new TextElement( "group by ${groupByClause}")); //新增節點到 配置檔案中 element.addElement(groupbyElement); return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element, introspectedTable); } /** * 給對應的實體 實體新增 屬性欄位 */ private void addProperty(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, String name, FullyQualifiedJavaType fullyQualifiedJavaType) { CommentGenerator commentGenerator = context.getCommentGenerator(); Field field = new Field(); field.setVisibility(JavaVisibility.PROTECTED); field.setType(fullyQualifiedJavaType); field.setName(name); // field.setInitializationString("-1"); commentGenerator.addFieldComment(field, introspectedTable); topLevelClass.addField(field); char c = name.charAt(0); String camel = Character.toUpperCase(c) + name.substring(1); Method method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setName("set" + camel); method.addParameter(new Parameter(fullyQualifiedJavaType, name)); method.addBodyLine("this." + name + "=" + name + ";"); commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setReturnType(fullyQualifiedJavaType); method.setName("get" + camel); method.addBodyLine("return " + name + ";"); commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); } /** * This plugin is always valid - no properties are required */ public boolean validate(List<String> warnings) { return true; } // public static void generate() { // String config = MysqlPagePlugin.class.getClassLoader().getResource("generatorConfig-w5Log.xml").getFile(); // String[] arg = { "-configfile", config, "-overwrite" }; // ShellRunner.main(arg); // } // public static void main(String[] args) { // generate(); // } }
2、然後為mybatisgenerator配置外掛
<?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="context1"> <!-- 使用自帶序列化外掛 --> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/> <!-- 使用自定義的外掛 --> <plugin type="com.demo.mybatis.plugin.MysqlPagePlugin"/> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF8" userId="root" password="123456"> </jdbcConnection> <javaModelGenerator targetPackage="com.ilovey.biz.entity.base" targetProject="ilovey.biz/src/main/java"/> <sqlMapGenerator targetPackage="com.ilovey.biz.mapper.base" targetProject="ilovey.biz/src/main/resources"/> <javaClientGenerator targetPackage="com.ilovey.biz.mapper.base" targetProject="ilovey.biz/src/main/java" type="XMLMAPPER"/> <table tableName="us_user_info" domainObjectName="UsUserInfo"> <generatedKey column="id" sqlStatement="MySql" identity="true"/> </table> </context> </generatorConfiguration>
3、使用示例
@Repository public class UsUserInfoDao { @Autowired private UsUserInfoMapper usUserMapper; /** * 分頁查詢使用者資訊 * @param kinCode 使用者型別 * @param page 分頁引數(page為自定義的物件) * @return */ public List<UsUserInfo> listUserInfo(String kinCode, Page page) { UsUserInfoExample example = new UsUserInfoExample(); example.createCriteria().andKindCodeEqualTo(kinCode); //分頁 example.setLimitStart(page.limitStart()); example.setLimitEnd(page.limitEnd()); //排序 example.setOrderByClause("create_time desc"); //查詢總數 page.setTotalCount(usUserMapper.countByExample(example)); return usUserMapper.selectByExample(example); } }