mybaties-plus代碼生成器

代碼生成器作用很強大,能夠減少無用的代碼量。尤其數據庫字段很多的時候,確實比較方便。

依賴文件:

	<dependency>
		<groupId>com.baomidou</groupId>
		<artifactId>mybatis-plus</artifactId>
		<version>${mybatis-plus.version}</version>
	</dependency>
	<!-- 模板引擎 -->
	<dependency>
		<groupId>org.apache.velocity</groupId>
		<artifactId>velocity</artifactId>
		<version>1.7</version>
	</dependency>
執行文件:

public class MyBatisCoder {
	/**
	 * <p>
	 * MySQL 生成演示
	 * </p>
	 */
	public static void main(String[] args) {
		AutoGenerator mpg = new AutoGenerator();

		// 全局配置
		GlobalConfig gc = new GlobalConfig();
		gc.setOutputDir("F:/demo/service-platform/service-console/target");
		
		gc.setFileOverride(true);
		gc.setActiveRecord(true);
		gc.setEnableCache(false);// XML 二級緩存
		gc.setBaseResultMap(true);// XML ResultMap
		gc.setBaseColumnList(false);// XML columList
		gc.setAuthor("renjianxu");

		// 自定義文件命名,注意 %s 會自動填充表實體屬性!
		gc.setMapperName("%sDao");
		gc.setXmlName("%sMapper");
		gc.setServiceName("%sService");
		gc.setServiceImplName("%sServiceImpl");
		gc.setControllerName("%sController");
		mpg.setGlobalConfig(gc);

		// 數據源配置
		DataSourceConfig dsc = new DataSourceConfig();
		dsc.setDbType(DbType.MYSQL);
		dsc.setDriverName("com.mysql.jdbc.Driver");
		dsc.setUsername("root");
		dsc.setPassword("123456");
		dsc.setUrl("jdbc:mysql://localhost:3306/xxx?characterEncoding=utf8");
		mpg.setDataSource(dsc);

		// 策略配置
		StrategyConfig strategy = new StrategyConfig();
	//	String[] tablePrefixes = new String[]{"gsp_"};
	//	strategy.setTablePrefix(tablePrefixes);// 此處可以修改爲您的表前綴
		strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
		//strategy.setInclude(new String[] { "user" }); // 需要生成的表
		// strategy.setExclude(new String[]{"test"}); // 排除生成的表
		// 字段名生成策略
		//strategy.setFieldNaming(NamingStrategy.underline_to_camel.name());
		// 自定義實體父類
		// strategy.setSuperEntityClass("com.baomidou.demo.TestEntity");
		// 自定義實體,公共字段
		// strategy.setSuperEntityColumns(new String[] { "test_id", "age" });
		// 自定義 mapper 父類
		// strategy.setSuperMapperClass("com.baomidou.demo.TestMapper");
		// 自定義 service 父類
		// strategy.setSuperServiceClass("com.baomidou.demo.TestService");
		// 自定義 service 實現類父類
		// strategy.setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl");
		// 自定義 controller 父類
		// strategy.setSuperControllerClass("com.baomidou.demo.TestController");
		// 【實體】是否生成字段常量(默認 false)
		// public static final String ID = "test_id";
		// strategy.setEntityColumnConstant(true);
		// 【實體】是否爲構建者模型(默認 false)
		// public User setName(String name) {this.name = name; return this;}
		// strategy.setEntityBuliderModel(true);
		mpg.setStrategy(strategy);

		// 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("cn.rjx.service.console");
       // pc.setController("action");
        pc.setEntity("entity");
      //  pc.setMapper("dao");
       // pc.setService("service");
        //pc.setServiceImpl("service.impl");
        //pc.setModuleName("test");
        mpg.setPackageInfo(pc);

		// 注入自定義配置,可以在 VM 中使用 cfg.abc 設置的值
		InjectionConfig cfg = new InjectionConfig() {
			@Override
			public void initMap() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
				this.setMap(map);
			}
		};
		mpg.setCfg(cfg);

		// 自定義模板配置
		// TemplateConfig tc = new TemplateConfig();
		// tc.setController("...");
		// tc.setEntity("...");
		// tc.setMapper("...");
		// tc.setXml("...");
		// tc.setService("...");
		// tc.setServiceImpl("...");
		// mpg.setTemplate(tc);

		// 執行生成
		mpg.execute();

		// 打印注入設置
		System.err.println(mpg.getCfg().getMap().get("abc"));
	}
	
}

結果:




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章