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

mybatis-generator 整合lombok 使用Lombok工具可以在生成实体类时省掉getter/setter以及构造方法等等代码,使得代码整体更加简洁优雅。在使用MyBatis时,可以利用generator映射数据库反向生成相对应得实体类以及mapper接口和mapper.xml文件。 这实现过程的关键时需要自己继承接口自定义一个插件实现类。 代码: pom.xml文件(引入org.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>  

使用Mybatis-Generator自动生成Dao、Model、Mapping文件实战 本文还有配套的精品资源,点击获取 简介:Mybatis-Generator是一款自动生成Mybatis代码的工具,可生成DAO、Model和Mapper XML文件,提高开发效率。本课程设计项目经过测试,旨在帮助学生掌握Mybatis-Generator的使用,包括配置XML文件、生成代码、使用自定义插件等。学生将通过实践任务,提升在Mybatis框架中开发应用的能力,为未... 阅读详情

相关推荐

UE4-(蓝图)第五十五课图片九宫格拉伸的使用

一、为什么使用九宫格拉伸 在UI布局的时候,有时候使用的图片与我们要制作的UI大小有差距,如果图片是普通方式直接拉伸很可能造成图片四个角上的效果与原图片差距太大甚至是变形,这时候使用九宫格模式拉伸,可以保持图片四个角上的样式不发生改变,只对中间区域拉伸。 原始 正常拉伸: 九宫格拉伸: 二、图片参数设置 1.新建一个UI 控件蓝图,在CanvasPanel下拖入一个Image,选择一张...

Stone的博客 7890

maven中mybatis-generator自定义插件,无法实例化的问题

maven中mybatis-generator自定义插件,无法实例化的问题 问题描述 Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.6:generate failed: Cannot instantiate object of type [你的自定义插件路径] 问...

zhaozhen的博客 3265

可视化工具--Plotly

随着信息技术的发展和硬件设备成本的降低,当今的互联网存在海量的数据,要想快速从这些数据中获取更多有效的信息,数据可视化是重要的一环。对于Python语言来说,比较传统的数据可视化模块是Matplotlib,但它存在不够美观、静态性、不易分享等缺点,限制了Python在数据可视化方面的发展。   为了解决这个问题,新型的动态可视化开源模块Plotly应运而生。由于Plotly具有动态、美观、易用、种类丰富等特性,所以一经问世就受到开发人员的喜爱。

YFater的博客 1万+

使用mybatis-generator添加自定义插件时提示无法实例化插件类 Cannot instantiate object of type

报错信息: Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.6:generate failed: Cannot instantiate object of type com.boot.sell.configs.mybatisGenerator.MyGeneratorPl...

想静静的搬运工的博客 1万+

Altium Designer操作技巧(1)——原理图中如何批量替换元器件?

《AltiumDesigner批量替换元器件技巧分享》摘要:本文介绍了两种在AltiumDesigner中批量替换元器件的实用方法。方法一通过“查找相似对象”功能,可精准匹配并批量修改器件参数;方法二利用ActiveBOM功能,在BOM表中直观操作器件替换,推荐使用第二种方法可使BOM管理更高效。这些技巧能有效减少物料种类、降低贴装成本,特别适用于电阻电容等常用器件的批量替换。关注公众号获取更多电子设计技巧。

u012526819的博客 2593

π_RL:解锁流匹配VLA强化学习后训练的关键技术与实战解析

本文深入解析了π_RL框架如何通过流匹配(flow matching)与强化学习(RL)的结合,解决视觉语言动作模型(VLA)在后训练中的关键挑战。详细介绍了Flow Noise和Flow SDE两大核心技术,以及它们在机器人控制任务中的实战效果,显著提升了模型的性能、数据效率和泛化能力。

weixin_29061531的博客 222

Caused by: java.lang.ClassNotFoundException: org.mybatis.generator.api.dom.xml.Element

报错信息 Caused by: java.lang.NoClassDefFoundError: org/mybatis/generator/api/dom/xml/Element at java.lang.Class.forName0 (Native Method) at java.lang.Class.forName (Class.java:348) at org.myb...

Leisurelyc的博客 8588

YOLOv11改进策略【卷积层】| CVPR-2023 部分卷积 PConv 轻量化卷积,降低内存占用

本文记录的是利用优化的目标检测方法研究。深度可分离卷积可以减少FLOPs,但会导致更高的内存访问,引起延迟并减慢整体计算。部分卷积利用逐点卷积处理通道冗余,减少模型计算量和内存访问量。Run, Don’t Walk: Chasing Higher FLOPS for Faster Neural Networks的实现代码如下: 四、创新模块 4.1 改进点⭐ 模块改进方法: 1️⃣ 加入。添加后如下:2️⃣:加入基于的。利用改进模块,减少通道冗余。改进代码如下:首先添加如下代码让加入,并重命名为 再添加如

Limiiiing的博客 3878

关于mybatis-generator一个报错问题--Cannot instantiate object of type tk.mybatis.mapper.generator.MapperPlugi

今天在用项目使用mybatis-generator的时候报了一个错 Cannot instantiate object of type tk.mybatis.mapper.generator.MapperPlugin 声明为无法实例化generator 搜了很久不知道是什么问题 最后感觉应该是pom文件配置问题 最后重置了我的pom配置 如下: <plugins> <pl...

Stay foolish Stay hungry 6376

【Python深度学习】做深度学习训练时,如果数据集太大,无法一次性加载进GPU,应该如何做?

方法核心思想优点缺点适用场景实现复杂度小批量训练(Mini-Batch Training)与数据加载器(DataLoader)将数据集划分为小批量,每次只加载一个小批量到GPU中进行训练。- 易于实现- 是深度学习的标准做法- 支持多进程加载- 内存可能成为瓶颈- 需要调整批量大小避免内存不足适用于大部分场景低数据分片与分布式训练(Data Sharding & Distributed Training)将数据集分片,每个GPU或节点处理其中一部分数据,分布式训练。

若北辰 1143

硬件测试有哪些?

硬件单元测试(UT)是验证电路功能与可靠性的关键环节,主要分为电源测试和信号测试两大模块。电源测试需验证输入电压范围、高低压极限、短路保护等功能,确保供电稳定性;信号测试则聚焦时钟、总线、存储等接口的时序与完整性。测试要求统一标准、可追溯数据、问题闭环处理,通过开发与测试协同确保质量。规范的单元测试能有效降低后期集成风险,提升产品可靠性,其核心始终是在最小单元上确保稳健性,为系统级可靠性奠定基础。

硬核科技 245

【Mybatis】Mybatis-generator插件Cannot instantiate object of type...

使用mybatis-generator插件自动生成代码,遇到了这么一个问题,当使用自定义的类型转换类时,启动插件始终实例化不了相应的转换类,报错如下: [ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.5:generate (default-cli) on project...

暮雨亦成詩 7906

PCL——KITTI数据集.bin文件转.pcd并可视化【2025最新版】

kitti数据集中的bin格式点云主要存储的字段为:`x`、`y`、`z`、`intensity`,对应于PCL中的PointXYZI。因此,只需将bin中的属性信息读取出来放入`PointXYZI`即可。本文给出简单易用的实现代码。博客长期更新,本文最近更新时间为:2025年1月11日。

点云侠的博客 3958

关于使用MyBatis-Generator时自定义注解生成器的一些问题

使用MyBatis-Generator和通用Mapper整合时自定义注解生成器注意的问题

me_ng的个人博客 6821

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

MyBatis Generator自动添加注解方法

首先,创建一个自定义插件类,继承类,并重写方法。这个方法会在生成实体类时被调用。import org// 添加注解 topLevelClass . addAnnotation("@lombok.Data");} }// 添加注解 topLevelClass . addAnnotation("@lombok.Data");} }// 添加注解 topLevelClass . addAnnotation("@lombok.Data");} }

命中的缘分的博客 893

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

mybatis-generator-plugin 插件开发指南:从零开始打造自定义生成器

mybatis-generator-plugin 是一款强大的 MyBatis Generator 代码生成插件拓展工具,它提供了丰富的插件功能,如查询单条数据插件、MySQL分页插件、数据Model链式构建插件等。本文将带领你从零开始,学习如何开发属于自己的自定义 MyBatis 生成器插件,轻松扩展代码生成功能。 ## 插件开发准备工作 ### 环境搭建 在开始开发之前,确保你的开发环境

gitblog_00434的博客 993

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

通过自定义插件实现Mybatis Generator 生成自定义SQL

在写自己的开发框架时,需要用到特殊的sql语句,使用默认的generatorConfig.xml文件配置,无法实现,我们可以自己定义一个generator插件 一、先新建一个项目,作为插件项目 首先引入依赖: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-inst

weixin_41547362的博客 1500

mybatis-generator-gui-extension 缓存插件配置教程:一行配置为 MyBatis 查询加上二级缓存

如果你正在使用 **mybatis-generator-gui-extension** 这款图形化 MBG 工具,那么今天这篇 **MyBatis 二级缓存配置教程** 能帮你省下大量手写 XML 的时间——只需勾选一个开关、填写几个参数,就能为生成的 Mapper 查询自动加上 `<cache>` 标签,让高频查询直接命中二级缓存,系统响应速度肉眼可见地提升。整个配置过程不用写一行 Java 代

gitblog_00744的博客 1086

基于注解的Mybatis Generator插件

用Mybatis Generator生成基于注解的mapper接口,可自定义方法.

Eric的专栏 2530

自定义mybatis-generator插件

由于mybatis-generator原生插件太过死板,不够灵活,生成的文件往往需要修改为需要格式,所以,自己就动手造轮子,自己写了个mybatis-generator插件,点此下载, 可以多多交流...

zhangpan_soft的博客 1万+

mybatis-generator插件的用法介绍

mybatis-generator是一款在使用mybatis框架时,根据我们的数据库表,自动生成对应model,dao和mapper的工具,很大程度上减少了业务开发人员的手动编码时间,接下来我们来了解它的用法。 文章目录在pom.xml中添加插件配置generatorConfig.xml文件使用命令执行插件 在pom.xml中添加插件 <plugins> <plugin>...

lxcoding 649

mybatis-generator自定义注释实现

(一)、项目结构 (二)、引入Maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schema...

一叶知秋hwb的博客 374
上一篇: springMVC上传文件
下一篇: 如何在IDEA中导入jar包?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值