Mysql+mybatis 反轉生成實體類和Mapper檔案以及Mapper.xml檔案
阿新 • • 發佈:2019-02-08
使用之前先在pom檔案中加上如下依賴:
<!-- 匯入Mysql資料庫連結jar包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<!-- mybatis自動生成實體類 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
這兩個依賴是程式能夠自動生成實體類的基礎,必不可少。下面是生成步驟:
①在當前專案中建立一個generator.xml檔案,這個檔名字可以自取,也不能太隨意
②generator.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>
<!-- <properties resource="db.properties" /> -->
<classPathEntry location="E:\Developments Tools\mysql-connector-java-5.1.33.jar" />
<context id="mysql2Beans" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 刪除程式碼中帶有 程式碼生成器的註釋資訊 -->
<property name="suppressAllComments" value="false" />
<!-- 是否去除自動生成的註釋 true:是 : false:否 -->
<property name="suppressDate" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="你的資料庫連線"
userId="登入名"
password="密碼" />
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="com.lyt.usermanage.pojo" targetProject="E:\ConvertBean">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="com.lyt.usermanage.mapper" targetProject="E:\ConvertBean">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator targetPackage="com.lyt.usermanage.dao" targetProject="E:\ConvertBean" type="XMLMAPPER">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 要生成的表 tableName是資料庫中的表名或檢視名 domainObjectName是實體類名-->
<!--無論欄位是什麼型別,生成的類屬性都是varchar -->
<!-- <table schema="btupayprod" tableName="T_INFO_MARKETING_CFG" enableSelectByExample="false"
enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="fasle">
</table> -->
<!-- <columnOverride column="AFTER_AMT" jdbcType="VARCHAR" /> -->
<!-- <table schema="btupayprod" tableName="t_log_online_payment" enableSelectByExample="false"
enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="fasle">
無論欄位是什麼型別,生成的類屬性都是varchar <columnOverride column="AFTER_AMT" jdbcType="VARCHAR"
/> </table> -->
<table schema="test" tableName="user_third" domainObjectName="LytUserThird" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table schema="test" tableName="user_stat" domainObjectName="LytUserStat" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
</table>
<table schema="test" tableName="user_relation" domainObjectName="LytUserRelation" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
具體配置標籤請跳轉:Mybatis Generator詳解
注意:table有多少寫多少,這是個手動活
③反轉生成的兩種方式
方式一:編寫工具類執行
package com.lyt.usermanage.utils;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
public class MyBatisGeneratorUtils {
public static void main(String[] args) {
try {
System.out.println("start generator ...");
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File(MyBatisGeneratorUtils.class.getResource("/mybatis/generator.xml").getFile());
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
System.out.println("end generator!");
} catch (IOException e) {
e.printStackTrace();
} catch (XMLParserException e) {
e.printStackTrace();
} catch (InvalidConfigurationException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
方式二:配置bat檔案執行
執行完成後你就可以到你配置的生成檔案儲存路徑去找檔案了