mybatisPlus code generation tools

Hand knock entity, dao, mapper.xml, service, controller of the old routine, not only make people tired, but also seriously affect the development efficiency. As a developer, you should not focus more on business function to achieve it?

After all these extraneous business logic, if there is a ready-made code just fine!

This does not, mybatisplus provides a code generator, mvc three easily generate the code.

First, the introduction of pom file

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.1</version>
        </dependency>    

Then, the code generator follows:

package domain;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;

/**
 * 生成代码工具
 */
public class Generate {

    public static void main(String[] args) {
        Generate g = new Generate();
        boolean startWithI = true;
        String projectName = "Demo";
        String packageName = "com.xp.boot.web";
        String tableName = "test_log";
        g.generateByTables(startWithI,projectName,packageName,tableName);
    }
    /**
     * 根据表自动生成
     *
     * @param serviceNameStartWithI 默认为false
     * @parampackageName package names 
     * @param TableNames table
      * / 
    Private  void generateByTables ( Boolean serviceNameStartWithI, the projectName String, String packageName, TableNames String ...) {
         // configuration data source 
        DataSourceConfig dataSourceConfig = getDataSourceConfig ();
         // policy configuration 
        StrategyConfig strategyConfig = getStrategyConfig (TableNames);
         // global variable configuration 
        globalconfig globalconfig = getGlobalConfig (serviceNameStartWithI, the projectName);
         // package names arranged 
        packageConfig packageConfig =getPackageConfig (the packageName);
         // automatically generated 
        atuoGenerator (dataSourceConfig, strategyConfig, globalconfig, packageConfig); 
    } 

    / ** 
     * Integration 
     * 
     * @param dataSourceConfig configuration data source 
     * @param strategyConfig policy configuration 
     * @param config global configuration variables 
     * @param packageConfig configuration package names
      * / 
    Private  void atuoGenerator (dataSourceConfig dataSourceConfig, strategyConfig strategyConfig, globalconfig config, packageConfig packageConfig) {
         new new AutoGenerator ()
                .setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(packageConfig)
                .setTemplateEngine(new VelocityTemplateEngine())
                .execute();
    }

    /**
     * 设置包名
     *
     * @param packageName 父路径包名
     * @return PackageConfig 包名配置
     */
    private PackageConfig getPackageConfig(String packageName) {
        return new PackageConfig()
                .setParent(packageName)
                .setXml("mapper")
                .setMapper("mapper")
                .setController("controller")
                .setService("service")
                .setEntity("entity");
    }

    /**
     * 全局配置
     *
     * @param serviceNameStartWithI false
     * @return GlobalConfig
     */
    private GlobalConfig getGlobalConfig(boolean serviceNameStartWithI,String projectName) {
        GlobalConfig globalConfig = newGlobalconfig (); 
        globalconfig 
                .setBaseColumnList ( to true ) 
                .setBaseResultMap ( to true ) 
                .setActiveRecord ( to false ) 
                .setAuthor ( "Nicholas - Shaw" )
                 // set the output path 
                .setOutputDir (System.getProperty ( "user.dir") + "/ "+ the projectName +" / the src / main / Java / " ) 
                .setFileOverride ( to true );
         IF ! ( serviceNameStartWithI) {
             // set the service name 
            globalConfig.setServiceName ("% Sservice " );
        }
        return globalconfig; 
    } 

    / ** 
     * policy configuration 
     * 
     * @param TableNames table name 
     * @return StrategyConfig
      * / 
    Private StrategyConfig getStrategyConfig (String ... TableNames) {
         return  new new StrategyConfig ()
                 // global capital named ORACLE attention 
                .setCapitalMode ( to true ) 
                .setEntityLombokModel ( to true ) 
                .setRestControllerStyle ( false )
                 // from the database table to a file naming policy
                .setNaming (NamingStrategy.underline_to_camel) 
                .setColumnNaming (NamingStrategy.underline_to_camel) 
                // need to generate a table name, the table name to a plurality of array 
                .setInclude (TableNames); 
    } 

    / ** 
     * Configuration Data Source 
     * 
     * @return data source configuration DataSourceConfig
      * / 
    Private DataSourceConfig getDataSourceConfig () {
         return  new new DataSourceConfig () setDbType (DbType.MYSQL). 
                .setUrl ( "jdbc: MySQL: // localhost:? 3306 / the Test serverTimezone = Hongkong & nullCatalogMeansCurrent = to true & characterEncoding = UTF-8 & useSSL = false" ) 
                .setUsername ( "root")
                .setPassword("123456")
                .setDriverName("com.mysql.cj.jdbc.Driver");
    }


}

 

Guess you like

Origin www.cnblogs.com/xphhh/p/11460600.html