使用mybatis-generator添加自定义分页插件时提示无法实例化插件类

1.插件类

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.Document;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;


import java.util.List;


/**
 * Example/Mapper增加分页信息
 * Created by Administrator on 2016-10-31.
 */
public class MysqlPagePlugin extends PluginAdapter {


    /**
     * This plugin is always valid - no properties are required
     * @param list
     * @return
     */
    @Override
    public boolean validate(List<String> list) {
        return true;
    }


    @Override
    public boolean modelExampleClassGenerated(TopLevelClass topLevelClass,
                                              IntrospectedTable introspectedTable) {
        // add field, getter, setter for limit clause
        addPage(topLevelClass, introspectedTable, "page");
        return super.modelExampleClassGenerated(topLevelClass,
                introspectedTable);
    }


    @Override
    public boolean sqlMapDocumentGenerated(Document document,
                                           IntrospectedTable introspectedTable) {
        XmlElement parentElement = document.getRootElement();


        // 产生分页语句
        XmlElement paginationSuffixElement = new XmlElement("sql");
        paginationSuffixElement.addAttribute(new Attribute("id",
                "MysqlDialectSuffix"));
        XmlElement pageEnd = new XmlElement("if");
        pageEnd.addAttribute(new Attribute("test", "page != null"));
        pageEnd.addElement(new TextElement(
                "LIMIT #{page.begin}, #{page.length}"));
        paginationSuffixElement.addElement(pageEnd);
        parentElement.addElement(paginationSuffixElement);


        return super.sqlMapDocumentGenerated(document, introspectedTable);
    }


    @Override
    public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(
            XmlElement element, IntrospectedTable introspectedTable) {


        XmlElement isNotNullElement = new XmlElement("include"); //$NON-NLS-1$
        isNotNullElement.addAttribute(new Attribute("refid",
                "MysqlDialectSuffix"));
        element.getElements().add(isNotNullElement);


        return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element,
                introspectedTable);
    }


    @Override
    public boolean sqlMapSelectByExampleWithBLOBsElementGenerated(
            XmlElement element, IntrospectedTable introspectedTable) {


        XmlElement isNotNullElement = new XmlElement("include"); //$NON-NLS-1$
        isNotNullElement.addAttribute(new Attribute("refid",
                "MysqlDialectSuffix"));
        element.getElements().add(isNotNullElement);


        return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element,
                introspectedTable);
    }


    /**
     * @param topLevelClass
     * @param introspectedTable
     * @param name
     */
    private void addPage(TopLevelClass topLevelClass,
                         IntrospectedTable introspectedTable, String name) {
        topLevelClass.addImportedType(new FullyQualifiedJavaType(
                "com.heifeng.framework.page.Page"));
        CommentGenerator commentGenerator = context.getCommentGenerator();
        Field field = new Field();
        field.setVisibility(JavaVisibility.PROTECTED);
        field.setType(new FullyQualifiedJavaType("com.heifeng.framework.page.Page"));
        field.setName(name);
        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(new FullyQualifiedJavaType(
                "com.heifeng.framework.page.Page"), name));
        method.addBodyLine("this." + name + "=" + name + ";");
        commentGenerator.addGeneralMethodComment(method, introspectedTable);
        topLevelClass.addMethod(method);
        method = new Method();
        method.setVisibility(JavaVisibility.PUBLIC);
        method.setReturnType(new FullyQualifiedJavaType(
                "com.heifeng.framework.page.Page"));
        method.setName("get" + camel);
        method.addBodyLine("return " + name + ";");
        commentGenerator.addGeneralMethodComment(method, introspectedTable);
        topLevelClass.addMethod(method);
    }
}

分页类:

import java.io.Serializable;


/**
 * 分页基础工具
 */
public class Page implements Serializable {


    // 分页查询开始记录位置
    private int begin;
    // 分页查看下结束位置
    private int end;
    // 每页显示记录数
    private int length;
    // 查询结果总记录数
    private int count;
    // 当前页码
    private int current;
    // 总共页数
    private int total;


    public Page() {
    }


    /**
     * 构造函数
     *
     * @param current
     * @param length
     */
    public Page(int current, int length) {
        this.begin = (current - 1) * length;
        this.length = length;
        this.end = this.begin + this.length;
        this.current = current;
    }


    /**
     * @param current
     * @param length
     * @param count
     */
    public Page(int current, int length, int count) {
        this(current, length);
        this.count = count;
    }


