MybatisPlus代码生成器

pom.xml

<dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.5</version>
 </dependency>
<!-可能需要模板依赖->
 <dependency>
           <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.28</version>
</dependency>

代码:

/**
 * @desc 生成mybatis plus相关的代码工具类 具体配置请参考
 * http://mp.baomidou.com/guide/generator.html#%E6%B7%BB%E5%8A%A0%E4%BE%9D%E8%B5%96
 */
public class MybatisPlusGenerator {
	//要生成的表名
	public static String[] genTablesName= new String[]{
			"sys_user",
			"sys_role"
	};
	public static void runMybatisPlusGenerator() {
		AutoGenerator autoGenerator = new AutoGenerator();
		// 全局配置
		GlobalConfig config = new GlobalConfig();
		config.setOutputDir("D:\\java");
		config.setFileOverride(true);
		config.setActiveRecord(true);
		config.setEnableCache(false);// XML 二级缓存
		config.setBaseResultMap(true);// XML ResultMap
		config.setBaseColumnList(false);// XML columList
		config.setAuthor("Mr.Geng");
		config.setOpen(false);
		// 自定义文件命名,注意 %s 会自动填充表实体属性!
		config.setMapperName("%sMapper");
		config.setXmlName("%sMapper");
		config.setServiceName("%sService");
		config.setServiceImplName("%sServiceImpl");
		config.setControllerName("%sController");
		autoGenerator.setGlobalConfig(config);
		// 数据源配置
		DataSourceConfig dataSourceConfig = new DataSourceConfig();
		dataSourceConfig.setDbType(DbType.MYSQL);
		dataSourceConfig.setDriverName("com.mysql.jdbc.Driver");
		dataSourceConfig.setUsername("root");
		dataSourceConfig.setPassword("123456");
		dataSourceConfig.setUrl("jdbc:mysql://127.0.0.1:3306/data?characterEncoding=utf8");
		autoGenerator.setDataSource(dataSourceConfig);
		// 策略配置
		StrategyConfig strategy = new StrategyConfig();
		//strategy.setTablePrefix("bmd_");// 此处可以修改为您的表前缀
		strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
		strategy.setInclude(genTablesName); // 需要生成的表
		// strategy.setExclude(new String[]{"test"}); // 排除生成的表
		// 字段名生成策略
		strategy.setColumnNaming(NamingStrategy.underline_to_camel);
		// 自定义实体父类
		// strategy.setSuperEntityClass("com.maxbill.base.SuperModel");
		// 自定义实体,公共字段
		// strategy.setSuperEntityColumns(new String[] { "test_id", "age" });
		// 自定义 mapper 父类
		// strategy.setSuperMapperClass("com.maxbill.base.SuperMapper");
		// 自定义 service 父类
		// strategy.setSuperServiceClass("com.maxbill.base.SuperService");
		// 自定义 service 实现类父类
		// strategy.setSuperServiceImplClass("com.maxbill.base.SuperServiceImpl");
		// 自定义 controller 父类
		// strategy.setSuperControllerClass("com.maxbill.base.SuperController");
		// 【实体】是否生成字段常量(默认 false)
		// public static final String ID = "test_id";
		strategy.setEntityColumnConstant(false);
		// 【实体】是否为构建者模型(默认 false)
		// public User setName(String name) {this.name = name; return this;}
		strategy.setEntityBuilderModel(true);
		autoGenerator.setStrategy(strategy);
		// 包配置
		PackageConfig packageConfig = new PackageConfig();
		packageConfig.setParent("com.enso");
		packageConfig.setController("controller");
		packageConfig.setEntity("entity");
		packageConfig.setService("service");
		packageConfig.setServiceImpl("service.impl");
		packageConfig.setMapper("mapper");
		packageConfig.setXml("mapper");
		// packageConfig.setModuleName("base");
		autoGenerator.setPackageInfo(packageConfig);
		// 注入自定义配置,可以在 VM 中使用 cfg.abc 设置的值
		/*InjectionConfig injectionConfig = new InjectionConfig() {
			@Override
			public void initMap() {
				Map map = new HashMap();
				map.put("maxbill", this.getConfig().getGlobalConfig().getAuthor() + "-MybatisPlus");
				this.setMap(map);
			}
		};
		autoGenerator.setCfg(injectionConfig);*/
		// 执行生成
		autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine());
		autoGenerator.execute();
		// 打印注入设置
		//System.err.println(autoGenerator.getCfg().getMap().get("maxbill"));
	}

	public static void main(String[] args) {
		runMybatisPlusGenerator();
		//System.out.println(LocalDateTime.now());
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值