簡介
Mybatis Generator(MBG)噪叙,下面我們統(tǒng)稱為MBG,是一個Mybatis和iBatis的代碼生成器琳钉。他可以內(nèi)省數(shù)據(jù)庫的表(或多個表)然后生成可以用來訪問(多個)表的基礎(chǔ)對象似枕。這樣減少了項目新建時各種配置對象求厕,配置文件和數(shù)據(jù)庫交互的麻煩戈抄。 MBG的解決了一些數(shù)據(jù)庫中比較重要的操作离唬,如CRUD(插入,查詢划鸽,更新输莺,刪除)。
有關(guān)Mybatis具體生成事項裸诽,可以參考Mybatis Generator官方文檔
Mybatis MBG設(shè)計用于開發(fā)環(huán)境中的交互嫂用,可以用在Ant 任務(wù),Maven插件丈冬,持續(xù)集成環(huán)境尸折。有關(guān)Mybatis的一些注意事項包括如下:
- MBG 會自動合并已經(jīng)存在并且和新生成的文件重名的 XML。MBG 不會覆蓋您對已經(jīng)生成xml所做的修改殷蛇。 您可以反復(fù)的運行而不必擔(dān)心失去您自定義的更改实夹。 Mybatis Gnerator會更新上次運行生成的元素。
- MBG 不會合并 Java 文件,如果你對一些MBG生成的文件做了修改粒梦,再次生成的時候亮航, 您可以手動合并這些更改。 當(dāng)您使用Eclipse 插件時, MBG 可以自動合并 Java 文件.
快速入門
概念
使用MBG的基本步驟
1匀们、創(chuàng)建和補全Mybatis MBG的配置文件缴淋,你至少要指定以下內(nèi)容
- <jdbcConnection>素,指定如何連接數(shù)據(jù)庫
- <JavaModelGenerator>泄朴,java模型對象生成位置
- <SqlMapGenerator>重抖,SQL映射文件位置
- 可選的,<javaClientGenerator>祖灰,java客戶端接口和類文件位置
- 至少一個<table>元素
2钟沛、把配置文件保存在方便的位置
3、運行MBG配置文件局扶,可以通過Ant恨统,Maven,Java代碼等
4三妈、修改Mybatis的一些配置畜埋,以便自己能夠使用MBG生成的代碼
創(chuàng)建項目
1、借用原來的之前的Mybatis入門教程畴蒲,我們創(chuàng)建me.aihe.testdao包悠鞍,具體結(jié)構(gòu)如下。
2模燥、創(chuàng)建MBG配置文件咖祭,如果使用Idea集成開發(fā)環(huán)境掩宜,可下載Mybatis plugin,省了不少功夫心肪,極大的方便了我們的操作锭亏。
3纠吴、修改MBG配置文件內(nèi)容如下
<?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不會覆蓋固该,而是追加在后面,會導(dǎo)致運行出錯糖儡。建議設(shè)置為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>
5埠褪、查看結(jié)果
6、測試生成的代碼
我們的數(shù)據(jù)庫內(nèi)容如下
測試程序
@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("%內(nèi)容%");
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();
}
}
}
運行結(jié)果如下:
插件
Mybaitis Generator有一些官方的插件挤庇,可以更好的定制生成的文件內(nèi)容钞速。
//緩存插件,生成的Mapper.xml文件中添加緩存配置
<plugin type="org.mybatis.generator.plugins.CachePlugin"></plugin>
//生成的Model文件嫡秕,加上toString方法
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"</plugin>
//生成的Model文件實現(xiàn)Serializable接口
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
//虛擬主鍵插件..
<plugin type="org.mybatis.generator.plugins.VirtualPrimaryKeyPlugin"></plugin>
還有一些額外的插件渴语,可以用途比較少,在這里就先不提到了昆咽。
總結(jié)
本文主要演示了一種通過Maven 插件來使用MBG生成JavaBean遵班,Mapper文件的案例。最后測試了生成代碼的可用性潮改。Mybatis Generator確實方便了很多我們的工作狭郑。
小提示
如果想要生成Example類,記得在Context標簽上的targetRuntime為Mybatis3汇在。