通常我們設計好數據庫之後,需要在項目中創建對應的實體類,mapper,dao,但是當我們的表數據很大的時候,工作就會變得繁瑣又複雜
Mybatis就爲我們提供了這樣一個操作,根據數據庫爲我們創建實體類,mapper,dao
- Mybatis官方提供了一種名爲“逆向工程”的機制,其可以針對數據庫中的表單自動生成Mybatis需要的代碼
- 包括:java實體類,Mapper映射配置,mapper代理接口
具體操作:
1、添加需要的jar包:
2、generatorConfig.xml
在resources下創建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>
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- 是否去除自動生成的代碼中的註釋 true:是 false:否-->
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- 數據庫連接信息:驅動類、連接地址、用戶名、密碼 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/tmall_ssm"
userId="root" password="root">
</jdbcConnection>
<!-- 默認 false,把 JDBC DECIMAL 和 NUMERIC 類型解析爲 Integer
爲 true 時解析爲 java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- targetProject:生成 POJO 類的位置 -->
<javaModelGenerator targetPackage="cn.wmyskxz.pojo" targetProject="src/main/java">
<!-- enableSubPackages:是否讓 schema 作爲包的後綴-->
<property name="enableSubPackages" value="false"/>
<!-- trimStrings:從數據庫返回的值被清理前後的空格 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- targetProject:生成xml映射文件存放位置 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<!-- enableSubPackages:是否讓 schema 作爲包的後綴-->
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- targetProject:生成mapper類存放位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="cn.wmyskxz.mapper" targetProject="src/main/java">
<!-- enableSubPackages:是否讓 schema 作爲包的後綴-->
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--生成對應表及類名
tableName:要生成的表名
domainObjectName:生成後的實例名
enableCountByExample:Count語句中加入where條件查詢,默認爲true開啓
enableUpdateByExample:Update語句中加入where條件查詢,默認爲true開啓
enableDeleteByExample:Delete語句中加入where條件查詢,默認爲true開啓
enableSelectByExample:Select多條語句中加入where條件查詢,默認爲true開啓
selectByExampleQueryId:Select單個對象語句中加入where條件查詢,默認爲true開啓
-->
<table tableName="category" domainObjectName="Category" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="true"
selectByExampleQueryId="false">
<!-- 使用數據庫中實際的字段名作爲生成的實體類的屬性 -->
<property name="useActualColumnNames" value="true"/>
<!-- 使用自增長鍵 -->
<property name="my.isgen.usekeys" value="true"/>
<generatedKey column="id" sqlStatement="JDBC"/>
</table>
<table tableName="property" domainObjectName="Property" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="true"
selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true"/>
<property name="my.isgen.usekeys" value="true"/>
<generatedKey column="id" sqlStatement="JDBC"/>
</table>
<table tableName="product" domainObjectName="Product" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="true" selectByExampleQueryId="false">
<property name="my.isgen.usekeys" value="true"/>
<property name="useActualColumnNames" value="true"/>
<generatedKey column="id" sqlStatement="JDBC"/>
</table>
<table tableName="product_image" domainObjectName="ProductImage" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="true"
selectByExampleQueryId="false">
<property name="my.isgen.usekeys" value="true"/>
<property name="useActualColumnNames" value="true"/>
<generatedKey column="id" sqlStatement="JDBC"/>
</table>
<table tableName="order_" domainObjectName="Order" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="true" selectByExampleQueryId="false">
<property name="my.isgen.usekeys" value="true"/>
<property name="useActualColumnNames" value="true"/>
<generatedKey column="id" sqlStatement="JDBC"/>
</table>
<table tableName="property_value" domainObjectName="PropertyValue" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="true"
selectByExampleQueryId="false">
<property name="my.isgen.usekeys" value="true"/>
<property name="useActualColumnNames" value="true"/>
<generatedKey column="id" sqlStatement="JDBC"/>
</table>
<table tableName="review" domainObjectName="Review" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="true" selectByExampleQueryId="false">
<property name="my.isgen.usekeys" value="true"/>
<property name="useActualColumnNames" value="true"/>
<generatedKey column="id" sqlStatement="JDBC"/>
</table>
<table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="true" selectByExampleQueryId="false">
<property name="my.isgen.usekeys" value="true"/>
<property name="useActualColumnNames" value="true"/>
<generatedKey column="id" sqlStatement="JDBC"/>
</table>
<table tableName="order_item" domainObjectName="OrderItem" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="true"
selectByExampleQueryId="false">
<property name="my.isgen.usekeys" value="true"/>
<property name="useActualColumnNames" value="true"/>
<generatedKey column="id" sqlStatement="JDBC"/>
</table>
<table tableName="referal_link" domainObjectName="ReferalLink" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="true"
selectByExampleQueryId="false">
<property name="my.isgen.usekeys" value="true"/>
<property name="useActualColumnNames" value="true"/>
<generatedKey column="id" sqlStatement="JDBC"/>
</table>
</context>
</generatorConfiguration>
3、逆向文件生成類
在完成配置文件之後,我們需要加載該配置文件,利用逆向工程機制來對數據庫的各個表進行文件生成
創建TestMyBatisGenerator類
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.api.ShellCallback;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* 逆向工程測試類
*/
public class TestMybatisGenerator {
public static void main(String[] args) throws Exception {
// warnings 爲用於放置生成過程中警告信息的集合對象
List<String> warnings = new ArrayList<String>();
// 指定是否覆蓋重名文件
boolean overwrite = true;
// 加載配置文件
File configFile = new File(MyBatisGenerator.class.getClassLoader().getResource("generatorConfig.xml").toURI());
// 配置解析類
ConfigurationParser cp = new ConfigurationParser(warnings);
// 配置解析類解析配置文件並生成 Configuration 配置對象
Configuration config = cp.parseConfiguration(configFile);
// ShellCallback 負責如何處理重複文件
ShellCallback callback = new DefaultShellCallback(overwrite);
// 逆向工程對象
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
// 執行逆向文件生成操作
myBatisGenerator.generate(null);
// 打印提示信息
System.out.println("MyBatis 逆向工程執行成功,刷新項目查看文件!");
}
}
之後就可以看到我們生成的文件,與數據庫表單列屬性可以進行對比
參考:我沒有三顆心臟