• Mybatis Generator by Java Api


    通常我们使用Mybatis Generator 来逆向生成Dao等代码,是通过Generator.xml配置文件来实现的。网络上有很多描述,在此不再赘述。

    我们今天来说的是,如何通过Java代码形式来替代Generator.xml的方式,也就是去XML,实现代码生成。

    下面就是我偶然在简书上找到的,帮了我大忙,附上原作者地址:https://www.jianshu.com/p/e9480ee6a9cc

    关键字:

    Mybatis Generator(MBG) -- Java 配置替代xml文件

    package com.snowy.fastgov.generator;
    
    import org.mybatis.generator.api.MyBatisGenerator;
    import org.mybatis.generator.config.*;
    import org.mybatis.generator.exception.InvalidConfigurationException;
    import org.mybatis.generator.internal.DefaultShellCallback;
    
    import java.io.IOException;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
    
    public class GeneratorConfig {
    
    
        private String driverClass = "com.mysql.cj.jdbc.Driver";
    
        private String connectionURL = "jdbc:mysql://localhost:3306/any?useUnicode=true&characterEncoding=utf8";
    
        private String userId = "root";
    
        private String password = "root";
    
    
        private String targetPackage = "com.snowy.fastgov.common.entity";
    
        private String targetProject = "src/main/java";
    
    
        private String sqlTargetPackage = "mapping";
    
        private String sqlTargetProject = "src/main/resources";
    
    
        private String daoTargetPackage = "com.snowy.fastgov.common.mapper";
    
        private String daoTargetProject = "src/main/java";
    
        private String classpathEntry="E:\IdeaProject\fastgov\src\main\resources\webapp\lib\mysql-connector-java-5.1.43.jar";
    
    
        public static void main(String[] args){
            GeneratorConfig generatorConfig = new GeneratorConfig();
            TableEntity tableEntity = new TableEntity();
            tableEntity.setTableName("good");
            tableEntity.setDomainObjectName("Good");
            List<TableEntity> list = new ArrayList<>();
            list.add(tableEntity);
            generatorConfig.generatorConfig(list);
        }
    
        public void generatorConfig(List<TableEntity> tableList) {
    
            List<String> warnings = new ArrayList<String>();
            boolean overwrite = true;
            Context context = new Context(ModelType.CONDITIONAL);
            context.setTargetRuntime("MyBatis3");
            context.setId("mysql");
            
    
            CommentGeneratorConfiguration commentGeneratorConfiguration = new CommentGeneratorConfiguration();
            commentGeneratorConfiguration.addProperty("suppressDate","true");  // if you want to add the comments, change to false
            commentGeneratorConfiguration.addProperty("suppressAllComments","true");  // same as above
    
            /*数据库链接URL,用户名、密码 */
            JDBCConnectionConfiguration jdbcConnectionConfiguration = new JDBCConnectionConfiguration();
            jdbcConnectionConfiguration.setDriverClass(driverClass);
            jdbcConnectionConfiguration.setConnectionURL(connectionURL);
            jdbcConnectionConfiguration.setUserId(userId);
            jdbcConnectionConfiguration.setPassword(password);
    
            JavaTypeResolverConfiguration javaTypeResolverConfiguration = new JavaTypeResolverConfiguration();
            javaTypeResolverConfiguration.addProperty("forceBigDecimals","false");
    
            /*生成模型的包名和位置*/
            JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = new JavaModelGeneratorConfiguration();
            javaModelGeneratorConfiguration.setTargetPackage(targetPackage);
            javaModelGeneratorConfiguration.setTargetProject(targetProject);
            javaModelGeneratorConfiguration.addProperty("enableSubPackages","true");
            javaModelGeneratorConfiguration.addProperty("trimStrings","true");
    
            /*生成映射文件的包名和位置*/
            SqlMapGeneratorConfiguration sqlMapGeneratorConfiguration = new SqlMapGeneratorConfiguration();
            sqlMapGeneratorConfiguration.setTargetPackage(sqlTargetPackage);
            sqlMapGeneratorConfiguration.setTargetProject(sqlTargetProject);
            sqlMapGeneratorConfiguration.addProperty("enableSubPackages","true");
    
            /*生成DAO的包名和位置*/
            JavaClientGeneratorConfiguration javaClientGeneratorConfiguration = new JavaClientGeneratorConfiguration();
            javaClientGeneratorConfiguration.setConfigurationType("XMLMAPPER");
            javaClientGeneratorConfiguration.setTargetPackage(daoTargetPackage);
            javaClientGeneratorConfiguration.setTargetProject(daoTargetProject);
            javaClientGeneratorConfiguration.addProperty("enableSubPackages","true");
    
    
            context.setCommentGeneratorConfiguration(commentGeneratorConfiguration);
            context.setJdbcConnectionConfiguration(jdbcConnectionConfiguration);
            context.setJavaTypeResolverConfiguration(javaTypeResolverConfiguration);
            context.setJavaModelGeneratorConfiguration(javaModelGeneratorConfiguration);
            context.setSqlMapGeneratorConfiguration(sqlMapGeneratorConfiguration);
            context.setJavaClientGeneratorConfiguration(javaClientGeneratorConfiguration);
            
    
            tableList.stream().forEach(tableEntity->{
                TableConfiguration tableConfiguration = new TableConfiguration(context);
                tableConfiguration.setTableName(tableEntity.getTableName());
                tableConfiguration.setDomainObjectName(tableEntity.getDomainObjectName());
           // If you want to remove the Example, do this
           tableConfiguration.setUpdateByExampleStatementEnabled(false);
           tableConfiguration.setCountByExampleStatementEnabled(false);
           tableConfiguration.setDeleteByExampleStatementEnabled(false);
           tableConfiguration.setSelectByExampleStatementEnabled(false);
           tableConfiguration.setSelectByExampleQueryId(false);
           
           // Sometiimes, your table' name like 't_teacher', how to make it to be a Camel style?
           tableConfiguration.addProperty("useActualColumnNames", "false");
    context.addTableConfiguration(tableConfiguration); }); Configuration config
    = new Configuration(); config.addClasspathEntry(classpathEntry); config.addContext(context); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = null; try { myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); } catch (InvalidConfigurationException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } }

    TableEntity实体类

    package com.snowy.fastgov.generator;
    
    import lombok.Data;
    
    @Data
    public class TableEntity {
        private String tableName;
        private String domainObjectName;
    }
  • 相关阅读:
    struts框架的基本使用
    软件体系结构的艺术阅读笔记3
    软件架构设计阅读笔记3
    TensorFlow实现线性回归算法
    python使用pycharts调用国家地图实现数据可视化
    pip出现WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.,已解决
    tensorflow使用Session模块时报错:AttributeError: module 'tensorflow' has no attribute 'Session',已解决
    软件架构设计阅读笔记2
    软件体系结构的艺术阅读笔记2
    python快速求一个数组的最大值/最小值及其索引
  • 原文地址:https://www.cnblogs.com/Night-Watch/p/12674924.html
Copyright © 2020-2023  润新知