Mybatis Generator
- 使用xml組態檔形式自動生成
- 只生成物體類、mapper介面及mapper.xml,并且包含豐富的內容
- 首先添加mybatis依賴和相關插件
<!-- 依賴MyBatis核心包 -->
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
</dependencies>
<!-- 控制Maven在構建程序中相關配置 -->
<build>
<!-- 構建程序中用到的插件 -->
<plugins>
<!-- 具體插件,逆向工程的操作是以構建程序中插件形式出現的 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.0</version>
<!-- 插件的依賴 -->
<dependencies>
<!-- 逆向工程的核心依賴 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
<!-- 資料庫連接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.2</version>
</dependency>
<!-- MySQL驅動 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.8</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
- 創建generatorConfig.xml組態檔,從官網中復制下來即可.其中元素標簽說明卸載注釋中
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--
targetRuntime: 執行生成的逆向工程的版本
MyBatis3Simple:生成基本的CURD(清新簡潔版)
MyBatis3:生成帶條件的CURD(奢華尊享版)
-->
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- 資料庫連接資訊 -->
<jdbcConnection driver
connectionURL="jdbc:mysql:///your_database?useUnicode=true&characterEncoding=utf8"
userId="username"
password="password" />
<!-- javaBean的生成策略 -->
<javaModelGenerator targetPackage="module_name.entity" targetProject=".\src\main\java">
<property name="enableSubPackages" value="https://www.cnblogs.com/aitiknowledge/archive/2022/07/22/true" />
<property name="trimStrings" value="https://www.cnblogs.com/aitiknowledge/archive/2022/07/22/true" />
</javaModelGenerator>
<!-- SQL映射檔案的生成策略 -->
<sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources">
<property name="enableSubPackages" value="https://www.cnblogs.com/aitiknowledge/archive/2022/07/22/true" />
</sqlMapGenerator>
<!-- Mapper介面的生成策略 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="module_name.mapper" targetProject=".\src\main\java">
<property name="enableSubPackages" value="https://www.cnblogs.com/aitiknowledge/archive/2022/07/22/true" />
</javaClientGenerator>
<!-- 逆向分析的表 -->
<!-- tableName設定為*號,可以對應所有的表,此時不用寫domainObjectName -->
<!-- domainObjectName屬性指定生成出來的物體類的類名 -->
<table tableName="admin" domainObjectName="Admin" />
</context>
</generatorConfiguration>
- 最后在右邊點擊maven中插件,雙擊mybatis-generator:generate
- 說明:上述兩個組態檔都會報紅,不用管盡管運行即可
MybatisPlus Generator
- 使用代碼形式自動生成
- 會生成從Controller層到Dao層的所有檔案,其中還包括物體類
- 首先添加必須的依賴
<dependencies>
<!--mybatis-plus 持久層-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<!-- velocity 模板引擎, Mybatis Plus 代碼生成器需要 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<!--lombok用來簡化物體類:需要安裝lombok插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
</dependencies>
- 這里是在單元測驗中運行
public class GeneratorTest{
@Test
public void run() {
// 1、創建代碼生成器
AutoGenerator mpg = new AutoGenerator();
// 2、全域配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
// 換成絕對路徑
gc.setOutputDir("your project absolute path" + "/src/main/java");
gc.setAuthor("xsha_h");
gc.setOpen(false); //生成后是否打開資源管理器
gc.setFileOverride(false); //重新生成時檔案是否覆寫
gc.setServiceName("%sService"); //去掉Service介面的首字母I
gc.setIdType(IdType.ID_WORKER_STR); //主鍵策略
gc.setDateType(DateType.ONLY_DATE);//定義生成的物體類中日期型別
gc.setSwagger2(true);//開啟Swagger2模式
mpg.setGlobalConfig(gc);
// 3、資料源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql:///database_name?serviceTimezone=GMT%2B8");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("username");
dsc.setPassword("password");
dsc.setDbType(DbType.MYSQL);
mpg.setDataSource(dsc);
// 4、包配置
PackageConfig pc = new PackageConfig();
// 包名 com.xsha.module_name
pc.setParent("com.xsha");
pc.setModuleName("module_name"); //模塊名
pc.setController("controller");
pc.setEntity("entity");
pc.setService("service");
pc.setMapper("mapper");
mpg.setPackageInfo(pc);
// 5、策略配置
StrategyConfig strategy = new StrategyConfig();
// 生成表格名稱對應的物體類
strategy.setInclude("table_name1", "table_name2", "table_name3", "table_name4");
strategy.setNaming(NamingStrategy.underline_to_camel);//資料庫表映射到物體的命名策略
strategy.setTablePrefix(pc.getModuleName() + "_"); //生成物體時去掉表前綴(根據需求變化)
strategy.setColumnNaming(NamingStrategy.underline_to_camel);//資料庫表欄位映射到物體的命名策略
strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter鏈式操作
strategy.setRestControllerStyle(true); //restful api風格控制器
strategy.setControllerMappingHyphenStyle(true); //url中駝峰轉連字符
mpg.setStrategy(strategy);
// 6、執行
mpg.execute();
}
}
- 運行即可生效
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/500064.html
標籤:其他
上一篇:🔥Likeshop100%開源無加密-B2B2C多商戶商城系統!!
下一篇:Java內部類簡介說明