    /**
     * @return the begin
     */
    public int getBegin() {
        return begin;
    }


    /**
     * @return the end
     */
    public int getEnd() {
        return end;
    }


    /**
     * @param end
     *            the end to set
     */
    public void setEnd(int end) {
        this.end = end;
    }


    /**
     * @param begin
     *            the begin to set
     */
    public void setBegin(int begin) {
        this.begin = begin;
    }


    /**
     * @return the length
     */
    public int getLength() {
        return length;
    }


    /**
     * @param length
     *            the length to set
     */
    public void setLength(int length) {
        this.length = length;
    }


    /**
     * @return the count
     */
    public int getCount() {
        return count;
    }


    /**
     * @param count
     *            the count to set
     */
    public void setCount(int count) {
        this.count = count;
        this.total = (int) Math.floor((this.count * 1.0d) / this.length);
        if (this.count % this.length != 0) {
            this.total++;
        }
    }


    /**
     * @return the current
     */
    public int getCurrent() {
        return current;
    }


    /**
     * @param current
     *            the current to set
     */
    public void setCurrent(int current) {
        this.current = current;
        if (this.current != 0){
            this.begin = (this.current - 1) * length;
        }
    }


    /**
     * @return the total
     */
    public int getTotal() {
        if (total == 0) {
            return 1;
        }
        return total;
    }


    /**
     * @param total
     *            the total to set
     */
    public void setTotal(int total) {
        this.total = total;
    }
}

3.pom.xml配置

先需要将自己的类打成jar包,然后在mybatis-generator-maven-plugin插件中添加依赖

<plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <dependencies>
                    <dependency>
                        <groupId>com.heifeng</groupId>
                        <artifactId>bhf-framework</artifactId>
                        <version>1.0-SNAPSHOT</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>

4.generator.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>


       <!-- 引入配置文件 -->
       <properties resource="conf/db.properties"/>


       <!-- 指定数据连接驱动jar地址 -->
       <classPathEntry location="D:\configure\repository\mysql\mysql-connector-java\5.1.39\mysql-connector-java-5.1.39.jar" />


       <!-- 一个数据库一个context -->
       <context id="context1" targetRuntime="MyBatis3">
              <!-- Pagination -->
              <plugin type="com.heifeng.framework.page.MysqlPagePlugin" />
              <!-- 注释 -->
              <commentGenerator >
                     <property name="suppressAllComments" value="false"/><!-- 是否取消注释 -->
                     <property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳-->
              </commentGenerator>


              <!-- jdbc连接 -->
              <jdbcConnection driverClass="${jdbc.driver}"
                              connectionURL="${jdbc.url}" userId="${jdbc.username}"
                              password="${jdbc.password}" />


              <!-- 类型转换 -->
              <javaTypeResolver>
                     <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
                     <property name="forceBigDecimals" value="false"/>
              </javaTypeResolver>


              <!-- 生成实体类地址 -->
              <javaModelGenerator targetPackage="com.heifeng.api.domain"
                                  targetProject="src/main/java" >
                     <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
                     <property name="enableSubPackages" value="true"/>
                     <!-- 是否针对string类型的字段在set的时候进行trim调用 -->
                     <property name="trimStrings" value="false"/>
              </javaModelGenerator>


              <!-- 生成mapxml文件 -->
              <!--<sqlMapGenerator targetPackage="mapper"-->
                               <!--targetProject="src/main/resources" >-->
              <sqlMapGenerator targetPackage="com.heifeng.data.dao"
                               targetProject="src/main/java" >
                     <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
                     <property name="enableSubPackages" value="false" />
              </sqlMapGenerator>


              <!-- 生成mapxml对应client,也就是接口dao -->
              <javaClientGenerator targetPackage="com.heifeng.data.dao"
                                   targetProject="src/main/java" type="XMLMAPPER" >
                     <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
                     <property name="enableSubPackages" value="false" />
              </javaClientGenerator>


              <!-- 配置表信息 -->
              <table schema="ibs_system" tableName="cms_ad"
                     domainObjectName="Ad" enableCountByExample="true"
                     enableDeleteByExample="true" enableSelectByExample="true"
                     enableUpdateByExample="true">
                     <!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample
                         是否生成 example类   -->
              </table>
       </context>
</generatorConfiguration>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值