• mybatis generator自定义文件后缀名


    现状及需求

    使用mybatis generator自动生成文件,默认名都是XXXExample.java,XXXMapper.java,XXXMapper.xml,待过几个公司或者项目组的小伙伴就有感受,有的组要求是XXXDao.java,XXXCondition.java,XXXDao.xml,后来就自己调研能不能自定义文件后缀名,总不能每次自动生成好,再手动修改吧

    解决办法

    mybatis-generator-core-1.3.5.jar自带可以修改Example的文件名,generatorConfig.xml中添加如下配置项就ok了

    <!--修改Example文件名-->
      <plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin">   
                <property name="searchString" value="Example$"/>  
                <property name="replaceString" value="Condition"/>  
       </plugin>
    

    但是修改Mapper的后缀名却没有提供,这样的话就只能修改mybatis generator的源码,参照RenameExampleClassPlugin类,添加个RenameJavaMapperPlugin,RenameXmlMapperPlugin,然后自己生成个jar吧,姑且叫mybatis-generator-core-1.3.7.jar,

    配置文件中继续添加如下配置:

    <!--修改Mapper文件名-->
      <plugin type="org.mybatis.generator.plugins.RenameJavaMapperPlugin">
             <property name="searchString" value="Mapper$" />
             <property name="replaceString" value="Dao" />
            </plugin>
      <!--修改xml文件名-->
      <plugin type="org.mybatis.generator.plugins.RenameSqlMapperPlugin">
             <property name="searchString" value="Mapper" />
             <property name="replaceString" value="Dao" />
            </plugin>
    

     运行命令为:java -jar mybatis-generator-core-1.3.7.jar -configfile generatorConfig.xml -overwrite

    PS:

    1.mybatis generator的github源码地址为:https://github.com/mybatis/generator

    2.RenameJavaMapperPlugin文件源码为:

    /**
     *    Copyright ${license.git.copyrightYears} the original author or authors.
     *
     *    Licensed under the Apache License, Version 2.0 (the "License");
     *    you may not use this file except in compliance with the License.
     *    You may obtain a copy of the License at
     *
     *       http://www.apache.org/licenses/LICENSE-2.0
     *
     *    Unless required by applicable law or agreed to in writing, software
     *    distributed under the License is distributed on an "AS IS" BASIS,
     *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     *    See the License for the specific language governing permissions and
     *    limitations under the License.
     */
    package org.mybatis.generator.plugins;
    
    import org.mybatis.generator.api.IntrospectedTable;
    import org.mybatis.generator.api.PluginAdapter;
    
    import java.util.List;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
    import static org.mybatis.generator.internal.util.messages.Messages.getString;
    
    /**
     * This plugin demonstrates overriding the initialized() method to rename the
     * generated example classes. Instead of xxxExample, the classes will be named
     * xxxCriteria.
     * 
     * <p>This plugin accepts two properties:
     * 
     * <ul>
     * <li><tt>searchString</tt> (required) the regular expression of the name
     * search.</li>
     * <li><tt>replaceString</tt> (required) the replacement String.</li>
     * </ul>
     * 
     * <p>For example, to change the name of the generated Example classes from
     * xxxExample to xxxCriteria, specify the following:
     * 
     * <dl>
     * <dt>searchString</dt>
     * <dd>Mapper$</dd>
     * <dt>replaceString</dt>
     * <dd>Criteria</dd>
     * </dl>
     * 
     * 
     * @author Jeff Butler
     * 
     */
    public class RenameJavaMapperPlugin extends PluginAdapter {
        private String searchString;
        private String replaceString;
        private Pattern pattern;
    
        public RenameJavaMapperPlugin() {
        }
    
        @Override
        public boolean validate(List<String> warnings) {
    
            searchString = properties.getProperty("searchString"); //$NON-NLS-1$
            replaceString = properties.getProperty("replaceString"); //$NON-NLS-1$
    
            boolean valid = stringHasValue(searchString)
                    && stringHasValue(replaceString);
    
            if (valid) {
                pattern = Pattern.compile(searchString);
            } else {
                if (!stringHasValue(searchString)) {
                    warnings.add(getString("ValidationError.18", //$NON-NLS-1$
                            "RenameExampleClassPlugin", //$NON-NLS-1$
                            "searchString")); //$NON-NLS-1$
                }
                if (!stringHasValue(replaceString)) {
                    warnings.add(getString("ValidationError.18", //$NON-NLS-1$
                            "RenameExampleClassPlugin", //$NON-NLS-1$
                            "replaceString")); //$NON-NLS-1$
                }
            }
    
            return valid;
        }
    
    
        @Override
        public void initialized(IntrospectedTable introspectedTable) {
            String oldType = introspectedTable.getMyBatis3JavaMapperType();
            Matcher matcher = pattern.matcher(oldType);
            oldType = matcher.replaceAll(replaceString);
    
            introspectedTable.setMyBatis3JavaMapperType(oldType);
        }
    }

    3.RenameXmlMapperPlugin文件源码为:

    /**
     *    Copyright ${license.git.copyrightYears} the original author or authors.
     *
     *    Licensed under the Apache License, Version 2.0 (the "License");
     *    you may not use this file except in compliance with the License.
     *    You may obtain a copy of the License at
     *
     *       http://www.apache.org/licenses/LICENSE-2.0
     *
     *    Unless required by applicable law or agreed to in writing, software
     *    distributed under the License is distributed on an "AS IS" BASIS,
     *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     *    See the License for the specific language governing permissions and
     *    limitations under the License.
     */
    package org.mybatis.generator.plugins;
    
    
    import org.mybatis.generator.api.IntrospectedTable;
    import org.mybatis.generator.api.PluginAdapter;
    
    import java.util.List;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
    import static org.mybatis.generator.internal.util.messages.Messages.getString;
    
    /**
     * This plugin demonstrates overriding the initialized() method to rename the
     * generated example classes. Instead of xxxExample, the classes will be named
     * xxxCriteria
     * 
     * This plugin accepts two properties:
     * <ul>
     * <li><tt>searchString</tt> (required) the regular expression of the name
     * search.</li>
     * <li><tt>replaceString</tt> (required) the replacement String.</li>
     * </ul>
     * 
     * For example, to change the name of the generated Example classes from
     * xxxExample to xxxCriteria, specify the following:
     * 
     * <dl>
     * <dt>searchString</dt>
     * <dd>Mapper$</dd>
     * <dt>replaceString</dt>
     * <dd>Criteria</dd>
     * </dl>
     * 
     * 
     * @author Jeff Butler
     * 
     */
    public class RenameXmlMapperPlugin extends PluginAdapter {
        private String searchString;
        private String replaceString;
        private Pattern pattern;
    
    
        public RenameXmlMapperPlugin() {
        }
    
        @Override
        public boolean validate(List<String> warnings) {
    
            searchString = properties.getProperty("searchString"); //$NON-NLS-1$
            replaceString = properties.getProperty("replaceString"); //$NON-NLS-1$
    
            boolean valid = stringHasValue(searchString)
                    && stringHasValue(replaceString);
    
            if (valid) {
                pattern = Pattern.compile(searchString);
            } else {
                if (!stringHasValue(searchString)) {
                    warnings.add(getString("ValidationError.18", //$NON-NLS-1$
                            "RenameExampleClassPlugin", //$NON-NLS-1$
                            "searchString")); //$NON-NLS-1$
                }
                if (!stringHasValue(replaceString)) {
                    warnings.add(getString("ValidationError.18", //$NON-NLS-1$
                            "RenameExampleClassPlugin", //$NON-NLS-1$
                            "replaceString")); //$NON-NLS-1$
                }
            }
    
            return valid;
        }
    
        @Override
        public void initialized(IntrospectedTable introspectedTable) {
            String oldType = introspectedTable.getMyBatis3XmlMapperFileName();
            Matcher matcher = pattern.matcher(oldType);
            oldType = matcher.replaceAll(replaceString);
            
            introspectedTable.setMyBatis3XmlMapperFileName(oldType);
        }
    }

    4.如果还有什么需求,可以继续修改源码

  • 相关阅读:
    嵌入式linux驱动开发之点亮led(驱动编程思想之初体验)
    嵌入式Linux驱动开发之helloword心得
    PJMEDIA之录音器的使用(capture sound to avi file)
    PJMEID学习之视频的捕捉与播放
    PJSIP-PJMEDIA【使用pjmedia 播放wav格式的音乐】
    PJSIP-PJLIB-Socket
    PJSIP-PJLIB(samples) (the usage of the pjlib lib) (eg:string/I/O)
    Start with PJSIP on windows
    Android实际开发之网络请求组件的封装(OkHttp为核心)
    Android实际开发中的首页框架搭建(二、首页框架实现)
  • 原文地址:https://www.cnblogs.com/muzhao/p/8447956.html
Copyright © 2020-2023  润新知