java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot 逆向工程

SpringBoot整合MybatisPlusGernerator实现逆向工程

作者:哈__

在我们写项目的时候,我们时常会因为需要创建很多的项目结构而头疼,本文主要介绍了SpringBoot整合MybatisPlusGernerator实现逆向工程,具有一定的参考价值,感兴趣的可以了解一下

在我们写项目的时候,我们时常会因为需要创建很多的项目结构而头疼。项目中的表很多的时候,我们连实体类都创建不完,这时候就需要我们的逆向工程来帮助我们生成我们的框架结构。这些结构都差不多,实体类,表现层,业务层和持久层。

大家可以使用自己的数据库,这篇文章主要是提供一下逆向工程的代码。

一、引入依赖

         <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
        </dependency>
         <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.3.2</version>
        </dependency>
         <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.7</version>
        </dependency>

二、代码

生成的代码作为我们的工具类即可。

public static void main(String[] args) {
        AutoGenerator autoGenerator = new AutoGenerator();

        //设置我们的数据源,根据自己的实际情况填写
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL);
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("2020");
        dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/community?useUnicode=true&characterEncoding=UTF-8");
        autoGenerator.setDataSource(dataSourceConfig);

        // 全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOpen(false);
        //文件生成到我们的java目录下
        globalConfig.setOutputDir(System.getProperty("user.dir")+"/src/main/java");
        //文档注释的作者
        globalConfig.setAuthor("admin");
        globalConfig.setServiceName("%sService");
        autoGenerator.setGlobalConfig(globalConfig);

        //设置我们生成的包
        PackageConfig packageConfig = new PackageConfig();
        //父包
        packageConfig.setParent("com.ha");
        //实体类
        packageConfig.setEntity("entity");
        //mapper
        packageConfig.setMapper("mapper");
        //controller
        packageConfig.setController("controller");
        //service接口
        packageConfig.setService("service");
        //service接口实现类
        packageConfig.setServiceImpl("service.impl");
        autoGenerator.setPackageInfo(packageConfig);

        //这里对我们生成的实体类的属性做处理
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setEntityLombokModel(true);
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        // 在这里修改你的表名称 生成哪个表,写哪个表的名字
        strategyConfig.setInclude("manual_record");
        //因为我数据库的字段有时间类型的字段
        /*TableFill tableFill1 = new TableFill("in_time", FieldFill.INSERT);
        TableFill tableFill2 = new TableFill("out_time", FieldFill.INSERT);
        TableFill tableFill3 = new TableFill("sign_time", FieldFill.INSERT);
        List<TableFill> list = Arrays.asList(tableFill1,tableFill2,tableFill3);*/
        strategyConfig.setTableFillList(list);
        autoGenerator.setStrategy(strategyConfig);

        autoGenerator.execute();
    }

生成策略大家可以自己探索一下。下边的方法都是可以设置的策略。

    public StrategyConfig setCapitalMode(final boolean isCapitalMode)

    public StrategyConfig setNameConvert(final INameConvert nameConvert) 

    public StrategyConfig setNaming(final NamingStrategy naming) 

    public StrategyConfig setColumnNaming(final NamingStrategy columnNaming) 
        
    public StrategyConfig setSuperMapperClass(final String superMapperClass) 

    public StrategyConfig setEntitySerialVersionUID(final boolean entitySerialVersionUID) 
       
    public StrategyConfig setEntityColumnConstant(final boolean entityColumnConstant) 
        
    public StrategyConfig setChainModel(final boolean chainModel) 
      
    public StrategyConfig setEntityLombokModel(final boolean entityLombokModel) 
        

三、测试 

生成的实体类如下。这里代码太长了截图展示一部分。

到此这篇关于SpringBoot整合MybatisPlusGernerator实现逆向工程的文章就介绍到这了,更多相关SpringBoot 逆向工程内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文