本文主要是介绍自定义修改 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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!