Mybatis屬于半自動ORM,在使用這個框架中,工作量最大的就是書寫Mapping的映射文件,由于手動書寫很容易出錯,我們可以利用Mybatis-Generator來幫我們自動生成文件。
1、相關(guān)文件
關(guān)于Mybatis-Generator的下載可以到這個地址:下載地址
由于我使用的是Mysql數(shù)據(jù)庫,這里需要在準(zhǔn)備一個連接mysql數(shù)據(jù)庫的驅(qū)動jar包

%B_3$))L52787QH(5`79WPB.png
和Hibernate逆向生成一樣,這里也需要一個配置文件:
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>
<!-- 數(shù)據(jù)庫驅(qū)動-->
<classPathEntry location="mysql-connector-java-5.1.25-bin.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自動生成的注釋 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--數(shù)據(jù)庫鏈接URL,用戶名、密碼 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/seckill" userId="root" password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="test.domain" targetProject="src">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="test.mapping" targetProject="src">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="test.IDao" targetProject="src">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成的表 tableName是數(shù)據(jù)庫中的表名或視圖名 domainObjectName是實(shí)體類名-->
<table tableName="seckill" domainObjectName="seckill" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
需要修改文件配置的地方我都已經(jīng)把注釋標(biāo)注出來了,這里的相關(guān)路徑(如數(shù)據(jù)庫驅(qū)動包,生成對應(yīng)的相關(guān)文件位置可以自定義)不能帶有中文。
上面配置文件中的:
<!-- 要生成的表 tableName是數(shù)據(jù)庫中的表名或視圖名 domainObjectName是實(shí)體類名-->
<table tableName="seckill" domainObjectName="seckill" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
生成語句文件:
java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite
最后:在命令窗口上執(zhí)行生成語句的命令,如下圖:

image.png
看到MyBatis Generator finished successfully.說明執(zhí)行成功。
二、在idea中自動生成代碼
1.在pom.xml文件中引入MyBatis Generator的插件
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
2.在項(xiàng)目中引入generatorConfig.xml文件
如圖:
8RSU8I0T$L58YI{B7FGY{5T.png
3.確認(rèn)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>
<!-- 數(shù)據(jù)庫驅(qū)動-->
<classPathEntry location="D:/mybatis-generator-core-1.3.2/lib/mysql-connector-java-5.1.25-bin.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自動生成的注釋 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--數(shù)據(jù)庫鏈接URL,用戶名、密碼 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test" userId="root" password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="com.ygf.domain" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.ygf.IDao" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成的表 tableName是數(shù)據(jù)庫中的表名或視圖名 domainObjectName是實(shí)體類名-->
<table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
4.生成代碼
雙擊插件就可以生成代碼如下圖:

@2}J(DWPBT`6(77L}$YS%GR.png
注意:報(bào)錯Result Maps collection already contains value for ***
多次生成同一張表的代碼后,mapper對應(yīng)的xml文件,不會自動覆蓋原來生成的xml文件,會在生成的xml文件中添加下一次生成的xml文件,需要把上次生成的xml文件先刪除,在生成就不會報(bào)錯了