编程知识 cdmana.com

Mybatis plus code tool class

package cn.cqs.springbootmybatisplus.util;

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






/**

  • @discription :
    // * @author: cqs
  • @date: 2020-11-11 2:09
    */

public class GeneratorPlusCodeConfigV3 {

public static void main(String[] args) {
    String packageName = "cn.cqs.springbootmybatisplus";
    boolean serviceNameStartWithI = false;//auth -> UserService,  Set to true: auth -> IUserService
    generateByTables(serviceNameStartWithI, packageName, "saytime", "sys", "user");

    System.out.println("completed...");
}

/**
 * @param serviceNameStartWithI
 * @param packageName            Package name 
 * @param author                 author 
 * @param database               Database name 
 * @param tableNames             Table name 
 */
private static void generateByTables(boolean serviceNameStartWithI, String packageName, String author, String database, String... tableNames) {

// 1. Set global configuration global
GlobalConfig config = new GlobalConfig();
// Get the project root path
String projectPath = System.getProperty("user.dir");
// Set the build path
config.setOutputDir(projectPath + "/src/main/java");
// config.setOutputDir("e:\codeGen"); Set to disk
// Set up activity record ActiveRecord
config.setActiveRecord(false);
// Set the author
config.setAuthor(author);
// The second time the setting file is repeated
config.setFileOverride(true);
// Set the cache cache Second level cache
config.setEnableCache(false);
// Entity properties Swagger2 annotation
config.setOpen(false);
// Custom file name , Be careful %s Automatically populates table entity properties !
config.setMapperName("%sMapper");
config.setXmlName("%sMapper");
// XML ResultMap
config.setBaseResultMap(true);
// XML columList
config.setBaseColumnList(false);
if (!serviceNameStartWithI) {
// Set up service Whether to add... Before the interface I(service The interface name )
config.setServiceName("%sService");
config.setServiceImplName("%sServiceImpl");
}
String dbUrl = "jdbc:mysql://192.168.1.115:3306/" + database + "?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false";
String username = "cqs";
String password = "cqs";
String driverName = "com.mysql.cj.jdbc.Driver";































// 2. Set up the database connection pool DataSource
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setDbType(DbType.MYSQL)
.setUrl(dbUrl)
.setUsername(username)
.setPassword(password)
.setDriverName(driverName);
// 3. Set the strategy Strategy
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setCapitalMode(true);
// Combined with the Lombok plug-in unit , So set to true, If there is no integration Lombok, It can be set to false
strategyConfig.setEntityLombokModel(false);
strategyConfig.setNaming(NamingStrategy.underline_to_camel);
// .setSuperMapperClass("cn.saytime.mapper.BaseMapper")
strategyConfig.setInclude(tableNames);// Change to the table name you need , Multiple table name arrays
// 4. Set package configuration package
PackageConfig packageConfig = new PackageConfig();
//pc.setModuleName(scanner(" Module name "));
// Set package name
packageConfig.setParent(packageName);
packageConfig.setController("web");
// Set the object instance class
packageConfig.setEntity("model");
// dao Interface package name
packageConfig.setMapper("mapper");
// service Interface package name
packageConfig.setService("service");
// service Implementation package name
packageConfig.setServiceImpl("service.impl");
packageConfig.setXml("mybatis.mappers");




























//
// Code generator --4. Application auto generation
AutoGenerator mpg = new AutoGenerator();
mpg.setGlobalConfig(config)
.setDataSource(dataSourceConfig)
.setStrategy(strategyConfig)
.setPackageInfo(packageConfig).execute();
}
}







版权声明
本文为[http://www.bing.com/searc....tin-round+rock+texas]所创,转载请带上原文链接,感谢

Scroll to Top