代码生成器

**
 * 代码生成器 
 */
public class Generator {

	/******* 需要进行修改的参数  ********/

	private static final String AUTHOR = "作者";
	private static final String PARENT_PACKAGE = "com.demo";//包路径
	private static final String Module_Name = "mall";//模块名
	private static final String DB_URL = "jdbc:mysql://localhost:3306/icoding_mall?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2B8";
	private static final String DB_USER = "root";
	private static final String DB_PWD = "mysql";
	private static final String[] Tables = {
			"im_buycart",
			"im_carousel"};

	@Test
	public void genCode() {

		// 1.创建代码生成器
		AutoGenerator mpg = new AutoGenerator();


		// 2.全局配置
		GlobalConfig gc = new GlobalConfig();

		//gc.setOutputDir("D://generator/");	//生成代码存放目录
		String projectPath = System.getProperty("user.dir"); //获取项目目录
		gc.setOutputDir(projectPath + "/src/main/java");
		gc.setFileOverride(false);			//重新生成文件的时候是否覆盖
		gc.setActiveRecord(false);			//不需要ActiveRecord特性的请改为false
		gc.setEnableCache(false);			// XML 二级缓存
		gc.setBaseResultMap(false);			// XML ResultMap
		gc.setBaseColumnList(false);		// XML columList
		gc.setOpen(false);					//生成代码后是否打开文件
		gc.setAuthor(AUTHOR);				//作者
		// gc.setMapperName("%sDao");
		// gc.setXmlName("%sDao");
		gc.setServiceName("%sService");
		gc.setServiceImplName("%sServiceImpl");
		gc.setIdType(IdType.ID_WORKER_STR); // 主键策略
		gc.setDateType(DateType.ONLY_DATE); // 定义生成的实体类中的日期类型
		gc.setSwagger2(true);  				// 实体属性 Swagger2 注解

		mpg.setGlobalConfig(gc);


		// 3.数据源配置
		DataSourceConfig dsc = new DataSourceConfig();

		dsc.setDbType(DbType.MYSQL);		// 设置数据库类型
		dsc.setDriverName("com.mysql.cj.jdbc.Driver");
		dsc.setUsername(DB_USER);
		dsc.setPassword(DB_PWD);
		dsc.setUrl(DB_URL);

		mpg.setDataSource(dsc);


		//4.包配置
		PackageConfig pc = new PackageConfig();

		pc.setModuleName(Module_Name); 	//模块名 :mall
		pc.setParent(PARENT_PACKAGE);	//com.demo ,包路径
		pc.setController("controller");
		pc.setMapper("mapper");
		pc.setService("service");
		pc.setEntity("entity");
		mpg.setPackageInfo(pc);

		// 5.策略配置
		StrategyConfig strategy = new StrategyConfig();

		strategy.setInclude(Tables);		 	// 需要生成的表
		strategy.setNaming(NamingStrategy.underline_to_camel);	// 数据库表映射到实体类的命名策略
		strategy.setEntityLombokModel(true); 	//链式编程
		strategy.setLogicDeleteFieldName("is_delete"); //逻辑删除字段
		strategy.setEntityBooleanColumnRemoveIsPrefix(true); //去掉布尔值的 is_ 前缀
		strategy.setTablePrefix("im_");		// 代表前缀不生成

		// 自动填充,策略
		TableFill gmtCreate = new TableFill("create_time", FieldFill.INSERT);
		TableFill gmtModified = new TableFill("update_time",FieldFill.INSERT_UPDATE);
		ArrayList<TableFill> tableFills = new ArrayList<>();
		tableFills.add(gmtCreate);
		tableFills.add(gmtModified);
		strategy.setTableFillList(tableFills);

		//strategy.setVersionFieldName("version");		//乐观锁
		strategy.setRestControllerStyle(true); //restful api风格
		strategy.setControllerMappingHyphenStyle(true) ;// URL中驼峰转连字符

		/*
		strategy.setExclude(new String[]{"test"}); 			// 排除生成的表


		strategy.setSuperEntityClass("com.sme.frameworkFoundation.common.core.base.BaseModel");	// 自定义实体父类
		strategy.setSuperEntityColumns(new String[] { "create_by", "create_time", "modify_by", "modify_time" });//自定义实体,公共字段
		strategy.setSuperMapperClass("com.sme.frameworkFoundation.common.core.base.BaseMapper");// 自定义 mapper 父类
		strategy.setSuperServiceClass("com.sme.frameworkFoundation.common.core.base.BaseService");// 自定义 service 父类
		strategy.setSuperServiceImplClass("com.sme.frameworkFoundation.common.core.base.BaseServiceImpl");// 自定义 service 实现类父类
		strategy.setSuperControllerClass("com.sme.frameworkFoundation.common.core.base.BaseController");// 自定义 controller 父类

		*/

		mpg.setStrategy(strategy);


		mpg.execute();		// 执行生成
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值