MyBatis-Plus之程式碼生成器

林夕$相心發表於2020-10-18

一、引入pom依賴

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.atguigu.mp</groupId>
  <artifactId>mp01</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>
  		<!-- mp依賴
  			 mybatisPlus 會自動的維護Mybatis 以及MyBatis-spring相關的依賴
  		 -->
		<dependency>
		    <groupId>com.baomidou</groupId>
		    <artifactId>mybatis-plus</artifactId>
		    <version>2.3</version>
		</dependency>		
		<!--junit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.9</version>
		</dependency>
		<!-- log4j -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
		<!-- c3p0 -->
		<dependency>
			<groupId>com.mchange</groupId>
			<artifactId>c3p0</artifactId>
			<version>0.9.5.2</version>
		</dependency>
		<!-- mysql -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.37</version>
		</dependency>
		<!-- spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.3.10.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>4.3.10.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.3.10.RELEASE</version>
		</dependency>
		
		<!-- Apache velocity -->
		<dependency>
		    <groupId>org.apache.velocity</groupId>
		    <artifactId>velocity-engine-core</artifactId>
		    <version>2.0</version>
		</dependency>
		
		<!-- sfl4j -->
		 <dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.7</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.7</version>
		</dependency>
		
  
  </dependencies>
  
  
</project>

二、配置測試

	public void  testGenerator() {
		//1. 全域性配置
		GlobalConfig config = new GlobalConfig();
		config.setActiveRecord(true) // 是否支援AR模式
			  .setAuthor("Tanghoa") // 作者
			  .setOutputDir("XXXX")
			  //XXX:為程式碼路徑地址:直接複製過來即可  D:\\workspace
			  .setFileOverride(true)  // 檔案覆蓋
			  .setIdType(IdType.AUTO) // 主鍵策略
			  .setServiceName("%sService")  // 設定生成的service介面的名字的首字母是否為I
			  					   // IEmployeeService
 			  .setBaseResultMap(true)
 			  .setBaseColumnList(true);
		
		//2. 資料來源配置
		DataSourceConfig  dsConfig  = new DataSourceConfig();
		dsConfig.setDbType(DbType.MYSQL)  // 設定資料庫型別
				.setDriverName("com.mysql.jdbc.Driver")
				.setUrl("jdbc:mysql://localhost:3306/mp")
				.setUsername("root")
				.setPassword("123456");
		 
		//3. 策略配置
		StrategyConfig stConfig = new StrategyConfig();
		stConfig.setCapitalMode(true) //全域性大寫命名
				.setDbColumnUnderline(true)  // 指定表名 欄位名是否使用下劃線
				.setNaming(NamingStrategy.underline_to_camel) // 資料庫表對映到實體的命名策略
				.setTablePrefix("tbl_")
				.setInclude("tbl_user");  // 生成的表
		
		//4. 包名策略配置 
		PackageConfig pkConfig = new PackageConfig();
		pkConfig.setParent("com.XXX.xx")
				.setMapper("mapper")
				.setService("service")
				.setController("controller")
				.setEntity("beans")
				.setXml("mapper");
		
		//5. 整合配置
		AutoGenerator  ag = new AutoGenerator();
		
		ag.setGlobalConfig(config)
		  .setDataSource(dsConfig)
		  .setStrategy(stConfig)
		  .setPackageInfo(pkConfig);
		
		//6. 執行
		ag.execute();
	}
	
	
	
}

相關文章