美文网首页程序员
Mybatis Generator(MBG) -- Java 配

Mybatis Generator(MBG) -- Java 配

作者: 小炉炉 | 来源:发表于2019-01-15 16:10 被阅读87次

    Mybatis Generator 是mybatis 逆向插件,可以通过数据库的表逆向生成pojo类,以及dao层和相应的mapper.xml文件。十分的方便,能很有效的减少代码书写量。不过一般都是使用配置xml的方式,通过maven的方式来运行的。具体请参看我上一篇文章:https://www.jianshu.com/p/b4609ca6ca33

    我也是找了好久才再mybatis的官网上找到了有关纯java类执行generator的方式。具体看连接如下:http://www.mybatis.org/generator/running/runningWithJava.html

    image.png

    看到了我红线标注的地方了么,这里才是有关java配置generatorConfig.xml的地方。可惜的是,忽略了很多的信息,对于我这种新手来说。最不擅长看官方文档了,所以很难才能找到这里。
    查看文档,大体上官方文档给出的意思是,在相应的方法中,将下面的代码写入,然后再在注释的地方进行详细的配置。我具体的java配置如下:

    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");
            commentGeneratorConfiguration.addProperty("suppressAllComments","true");
    
            /*数据库链接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());
                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;
    }
    
    

    导入mybatis generator 的maven依赖之后,把局部变量的内容更换城你自己的运行maim方法即可。

    相关文章

      网友评论

        本文标题:Mybatis Generator(MBG) -- Java 配

        本文链接:https://www.haomeiwen.com/subject/nbjydqtx.html