mybatis generator mysql分页

mybatis generator生成工具是非常强大的。基本上满足我们的很多需求了。

但是如果想像下面这样的,暂时还是木有滴:

    List<PhysicalOrderInfo> selectPage(@Param("info")PhysicalOrderInfo info, @Param("pageable")Pageable pageable);

    int selectCount(@Param("info")PhysicalOrderInfo info);
<select id="selectPage" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from physical_order_info where 1 = 1
    <if test="info.productName != null">
     and product_name = #{info.productName,jdbcType=VARCHAR}
    </if>
    <if test="info.productNumber != null">
      and  product_number = #{info.productNumber,jdbcType=VARCHAR}
    </if>
    <if test="info.orderNumber != null">
      and  order_number = #{info.orderNumber,jdbcType=VARCHAR}
    </if>
    <if test="info.wayId != null">
      and  way_id = #{info.wayId,jdbcType=INTEGER}
    </if>
    <if test="info.updateTime != null">
      and update_time = #{info.updateTime,jdbcType=TIMESTAMP}
    </if>
    <if test="pageable.offset >= 0 and pageable.pageSize > 0">
      limit ${pageable.offset}, ${pageable.pageSize}
    </if>
  </select>

所以可以自己简单修改一下源码,实现selectPage,selectCount等功能。

修改的比较简单粗暴,给自己使用的。如果想写好点,可以自行修改。

本文所讲源码地址

https://github.com/daiagou/generator

也可以直接下载jar文件通过命令使用地址:

https://raw.githubusercontent.com/daiagou/generator/master/core/bin/mybatis-generator-core-1.3.7-SNAPSHOT.jar

1.dao部分:

org.mybatis.generator.api.dom.java.Interface.java,org.mybatis.generator.api.dom.java.InnerInterface.java为生成dao部分的主要代码,

可以再里面很简单的加上你要的selectPage,和selectCount方法,比如:

Interface.java中增加:

        sb.append("import org.apache.ibatis.annotations.Param;");
        newLine(sb);
        sb.append("import org.springframework.data.domain.Pageable;");
        newLine(sb);

InnerInterface.java中增加:

        //添加selectPage
        Method selectPage = new Method("selectPage");
        FullyQualifiedJavaType selectPageReturnType = new FullyQualifiedJavaType(returnType);
        selectPage.setReturnType(selectPageReturnType);
        newLine(sb);
        sb.append(selectPage.getSelectPage());
        //selectCount
        Method selectCount = new Method("selectCount");
        FullyQualifiedJavaType selectCountReturnType = new FullyQualifiedJavaType(returnType);
        selectPage.setReturnType(selectCountReturnType);
        newLine(sb);
        sb.append(selectPage.getSelectCount());

2.xml部分:

xml部分稍微麻烦一点

org.mybatis.generator.api.dom.xml.Document.java和org.mybatis.generator.api.dom.xml.XmlElement

为主要xml生成代码,但是直接修改没dao那么简单。

所以新增org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.SelectPageElementGenerator.java

和org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.SelectCountElementGenerator.java

package org.mybatis.generator.codegen.mybatis3.xmlmapper.elements;

import org.mybatis.generator.api.IntrospectedColumn;
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 org.mybatis.generator.codegen.mybatis3.ListUtilities;
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;

/**
 *
 * @author abner
 *
 */
