您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān) generator如何在mybatis中使用,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
引言:
最近的一個項目,由于數(shù)據(jù)庫表巨多,導(dǎo)致需要創(chuàng)建N多個java實體、dao、mapper.xml映射文件,如果均使用純手工編寫,無疑需要耗費大量時間和精力。于是上網(wǎng)學(xué)習(xí)了mybatis generator的使用。
現(xiàn)在項目寫完了,閑暇之余把干貨奉上,供大家直接使用。
需求場景:
當(dāng)你的java 項目數(shù)據(jù)庫有N張表需要使用mybatis進(jìn)行數(shù)據(jù)庫操作時,建議使用mybatis generator 自動生成工具??梢宰詣訋椭闵蒵ava實體類、dao、mapper.xml等。
首先給大家分享我自己封裝好的mybatis generator代碼自動生成項目,里面集成了中文注釋、mysql的limit分頁功能。
此外需要注意需要重新引入一下jar文件夾中的mybatis-generator-plugin-1.0.0.jar,如圖:
最終目錄結(jié)構(gòu)如下:
接下來,請打開配置文件,如圖:
(關(guān)于generatorConfig.xml的具體教程可參見:http://blog.csdn.net/isea533/article/details/42102297)
接下來,打開generatorConfig.xml,根據(jù)你自己的需求,改變?nèi)缦屡渲茫?/p>
首先,修改數(shù)據(jù)庫連接地址。
期次,聲明本次需要操作的表及為即將生成的實體類命名。
再次,設(shè)置實體文件、dao、mapper.xml生成的路徑。
最后,運行StartUp.java
的main方法執(zhí)行生成操作。
mysql中本地數(shù)據(jù)庫表為
CREATE TABLE `student` (
`id` varchar(50) NOT NULL COMMENT '主鍵',
`name` varchar(10) DEFAULT NULL COMMENT '姓名',
`gender` int(2) DEFAULT NULL COMMENT '性別1男2女',
`disc` longtext COMMENT '大文本描述',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
對照表,我們看一下生成的包和文件:
其中Student.java文件當(dāng)然就是數(shù)據(jù)庫表實體類,對應(yīng)表的相關(guān)字段。
下面,在我們的項目中導(dǎo)入生成的相關(guān)文件,如下:
打開Student.java 我們可以發(fā)現(xiàn)字段已經(jīng)生成了中文注釋;
打開StudentMapper.xml可以發(fā)現(xiàn)已經(jīng)可以使用mysql的limit分頁;
在配置好mybatis的數(shù)據(jù)庫連接后(mybatis相關(guān)配置請自行baidu,本文終點介紹mybatis generator的使用),我們開始數(shù)據(jù)庫的相關(guān)操作:
打開: testMybatis.java
在此,我主要講幾個容易出錯的方法和區(qū)別:
1.selectByExample和selectByExampleWithBLOBs的區(qū)別(包含Example的使用)
@Test public void testQueryStudentExample() { SqlSession sqlSession = sqlSessionFactory.openSession(false); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); try { //分頁查詢性別為男、并且名稱中包含z的記錄,第一頁,每頁3條記錄,按性別排序 StudentExample studentExample=new StudentExample(); studentExample.or().andGenderEqualTo(1).andNameLike("%z%"); studentExample.setOffset(0); studentExample.setLimit(3); studentExample.setOrderByClause("GENDER DESC"); List<Student> list1 = studentMapper.selectByExample(studentExample); List<Student> list2 = studentMapper.selectByExampleWithBLOBs(studentExample); System.out.println(list1.get(0).getDisc()); System.out.println(list2.get(0).getDisc()); } catch(Exception e){ e.printStackTrace(); sqlSession.rollback(); }finally { sqlSession.close(); } }
結(jié)果:
原因:
由于student表中,disc字段類型為longtext,故如果想要搜索結(jié)果包含大字段類型,則必須使用selectByExampleWithBLOBs。無需檢索大字段,則使用selectByExample;
2.insertSelective和insert的區(qū)別
當(dāng)有部分字段未設(shè)值時,使用insertSelective:
<SPAN >@Test public void testInsertStudent() { SqlSession sqlSession = sqlSessionFactory.openSession(false); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); try { Student s=new Student(); s.setId(java.util.UUID.randomUUID().toString().replaceAll("\\-", "")); s.setName("zjt"); s.setGender(1); s.setDisc("MyBatis Generator 真心好用"); studentMapper.insertSelective(s); sqlSession.commit(); } catch(Exception e){ e.printStackTrace(); sqlSession.rollback(); }finally { sqlSession.close(); } } </SPAN>
結(jié)果:
當(dāng)有所有字段均已設(shè)值時,使用insert;
<SPAN >@Test public void testInsertStudent() { SqlSession sqlSession = sqlSessionFactory.openSession(false); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); try { Student s=new Student(); s.setId(java.util.UUID.randomUUID().toString().replaceAll("\\-", "")); s.setName("zjt"); s.setGender(1); s.setDisc("MyBatis Generator 真心好用"); studentMapper.insertSelective(s); sqlSession.commit(); } catch(Exception e){ e.printStackTrace(); sqlSession.rollback(); }finally { sqlSession.close(); } } </SPAN>
結(jié)果:
3.修改操作
updateByExample
如果example定義了兩個字段,數(shù)據(jù)庫共4個字段,則修改數(shù)據(jù)庫的兩個字段,其余兩個字段改為null;
updateByExampleSelective
如果example定義了兩個字段,數(shù)據(jù)庫共4個字段,則修改數(shù)據(jù)庫的兩個字段,其余兩個字段不動;
updateByExampleWithBLOBs
和updateByExample相比此方法可以修改大字段類型,其余性質(zhì)和updateByExample相同
updateByPrimaryKey
如果record定義了兩個字段,其中有一個字段是主鍵,數(shù)據(jù)庫共4個字段,則根據(jù)主鍵修改數(shù)據(jù)庫的兩個字段,其余兩個字段改為null;
updateByPrimaryKeySelective
如果record定義了兩個字段,其中有一個字段是主鍵,數(shù)據(jù)庫共4個字段,則根據(jù)主鍵修改數(shù)據(jù)庫的兩個字段,其余兩個字段不動;
updateByPrimaryKeyWithBLOBs
和updateByPrimaryKey相比此方法可以修改大字段類型,其余性質(zhì)和updateByPrimaryKey相同
上述就是小編為大家分享的 generator如何在mybatis中使用了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。