簡介
Mybatis Generator(MBG),下面我們統稱為MBG,是一個Mybatis和iBatis的代碼生成器。他可以内省資料庫的表(或多個表)然後生成可以用來通路(多個)表的基礎對象。這樣減少了項目建立時各種配置對象,配置檔案和資料庫互動的麻煩。 MBG的解決了一些資料庫中比較重要的操作,如CRUD(插入,查詢,更新,删除)。
有關Mybatis具體生成事項,可以參考
Mybatis Generator官方文檔Mybatis MBG設計用于開發環境中的互動,可以用在Ant 任務,Maven插件,持續內建環境。有關Mybatis的一些注意事項包括如下:
- MBG 會自動合并已經存在并且和新生成的檔案重名的 XML。MBG 不會覆寫您對已經生成xml所做的修改。 您可以反複的運作而不必擔心失去您自定義的更改。 Mybatis Gnerator會更新上次運作生成的元素。
- MBG 不會合并 Java 檔案,如果你對一些MBG生成的檔案做了修改,再次生成的時候, 您可以手動合并這些更改。 當您使用Eclipse 插件時, MBG 可以自動合并 Java 檔案.
快速入門
概念
使用MBG的基本步驟
1、建立和補全Mybatis MBG的配置檔案,你至少要指定以下内容
- <jdbcConnection>素,指定如何連接配接資料庫
- <JavaModelGenerator>,java模型對象生成位置
- <SqlMapGenerator>,SQL映射檔案位置
- 可選的,<javaClientGenerator>,java用戶端接口和類檔案位置
- 至少一個<table>元素
2、把配置檔案儲存在友善的位置
3、運作MBG配置檔案,可以通過Ant,Maven,Java代碼等
4、修改Mybatis的一些配置,以便自己能夠使用MBG生成的代碼
建立項目
1、借用原來的之前的
Mybatis入門教程,我們建立me.aihe.testdao包,具體結構如下。

項目結構
2、建立MBG配置檔案,如果使用Idea內建開發環境,可下載下傳Mybatis plugin,省了不少功夫,極大的友善了我們的操作。
建立MBG配置檔案
3、修改MBG配置檔案内容如下
<?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>
<!-- !!!! Driver Class Path !!!! -->
<classPathEntry location="/Users/aihe/.m2/repository/mysql/mysql-connector-java/5.1.38/mysql-connector-java-5.1.38.jar" />
<!--<properties resource="classpa"-->
<context id="context" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="false"/>
<property name="suppressDate" value="true"/>
</commentGenerator>
<!-- !!!! Database Configurations !!!! -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test" userId="aihe" password="123456"/>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- !!!! Model Configurations !!!! -->
<javaModelGenerator targetPackage="me.aihe.testdao" targetProject="/Users/aihe/IdeaProjects/MybatisCook/src/main/java">
<property name="enableSubPackages" value="false"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- !!!! Mapper XML Configurations !!!! -->
<sqlMapGenerator targetPackage="me.aihe.testdao" targetProject="/Users/aihe/IdeaProjects/MybatisCook/src/main/resources">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- !!!! Mapper Interface Configurations !!!! -->
<javaClientGenerator targetPackage="me.aihe.testdao" targetProject="/Users/aihe/IdeaProjects/MybatisCook/src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- !!!! Table Configurations !!!! -->
<table tableName="Test" enableCountByExample="true" enableDeleteByExample="true" enableSelectByExample="true"
enableUpdateByExample="true"/>
</context>
</generatorConfiguration>
注意:在<commentGenerator>裡面有個suppressAllComments的屬性,如果填寫為true的話,所有生成的模型檔案雖然會沒有注釋,但是Mapper.xml不會覆寫,而是追加在後面,會導緻運作出錯。建議設定為false
4、運作MBG
運作方式有很多種,基于Ant Task,Maven 插件,Java程式等,這裡我們使用Maven Plugin。
主意:建議大家下載下傳Idea的Maven Helper插件,友善了很多maven的操作。
配置Pom.xml檔案,添加MBG插件
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/mybatis-generator.xml</configurationFile>
</configuration>
</plugin>
</plugins>
</build>
運作Maven插件
5、檢視結果
生成結果
6、測試生成的代碼
我們的資料庫内容如下
Test表内容
測試程式
@Test
public void test11(){
InputStream inputStream = null;
SqlSession sqlSession = null;
try {
inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory mSqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
sqlSession = mSqlSessionFactory.openSession();
TestMapper testMapper = sqlSession.getMapper(TestMapper.class);
TestExample testExample = new TestExample();
TestExample.Criteria criteria = testExample.createCriteria();
criteria.andContentLike("%内容%");
List<me.aihe.dao.Test> testList = testMapper.selectByExample(testExample);
System.out.println(testList);
// Good good = goodMapper.getGoodAndCouponMap2(1);
// System.out.println(good);
sqlSession.commit();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
運作結果如下:
運作結果
插件
Mybaitis Generator有一些官方的插件,可以更好的定制生成的檔案内容。
//緩存插件,生成的Mapper.xml檔案中添加緩存配置
<plugin type="org.mybatis.generator.plugins.CachePlugin"></plugin>
//生成的Model檔案,加上toString方法
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"</plugin>
//生成的Model檔案實作Serializable接口
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
//虛拟主鍵插件..
<plugin type="org.mybatis.generator.plugins.VirtualPrimaryKeyPlugin"></plugin>
還有一些額外的插件,可以用途比較少,在這裡就先不提到了。
總結
本文主要示範了一種通過Maven 插件來使用MBG生成JavaBean,Mapper檔案的案例。最後測試了生成代碼的可用性。Mybatis Generator确實友善了很多我們的工作。
小提示
如果想要生成Example類,記得在Context标簽上的targetRuntime為Mybatis3。