Mybatis-plus 逆向工程

161 阅读2分钟

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @Author D
 * @Time 2022年8月4日 上午8:38:53
 * @Version 1.0
 *          <p>
 *          Description:
 *          </p>
 */
public class CodeGenerator {
	// 数据库连接参数
	public static String driver = "com.mysql.cj.jdbc.Driver";
	public static String url = "jdbc:mysql://127.0.0.1:3306/interfacecenter?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true";
	public static String username = "username";
	public static String password = "password";
	// 父级别包名称
	public static String parentPackage = "com.szlanyou.busicen.xapi";
	// 代码生成的目标路径
	public static String generateTo = "D:\\download/";
	// mapper.xml的生成路径,复制项目中的xml文件存档地址
	 public static String mapperXmlPath = "/src/main/resources/mybatis/mapping/mysql";
	// 控制器的公共基类,用于抽象控制器的公共方法,null值表示没有父类
	public static String baseControllerClassName;
	// 业务层的公共基类,用于抽象公共方法
	public static String baseServiceClassName;
	// 作者名
	public static String author = "D";
	// 模块名称,用于组成包名
	public static String modelName = "goinixvr";
	// Mapper接口的模板文件,不用写后缀 .ftl
	// public static String mapperTempalte = "/ftl/mapper.java";

	/**
	 * <p>
	 * 读取控制台内容
	 * </p>
	 */
	public static String scanner(String tip) {
		Scanner scanner = new Scanner(System.in);
		StringBuilder help = new StringBuilder();
		help.append("请输入" + tip + ":");
		System.out.println(help.toString());
		if (scanner.hasNext()) {
			String ipt = scanner.next();
			if (StringUtils.isNotEmpty(ipt)) {
				return ipt;
			}
		}
		throw new MybatisPlusException("请输入正确的" + tip + "!");
	}

	/**
	 * RUN THIS
	 */
	public static void main(String[] args) {
		// 代码生成器
		AutoGenerator mpg = new AutoGenerator();

		// 全局配置
		GlobalConfig gc = new GlobalConfig();
		String projectPath = System.getProperty("user.dir");
		gc.setOutputDir(generateTo);
		gc.setAuthor(author);
		gc.setOpen(false);
		// 设置时间类型为Date
		gc.setDateType(DateType.TIME_PACK);
		// 开启swagger
		// gc.setSwagger2(true);
		// 设置mapper.xml的resultMap
		gc.setBaseResultMap(true);
		mpg.setGlobalConfig(gc);

		// 数据源配置
		DataSourceConfig dsc = new DataSourceConfig();
		dsc.setUrl(url);
		// dsc.setSchemaName("public");
		dsc.setDriverName(driver);
		dsc.setUsername(username);
		dsc.setPassword(password);
		mpg.setDataSource(dsc);

		// 包配置
		PackageConfig pc = new PackageConfig();
		pc.setEntity("entities");
		// pc.setModuleName(scanner("模块名"));
		pc.setModuleName(modelName);
		pc.setParent(parentPackage);
		mpg.setPackageInfo(pc);

		// 自定义配置
		InjectionConfig cfg = new InjectionConfig() {
			@Override
			public void initMap() {
				// to do nothing
			}
		};
		List<FileOutConfig> focList = new ArrayList<>();
		focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
			@Override
			public String outputFile(TableInfo tableInfo) {
				// 自定义输入文件名称
				return projectPath + mapperXmlPath + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
			}
		});
		cfg.setFileOutConfigList(focList);
		mpg.setCfg(cfg);
		mpg.setTemplate(new TemplateConfig().setXml(null));
		// mpg.setTemplate(new TemplateConfig().setMapper(mapperTempalte));

		// 策略配置
		StrategyConfig strategy = new StrategyConfig();
		strategy.setNaming(NamingStrategy.underline_to_camel);
		// 字段驼峰命名
		strategy.setColumnNaming(NamingStrategy.underline_to_camel);
		// 设置实体类的lombok
		strategy.setEntityLombokModel(true);
		// 设置controller的父类
		if (baseControllerClassName != null)
			strategy.setSuperControllerClass(baseControllerClassName);
		// 设置服务类的父类
		if (baseServiceClassName != null)
			strategy.setSuperServiceImplClass(baseServiceClassName);
		// strategy.
		// 设置实体类属性对应表字段的注解
		strategy.setEntityTableFieldAnnotationEnable(true);
		// 设置表名
		
		  String tableName = scanner("表名, all全部表"); if(!
		  "all".equalsIgnoreCase(tableName)){ strategy.setInclude(tableName); }
		 
                // 单表
		/*String[] tableNames = { "t_orc_bu_gnc_wx_appraise", "t_orc_bu_gnc_wx_user_service", "t_orc_bu_gnc_wx_browse",
				"t_orc_db_gnc_wx_user", "t_orc_db_gnc_sys_wx_car", "t_orc_db_gnc_sys_user" };// 数据库表名的集合
		*/
                 // 多表
		 /* String[] tableNames ={ 
		  "​t_if_r_db_gnc_wx_user", 
		  "​t_if_r_db_gnc_sys_wx_car",
		  "​t_if_r_db_gnc_sys_user", 
		  "​t_if_r_bu_gnc_wx_browse_his"
		  };
		 
		for (int i = 0; i < tableNames.length; i++) {
			strategy.setInclude(tableNames);
		}*/
		strategy.setTablePrefix(pc.getModuleName() + "_");
		strategy.setRestControllerStyle(true);
		mpg.setStrategy(strategy);

		// 选择 freemarker 引擎需要指定如下加,注意 pom 依赖必须有!
		mpg.setTemplateEngine(new FreemarkerTemplateEngine());
	    mpg.execute();
	}
}