mysql mybatis-generator plugin 分页
generator.xml配置如下:
plugin必须紧跟context,否则会报错
<?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> <!-- oracle lib location --> <classPathEntry location="E:\backup\repository\mysql\mysql-connector-java\5.1.40\mysql-connector-java-5.1.40.jar" /> <context id="DB2Tables" targetRuntime="MyBatis3"> <!-- 生成的pojo,将implements Serializable --> <!-- <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin> --> <plugin type="cn.zsmy.tmp.DeleteLogicByIdsPlugin"></plugin> <plugin type="cn.zsmy.tmp.MySQLLimitPlugin"></plugin> <commentGenerator> <property name="suppressAllComments" value="true" /> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://192.168.1.2:3306/palm_2_0_16" userId="root" password="sqj888"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- model package and location --> <javaModelGenerator targetPackage="cn.zsmy.entity" targetProject="palmdoctor.code\src\main\java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- mapping package and location --> <sqlMapGenerator targetPackage="cn.zsmy.mapper" targetProject="palmdoctor.code\src\main\java"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- dao package and location --> <javaClientGenerator type="XMLMAPPER" targetPackage="cn.zsmy.mapper" targetProject="palmdoctor.code\src\main\java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- enableSelectByExample不为true就不能生成分页的示例 --> <table tableName="tb_hello" domainObjectName="Hello" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="true" selectByExampleQueryId="false" /> </context> </generatorConfiguration>
package cn.zsmy.tmp; import java.util.List; import org.mybatis.generator.api.IntrospectedTable; import org.mybatis.generator.api.PluginAdapter; import org.mybatis.generator.api.dom.java.Field; import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; import org.mybatis.generator.api.dom.java.JavaVisibility; import org.mybatis.generator.api.dom.java.Method; import org.mybatis.generator.api.dom.java.Parameter; import org.mybatis.generator.api.dom.java.PrimitiveTypeWrapper; import org.mybatis.generator.api.dom.java.TopLevelClass; import org.mybatis.generator.api.dom.xml.Attribute; import org.mybatis.generator.api.dom.xml.TextElement; import org.mybatis.generator.api.dom.xml.XmlElement; public class MySQLLimitPlugin extends PluginAdapter { @Override public boolean validate(List<String> warnings) { return true; } /** * 为每个Example类添加limit和offset属性已经set、get方法 */ @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) { 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); return true; } }
效果如下图:只能在selectByExample方法中生成
再来个自定义方法,上面的generator.xml已加入该plugin,有个问题,就是自定义的方法在mapper.xml里生成了,但是却没有在mapper.java里生成,不清楚怎么回事。
package cn.zsmy.tmp; import java.util.List; import org.mybatis.generator.api.IntrospectedTable; import org.mybatis.generator.api.PluginAdapter; import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; import org.mybatis.generator.api.dom.java.Interface; import org.mybatis.generator.api.dom.java.Method; import org.mybatis.generator.api.dom.java.Parameter; import org.mybatis.generator.api.dom.java.TopLevelClass; import org.mybatis.generator.api.dom.xml.Attribute; import org.mybatis.generator.api.dom.xml.Document; import org.mybatis.generator.api.dom.xml.TextElement; import org.mybatis.generator.api.dom.xml.XmlElement; public class DeleteLogicByIdsPlugin extends PluginAdapter { /** * {@inheritDoc} */ public boolean validate(List<String> warnings) { return true; } /** * {@inheritDoc} */ @Override public boolean clientSelectByExampleWithBLOBsMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) { interfaze.addMethod(generateDeleteLogicByIds(method, introspectedTable)); return true; } /** * {@inheritDoc} */ @Override public boolean clientSelectByExampleWithoutBLOBsMethodGenerated( Method method, Interface interfaze, IntrospectedTable introspectedTable) { interfaze.addMethod(generateDeleteLogicByIds(method, introspectedTable)); return true; } /** * {@inheritDoc} */ @Override public boolean clientSelectByExampleWithBLOBsMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { topLevelClass.addMethod(generateDeleteLogicByIds(method, introspectedTable)); return true; } /** * {@inheritDoc} */ @Override public boolean clientSelectByExampleWithoutBLOBsMethodGenerated( Method method, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { topLevelClass.addMethod(generateDeleteLogicByIds(method, introspectedTable)); return true; } @Override public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) { String tableName = introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime();//数据库表名 XmlElement parentElement = document.getRootElement(); // 产生分页语句前半部分 XmlElement deleteLogicByIdsElement = new XmlElement("update"); deleteLogicByIdsElement.addAttribute(new Attribute("id", "deleteLogicByIds")); deleteLogicByIdsElement.addElement( new TextElement( "update " + tableName + " set deleteFlag = #{deleteFlag,jdbcType=INTEGER} where id in " + " <foreach item=\"item\" index=\"index\" collection=\"ids\" open=\"(\" separator=\",\" close=\")\">#{item}</foreach> " )); parentElement.addElement(deleteLogicByIdsElement); return super.sqlMapDocumentGenerated(document, introspectedTable); } private Method generateDeleteLogicByIds(Method method, IntrospectedTable introspectedTable) { Method m = new Method("deleteLogicByIds"); m.setVisibility(method.getVisibility()); m.setReturnType(FullyQualifiedJavaType.getIntInstance()); m.addParameter(new Parameter(FullyQualifiedJavaType.getIntInstance(), "deleteFlag", "@Param(\"deleteFlag\")")); m.addParameter(new Parameter(new FullyQualifiedJavaType("Integer[]"), "ids", "@Param(\"ids\")")); context.getCommentGenerator().addGeneralMethodComment(m, introspectedTable); return m; } }
看完打开支付宝扫一扫领个红包吧!