代码模板生成

package com.tkgm.kf.properties.db;

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.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * 代码模板生成
 * @author w_liusc
 *
 */
public class Generator {
	public static void main(String[] args) {
		String packageName = "record";
		boolean serviceNameStartWithI = false;// user -> UserService, 设置成true: user -> IUserService
		//GenerateApi(serviceNameStartWithI, packageName, "t_dict_type");// 修改为你的表名
		GenerateAdmin(serviceNameStartWithI, packageName, "t_mail_list");// 修改为你的表名
		
	}

	protected static void GenerateApi(boolean serviceNameStartWithI, String packageName, String... tableNames) {
		System.out.println("开始生成接口模板....");
		// 全局配置
		GlobalConfig config = new GlobalConfig();
		config.setActiveRecord(false)
				.setEnableCache(false)
				.setAuthor("lsc")
				.setOutputDir("D:\\workspace\\tkgm-pom\\tkgm-kf\\src\\main\\java")
				.setOpen(false);
		
		// 数据源配置
		DataSourceConfig dataSourceConfig = new DataSourceConfig();
		dataSourceConfig.setDbType(DbType.MYSQL)
				.setUrl("jdbc:mysql://10.92.1.17:3306/tkgmkf_db")
				.setUsername("w_liusc")
				.setPassword("tkamcgm.00")
				.setDriverName("com.mysql.cj.jdbc.Driver")
				.setTypeConvert(new MySqlTypeConvert());

		// 配置策略
		StrategyConfig strategyConfig = new StrategyConfig();
		strategyConfig.setCapitalMode(true)
				.setEntityLombokModel(false)
				.setDbColumnUnderline(true)
				.setTablePrefix(new String[] { "t_" })// 此处可以修改为您的表前缀
				.setNaming(NamingStrategy.underline_to_camel)
				.setSuperControllerClass("com.tkgm.kf.properties.base.ApiController")
				.setInclude(tableNames);// 修改替换成你需要的表名,多个表名传数组

		// 包配置
		PackageConfig packageInfo = new PackageConfig();
		packageInfo.setParent("com.tkgm.kf")// 自定义包路径
				.setController("api." + packageName + ".controller")
				.setService("api." + packageName + ".service")
				.setServiceImpl("api." + packageName + ".service.impl")
				.setEntity("mapper.po")
				.setMapper("mapper")
				.setXml("mapper");

		// 执行配置
		new AutoGenerator()
				.setGlobalConfig(config)
				.setDataSource(dataSourceConfig)
				.setStrategy(strategyConfig)
				.setPackageInfo(packageInfo)
				.execute();
		System.out.println("接口模板已生成,请刷新项目目录");
	}
	
	
	
	protected static void GenerateAdmin(boolean serviceNameStartWithI, String packageName, String... tableNames) {
		System.out.println("开始生成后台模板");
		// 全局配置
		GlobalConfig config = new GlobalConfig();
		config.setActiveRecord(false)
		.setEnableCache(false).setAuthor("lsc")
		.setOutputDir("D:\\workspace\\tkgm-pom\\tkgm-kf\\src\\main\\java")
		.setOpen(false);
		
		// 数据源配置
		DataSourceConfig dataSourceConfig = new DataSourceConfig();
		dataSourceConfig.setDbType(DbType.MYSQL)
						.setUrl("jdbc:mysql://10.92.1.17:3306/tkgmkf_db")
						.setUsername("w_liusc")
						.setPassword("tkamcgm.00")
						.setDriverName("com.mysql.cj.jdbc.Driver")
						.setTypeConvert(new MySqlTypeConvert());
		
		// 配置策略
		StrategyConfig strategyConfig = new StrategyConfig();
		strategyConfig.setCapitalMode(true)
					  .setEntityLombokModel(false)
					  .setDbColumnUnderline(true)
					  .setTablePrefix(new String[] { "t_" })// 此处可以修改为您的表前缀
					  .setNaming(NamingStrategy.underline_to_camel)
					  .setSuperControllerClass("com.tkgm.kf.properties.base.BaseController")
					  .setInclude(tableNames);// 修改替换成你需要的表名,多个表名传数组
		
		// 包配置
		PackageConfig packageInfo = new PackageConfig();
		packageInfo.setParent("com.tkgm.kf")// 自定义包路径
					.setController("admin." + packageName + ".controller")
					.setService("admin." + packageName + ".service")
					.setServiceImpl("admin." + packageName + ".service.impl")
					.setEntity("mapper.po")
					.setMapper("mapper")
					.setXml("mapper");
		
		// 执行配置
		new AutoGenerator().setGlobalConfig(config)
						   .setDataSource(dataSourceConfig)
						   .setStrategy(strategyConfig)
						   .setPackageInfo(packageInfo)
						   .execute();
		
		System.out.println("后台模板已生成,请刷新项目目录");
	}
}

  

    <!-- mp -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>2.3</version>
        </dependency>
        <!-- velocity 依赖,用于代码生成 -->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.0</version>
        </dependency>

 

上一篇:Mybatis-plus 笔记


下一篇:mybatis-plus 入门使用