博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
定制自己的mybatis生成
阅读量:6844 次
发布时间:2019-06-26

本文共 10177 字,大约阅读时间需要 33 分钟。

  MyBatis Generator原生提供的生成方式targetRuntime有几种,但都不符合项目需求或想自定义自己的方法。   网上的文章也很多:      如:      这里我说下我的做法:   1、继承IntrospectedTableMyBatis3Impl,重写自己要改写的方法     InsoIntrospectedTable.java
    重写calculateXmlMapperGenerator使用自己的XMLMapperGenerator
    重写createJavaClientGenerator使用自己的JavaMapperGenerator     我的做法比较粗暴,就是注释掉原来的逻辑,自己new自己的替代原来的。
public class InsoIntrospectedTable extends IntrospectedTableMyBatis3Impl {    protected void calculateXmlMapperGenerator(AbstractJavaClientGenerator javaClientGenerator,                                               List
warnings, ProgressCallback progressCallback) {// if (javaClientGenerator == null) {// if (context.getSqlMapGeneratorConfiguration() != null) {// xmlMapperGenerator = new XMLMapperGenerator();// }// } else {// xmlMapperGenerator = javaClientGenerator.getMatchedXMLGenerator();// } xmlMapperGenerator = new InsoXMLMapperGenerator(); initializeAbstractGenerator(xmlMapperGenerator, warnings, progressCallback); } protected AbstractJavaClientGenerator createJavaClientGenerator() { if (context.getJavaClientGeneratorConfiguration() == null) { return null; }// String type = context.getJavaClientGeneratorConfiguration()// .getConfigurationType(); AbstractJavaClientGenerator javaGenerator;// if ("XMLMAPPER".equalsIgnoreCase(type)) { //$NON-NLS-1$// javaGenerator = new JavaMapperGenerator();// } else if ("MIXEDMAPPER".equalsIgnoreCase(type)) { //$NON-NLS-1$// javaGenerator = new MixedClientGenerator();// } else if ("ANNOTATEDMAPPER".equalsIgnoreCase(type)) { //$NON-NLS-1$// javaGenerator = new AnnotatedClientGenerator();// } else if ("MAPPER".equalsIgnoreCase(type)) { //$NON-NLS-1$// javaGenerator = new JavaMapperGenerator();// } else {// javaGenerator = (AbstractJavaClientGenerator) ObjectFactory// .createInternalObject(type);// } javaGenerator = new InsoJavaMapperGenerator(); return javaGenerator; }}

  2、继承XMLMapperGenerator,重写自己要改写的方法

  InsoXMLMapperGenerator.java在getSqlMapElement方法中,可以添加或删除自己要的方法,这里我只添加了一个selectAll方法

public class InsoXMLMapperGenerator extends XMLMapperGenerator {    public InsoXMLMapperGenerator() {        super();    }    protected XmlElement getSqlMapElement() {        FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();        progressCallback.startTask(getString(                "Progress.12", table.toString())); //$NON-NLS-1$        XmlElement answer = new XmlElement("mapper"); //$NON-NLS-1$        String namespace = introspectedTable.getMyBatis3SqlMapNamespace();        answer.addAttribute(new Attribute("namespace", //$NON-NLS-1$                namespace));        context.getCommentGenerator().addRootComment(answer);        addResultMapWithoutBLOBsElement(answer);        addResultMapWithBLOBsElement(answer);        addExampleWhereClauseElement(answer);        addMyBatis3UpdateByExampleWhereClauseElement(answer);        addBaseColumnListElement(answer);        addBlobColumnListElement(answer);        addSelectByExampleWithBLOBsElement(answer);        addSelectByExampleWithoutBLOBsElement(answer);        addSelectByPrimaryKeyElement(answer);        addDeleteByPrimaryKeyElement(answer);        addDeleteByExampleElement(answer);        addInsertElement(answer);        addInsertSelectiveElement(answer);        addCountByExampleElement(answer);        addUpdateByExampleSelectiveElement(answer);        addUpdateByExampleWithBLOBsElement(answer);        addUpdateByExampleWithoutBLOBsElement(answer);        addUpdateByPrimaryKeySelectiveElement(answer);        addUpdateByPrimaryKeyWithBLOBsElement(answer);        addUpdateByPrimaryKeyWithoutBLOBsElement(answer);        //add select all        addSimpleSelectAllElement(answer);        return answer;    }    protected void addSimpleSelectAllElement(            XmlElement parentElement) {        if (introspectedTable.getRules()                .generateSelectByPrimaryKey()) {            AbstractXmlElementGenerator elementGenerator = new InsoSelectAllElementGenerator();            initializeAndExecuteGenerator(elementGenerator, parentElement);        }    }}
  3、继承JavaMapperGenerator,重写自己要改写的方法
  InsoJavaMapperGenerator.java在getCompilationUnits方法中定制自己要的方法,这里我只添加了一个selectAll方法。   注意:这里的项要与上面的XMLMapperGenerator一一对应,其它情况,我没有研究(比较菜。。。)
public class InsoJavaMapperGenerator extends JavaMapperGenerator {    @Override    public List
getCompilationUnits() { progressCallback.startTask(getString("Progress.17", //$NON-NLS-1$ introspectedTable.getFullyQualifiedTable().toString())); CommentGenerator commentGenerator = context.getCommentGenerator(); FullyQualifiedJavaType type = new FullyQualifiedJavaType( introspectedTable.getMyBatis3JavaMapperType()); Interface interfaze = new Interface(type); interfaze.setVisibility(JavaVisibility.PUBLIC); commentGenerator.addJavaFileComment(interfaze); String rootInterface = introspectedTable .getTableConfigurationProperty(PropertyRegistry.ANY_ROOT_INTERFACE); if (!stringHasValue(rootInterface)) { rootInterface = context.getJavaClientGeneratorConfiguration() .getProperty(PropertyRegistry.ANY_ROOT_INTERFACE); } if (stringHasValue(rootInterface)) { FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType( rootInterface); interfaze.addSuperInterface(fqjt); interfaze.addImportedType(fqjt); } addCountByExampleMethod(interfaze); addDeleteByExampleMethod(interfaze); addDeleteByPrimaryKeyMethod(interfaze); addInsertMethod(interfaze); addInsertSelectiveMethod(interfaze); addSelectByExampleWithBLOBsMethod(interfaze); addSelectByExampleWithoutBLOBsMethod(interfaze); addSelectByPrimaryKeyMethod(interfaze); addUpdateByExampleSelectiveMethod(interfaze); addUpdateByExampleWithBLOBsMethod(interfaze); addUpdateByExampleWithoutBLOBsMethod(interfaze); addUpdateByPrimaryKeySelectiveMethod(interfaze); addUpdateByPrimaryKeyWithBLOBsMethod(interfaze); addUpdateByPrimaryKeyWithoutBLOBsMethod(interfaze); //增加selectAll addSelectAllMethod(interfaze); List
answer = new ArrayList
(); if (context.getPlugins().clientGenerated(interfaze, null, introspectedTable)) { answer.add(interfaze); } List
extraCompilationUnits = getExtraCompilationUnits(); if (extraCompilationUnits != null) { answer.addAll(extraCompilationUnits); } return answer; } /** * 增加eelectAll * @param interfaze */ protected void addSelectAllMethod(Interface interfaze) { if (introspectedTable.getRules() .generateSelectByPrimaryKey()) { AbstractJavaMapperMethodGenerator methodGenerator = new SelectAllMethodGenerator(); initializeAndExecuteGenerator(methodGenerator, interfaze); } }

  4、配置XML使用上面自定义targetRuntime

   inso-generator\src\main\resources\generatorConfig-sys.xml

  • targetRuntime中配置全路径类名
  • 在table节点,可以配置一些开关,是否生成某些方法,如下,我关掉生成ByExample的方法
  • 在table节点里,属性ignoreQualifiersAtRuntime,默认为false,如果设置为true,在生成的SQL中,table名字不会加上catalog或schema,此配置,在Oracle下好用。

 

 

 

 

  5、pom文件参考

  项目原型是ibase4j(http://git.oschina.net/iBase4J/iBase4J),此pom仅供参考一下吧  

 

4.0.0
inso-generator
inso-generator
http://maven.apache.org
jar
com.xxcomp
inso
0.5.0
${project.name}
src/main/java
org.apache.maven.plugins
maven-compiler-plugin
3.3
1.7
1.7
UTF-8
none
org.apache.maven.plugins
maven-jdeps-plugin
3.0.0
jdkinternals
test-jdkinternals
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
true
true
src/main/resources/${configurationFile}
com.oracle
ojdbc6
11.2.0
com.xxcomp
inso-generator
${project.version}
Generate MyBatis Artifacts
generate

 

 

 

 

 

 

  通过以上的改造,可以简单自定义组装一些原有的方法,生成自己需要的sqlMap,进一步还可以自己编写具体的方法实现,生成真正自定义的sql。(此步,我没有去做,太懒了。。)      从没写过文章,真不会写,若看不明白或对你没帮忙,大侠请自动忽略。   写一写才发觉,写这个有点费时间,虽然我只是贴一下。   2016-09-27 10:31:24

转载于:https://www.cnblogs.com/kstrive/p/5911873.html

你可能感兴趣的文章