Mybatis-Generator(MBG)教程与Idea的MBG插件
簡介
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插件
5、查看結果
生成結果 生成結果6、測試生成的代碼
我們的數據庫內容如下
測試程序
@Testpublic 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。
參考
- Mybatis入門教程
- Mybatis Generator文檔
總結
以上是生活随笔為你收集整理的Mybatis-Generator(MBG)教程与Idea的MBG插件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《vSphere性能设计:性能密集场景下
- 下一篇: JavaFX 一 出生新手村(