自定义修改 MyBatis Generator 方法名后缀 Primary Key 为 Id

2024-09-01 21:08

本文主要是介绍自定义修改 MyBatis Generator 方法名后缀 Primary Key 为 Id,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

自定义修改 MyBatis Generator 方法名后缀 Primary Key 为 Id

原理

MyBatis Generator 先生成自身自带内容,再调用我们指定的 plugin,最后才将内容刷出到磁盘文件;在其调用 plugin 时会将上下文(包括 Java 方法、Xml 节点)传递过来,此时可以对其已有内容进行定制修改

第一步

在 generatorConfig.xml 文件 context 节点添加 plugin 配置

<generatorConfiguration><context id="simple" targetRuntime="MyBatis3Simple"><plugin type="zhong.mybatis.generator.plugin.MyRenamePkToIdPlugin" /></context>
</generatorConfiguration>

第二步

新建类 zhong.mybatis.generator.plugin.MyRenamePkToIdPlugin

package zhong.mybatis.generator.plugin;import java.lang.reflect.Field;
import java.util.List;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.Method;
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.Element;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.codegen.ibatis2.sqlmap.elements.AbstractXmlElementGenerator;
import org.mybatis.generator.codegen.mybatis3.javamapper.elements.AbstractJavaMapperMethodGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ReflectionUtils;/*** 重命名 Mapper.java 和 Mapper.xml 文件方法名带 PrimaryKey 的改为 ID。<br>* 如:selectByPrimaryKey 改为 selectById** @author Zhong*/
public class MyRenamePkToIdPlugin extends PluginAdapter {private static final Logger LOGGER = LoggerFactory.getLogger(MyRenamePkToIdPlugin.class);private static final String PK = "PrimaryKey";private static final String ID = "Id";@Overridepublic boolean validate(List<String> warnings) {return true;}@Overridepublic boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {AbstractXmlElementGenerator plugin = new AbstractXmlElementGenerator() {@Overridepublic void addElements(XmlElement parentElement) {Field f = null;try {f = Attribute.class.getDeclaredField("value");ReflectionUtils.makeAccessible(f);LOGGER.debug(Attribute.class.getName() + " exists declared field 'value'");} catch (NoSuchFieldException e) {throw new IllegalStateException(Attribute.class.getName() + " not exists declared field 'value'");}for (Element e : parentElement.getElements()) {if (e instanceof XmlElement) {XmlElement x = (XmlElement) e;if (x.getName().equals("insert")|| x.getName().equals("delete")|| x.getName().equals("update")|| x.getName().equals("select")) {for (Attribute a : x.getAttributes()) {if (a.getName().equals("id") && a.getValue().endsWith(PK)) {ReflectionUtils.setField(f, a, renamePkToId(a.getValue()));}}}}}}};plugin.setContext(context);plugin.setIntrospectedTable(introspectedTable);plugin.addElements(document.getRootElement());return super.sqlMapDocumentGenerated(document, introspectedTable);}@Overridepublic boolean clientGenerated(Interface theInterface, TopLevelClass topLevelClass,IntrospectedTable introspectedTable) {AbstractJavaMapperMethodGenerator plugin = new AbstractJavaMapperMethodGenerator() {@Overridepublic void addInterfaceElements(Interface interfaze) {for (Method e : interfaze.getMethods()) {if (e.getName().endsWith(PK)) {e.setName(renamePkToId(e.getName()));}}}};plugin.setContext(context);plugin.setIntrospectedTable(introspectedTable);plugin.addInterfaceElements(theInterface);return super.clientGenerated(theInterface, topLevelClass, introspectedTable);}private static String renamePkToId(String name) {if (name.endsWith(PK)) {name = name.substring(0, name.length() - PK.length()) + ID;}return name;}
}

这篇关于自定义修改 MyBatis Generator 方法名后缀 Primary Key 为 Id的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1128048

相关文章

MyBatis-Plus通用中等、大量数据分批查询和处理方法

《MyBatis-Plus通用中等、大量数据分批查询和处理方法》文章介绍MyBatis-Plus分页查询处理,通过函数式接口与Lambda表达式实现通用逻辑,方法抽象但功能强大,建议扩展分批处理及流式... 目录函数式接口获取分页数据接口数据处理接口通用逻辑工具类使用方法简单查询自定义查询方法总结函数式接口

MySQL深分页进行性能优化的常见方法

《MySQL深分页进行性能优化的常见方法》在Web应用中,分页查询是数据库操作中的常见需求,然而,在面对大型数据集时,深分页(deeppagination)却成为了性能优化的一个挑战,在本文中,我们将... 目录引言:深分页,真的只是“翻页慢”那么简单吗?一、背景介绍二、深分页的性能问题三、业务场景分析四、

JAVA中安装多个JDK的方法

《JAVA中安装多个JDK的方法》文章介绍了在Windows系统上安装多个JDK版本的方法,包括下载、安装路径修改、环境变量配置(JAVA_HOME和Path),并说明如何通过调整JAVA_HOME在... 首先去oracle官网下载好两个版本不同的jdk(需要登录Oracle账号,没有可以免费注册)下载完

MyBatis中$与#的区别解析

《MyBatis中$与#的区别解析》文章浏览阅读314次,点赞4次,收藏6次。MyBatis使用#{}作为参数占位符时,会创建预处理语句(PreparedStatement),并将参数值作为预处理语句... 目录一、介绍二、sql注入风险实例一、介绍#(井号):MyBATis使用#{}作为参数占位符时,会

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu

Java 方法重载Overload常见误区及注意事项

《Java方法重载Overload常见误区及注意事项》Java方法重载允许同一类中同名方法通过参数类型、数量、顺序差异实现功能扩展,提升代码灵活性,核心条件为参数列表不同,不涉及返回类型、访问修饰符... 目录Java 方法重载(Overload)详解一、方法重载的核心条件二、构成方法重载的具体情况三、不构

SQL中如何添加数据(常见方法及示例)

《SQL中如何添加数据(常见方法及示例)》SQL全称为StructuredQueryLanguage,是一种用于管理关系数据库的标准编程语言,下面给大家介绍SQL中如何添加数据,感兴趣的朋友一起看看吧... 目录在mysql中,有多种方法可以添加数据。以下是一些常见的方法及其示例。1. 使用INSERT I

Python中反转字符串的常见方法小结

《Python中反转字符串的常见方法小结》在Python中,字符串对象没有内置的反转方法,然而,在实际开发中,我们经常会遇到需要反转字符串的场景,比如处理回文字符串、文本加密等,因此,掌握如何在Pyt... 目录python中反转字符串的方法技术背景实现步骤1. 使用切片2. 使用 reversed() 函