public class SelectPageElementGenerator extends
        AbstractXmlElementGenerator {

    public SelectPageElementGenerator() {
        super();
    }

    @Override
    public void addElements(XmlElement parentElement) {
        XmlElement answer = new XmlElement("select"); //$NON-NLS-1$

        answer.addAttribute(new Attribute(
                "id", introspectedTable.getSelectPageStatementId())); //$NON-NLS-1$
        answer.addAttribute(new Attribute("resultMap", //$NON-NLS-1$
                introspectedTable.getBaseResultMapId()));


        context.getCommentGenerator().addComment(answer);

        StringBuilder sb = new StringBuilder();


        sb.append("select\n" +
                "    <include refid=\"Base_Column_List\" />\n" +
                "    from ");

        sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
        sb.append(" where 1 = 1");
        answer.addElement(new TextElement(sb.toString()));


        for (IntrospectedColumn introspectedColumn : ListUtilities.removeGeneratedAlwaysColumns(introspectedTable
                .getNonPrimaryKeyColumns())) {
            sb.setLength(0);
            sb.append("record.");
            sb.append(introspectedColumn.getJavaProperty());
            sb.append(" != null"); //$NON-NLS-1$
            XmlElement isNotNullElement = new XmlElement("if"); //$NON-NLS-1$
            isNotNullElement.addAttribute(new Attribute("test", sb.toString())); //$NON-NLS-1$
            answer.addElement(isNotNullElement);

            sb.setLength(0);
            sb.append("and ");
            sb.append(MyBatis3FormattingUtilities
                    .getEscapedColumnName(introspectedColumn));
            sb.append(" = "); //$NON-NLS-1$
            sb.append(MyBatis3FormattingUtilities
                    .getParameterPageClause(introspectedColumn));

            isNotNullElement.addElement(new TextElement(sb.toString()));
        }


        sb.setLength(0);
        sb.append("<if test=\"pageable!=null\">\n" +
                "      <if test=\"pageable.offset = 0 and pageable.pageSize > 0\">\n" +
                "        limit ${pageable.pageSize}\n" +
                "     </if>\n" +
                "\t <if test=\"pageable.offset > 0 and pageable.pageSize > 0\">\n" +
                "        limit ${pageable.offset}, ${pageable.pageSize}\n" +
                "     </if>\n" +
                "    </if>");
        answer.addElement(new TextElement(sb.toString()));

        if (context.getPlugins()
                .sqlMapUpdateByPrimaryKeySelectiveElementGenerated(answer,
                        introspectedTable)) {
            parentElement.addElement(answer);
        }
    }
}
package org.mybatis.generator.codegen.mybatis3.xmlmapper.elements;

import org.mybatis.generator.api.IntrospectedColumn;
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 org.mybatis.generator.codegen.mybatis3.ListUtilities;
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;

/**
 *
 * @author abner
 *
 */
public class SelectCountElementGenerator extends
        AbstractXmlElementGenerator {

    public SelectCountElementGenerator() {
        super();
    }

    @Override
    public void addElements(XmlElement parentElement) {
        XmlElement answer = new XmlElement("select"); //$NON-NLS-1$

        answer.addAttribute(new Attribute(
                "id", introspectedTable.getSelectCountStatementId())); //$NON-NLS-1$
        answer.addAttribute(new Attribute("resultMap", //$NON-NLS-1$
        "java.lang.Integer"));

        context.getCommentGenerator().addComment(answer);

        StringBuilder sb = new StringBuilder();


        sb.append("select count(1)\n" +
                "    from ");

        sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
        sb.append(" where 1 = 1");
        answer.addElement(new TextElement(sb.toString()));


        for (IntrospectedColumn introspectedColumn : ListUtilities.removeGeneratedAlwaysColumns(introspectedTable
                .getNonPrimaryKeyColumns())) {
            sb.setLength(0);
            sb.append("record.");
            sb.append(introspectedColumn.getJavaProperty());
            sb.append(" != null"); //$NON-NLS-1$
            XmlElement isNotNullElement = new XmlElement("if"); //$NON-NLS-1$
            isNotNullElement.addAttribute(new Attribute("test", sb.toString())); //$NON-NLS-1$
            answer.addElement(isNotNullElement);

            sb.setLength(0);
            sb.append("and ");
            sb.append(MyBatis3FormattingUtilities
                    .getEscapedColumnName(introspectedColumn));
            sb.append(" = "); //$NON-NLS-1$
            sb.append(MyBatis3FormattingUtilities
                    .getParameterPageClause(introspectedColumn));

            isNotNullElement.addElement(new TextElement(sb.toString()));
        }



        if (context.getPlugins()
                .sqlMapUpdateByPrimaryKeySelectiveElementGenerated(answer,
                        introspectedTable)) {
            parentElement.addElement(answer);
        }
    }
}
然后在org.mybatis.generator.codegen.mybatis3.xmlmapper.XMLMapperGenerator.java中add

        //添加selectPage,selectCount
        addSelectPageElement(answer);
        addSelectCountElement(answer);
    protected void addSelectPageElement(
            XmlElement parentElement) {
        if (introspectedTable.getRules().generateUpdateByPrimaryKeySelective()) {
            AbstractXmlElementGenerator elementGenerator = new SelectPageElementGenerator();
            initializeAndExecuteGenerator(elementGenerator, parentElement);
        }
    }
    protected void addSelectCountElement(
            XmlElement parentElement) {
        if (introspectedTable.getRules().generateUpdateByPrimaryKeySelective()) {
            AbstractXmlElementGenerator elementGenerator = new SelectCountElementGenerator();
            initializeAndExecuteGenerator(elementGenerator, parentElement);
        }
    }
上面贴的代码不全哈,原来里面自己看看,写的简单,只提供思路,抛砖引玉。


猜你喜欢

转载自blog.csdn.net/plm609337931/article/details/79880070