日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Mybatis-Generator(MBG)教程与Idea的MBG插件

發(fā)布時間:2023/12/4 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Mybatis-Generator(MBG)教程与Idea的MBG插件 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

簡介

Mybatis Generator(MBG),下面我們統(tǒng)稱為MBG,是一個Mybatis和iBatis的代碼生成器。他可以內省數(shù)據(jù)庫的表(或多個表)然后生成可以用來訪問(多個)表的基礎對象。這樣減少了項目新建時各種配置對象,配置文件和數(shù)據(jù)庫交互的麻煩。 MBG的解決了一些數(shù)據(jù)庫中比較重要的操作,如CRUD(插入,查詢,更新,刪除)。

有關Mybatis具體生成事項,可以參考Mybatis Generator官方文檔
Mybatis MBG設計用于開發(fā)環(huán)境中的交互,可以用在Ant 任務,Maven插件,持續(xù)集成環(huán)境。有關Mybatis的一些注意事項包括如下:

  • MBG 會自動合并已經存在并且和新生成的文件重名的 XML。MBG 不會覆蓋您對已經生成xml所做的修改。 您可以反復的運行而不必擔心失去您自定義的更改。 Mybatis Gnerator會更新上次運行生成的元素。
  • MBG 不會合并 Java 文件,如果你對一些MBG生成的文件做了修改,再次生成的時候, 您可以手動合并這些更改。 當您使用Eclipse 插件時, MBG 可以自動合并 Java 文件.

快速入門

概念

使用MBG的基本步驟
1、創(chuàng)建和補全Mybatis MBG的配置文件,你至少要指定以下內容

  • <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包,具體結構如下。

項目結構

2、創(chuàng)建MBG配置文件,如果使用Idea集成開發(fā)環(huán)境,可下載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、測試生成的代碼
我們的數(shù)據(jù)庫內容如下

Test表內容

測試程序

@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文件實現(xiàn)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插件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。