經(jīng)過(guò)周末的休息,Damon也是重新更新了纱注。在我們做項(xiàng)目中畏浆,關(guān)于持久層的生成相信是許多小伙伴頭疼的事情。并且mybatis是目前很流行的持久層框架狞贱,因此有沒(méi)有一種辦法能夠減少我們的開(kāi)發(fā)時(shí)間量全度。
對(duì)于上面的疑問(wèn)自然是有的,那就是逆向工程斥滤。對(duì)于逆向工程這個(gè)概念以及方法其實(shí)已經(jīng)出現(xiàn)很長(zhǎng)一段時(shí)間将鸵,其存在的目的就是為了我們的開(kāi)發(fā)時(shí)間。所謂mybatis逆向工程佑颇,就是mybatis會(huì)根據(jù)我們?cè)O(shè)計(jì)好的數(shù)據(jù)表顶掉,自動(dòng)生成pojo、mapper以及mapper.xml挑胸。不用通過(guò)自己編寫(xiě)痒筒,依靠插件為我們生成,從而大大減少我們的工作量。接下來(lái)就讓Damon來(lái)跟大家說(shuō)說(shuō)如何操作簿透。
首先從插件的安裝上(資料可以在文章末尾的Github下載):
-
將features移袍、plugins拷貝到myeclipse10下的dropins文件夾中,如下圖所示老充,重啟myeclipse葡盗。
image
-以下以MySQL為例逆向生成映射文件,其中數(shù)據(jù)庫(kù)為test表為userinfo啡浊,id字段自增觅够。
創(chuàng)建一新的web project mybatis_generator,并添加sql驅(qū)動(dòng)jar包巷嚣。
image -
在src中創(chuàng)建一個(gè)MyBatis生成orm文件的配置文件喘先,單擊next,保持默認(rèn)設(shè)置finish即可廷粒。
image - 對(duì)生成的generatorConfig.xml文件進(jìn)行更改窘拯。
<?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 >
<context id="mybatis_generator" >
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/test"
userId="root" password="root" />
<javaModelGenerator targetPackage="orm" targetProject="mybatis_generator" />
<sqlMapGenerator targetPackage="orm" targetProject="mybatis_generator" />
<javaClientGenerator targetPackage="orm" targetProject="mybatis_generator"
type="XMLMAPPER" />
<table schema="test" tableName="userinfo" >
<generatedKey column="id" sqlStatement="mysql" identity="true" />
</table>
</context>
</generatorConfiguration>
- 注:context id、targetProject均為項(xiàng)目名稱坝茎,generatorKey標(biāo)簽配置id字段自增涤姊。
-
右鍵配置文件,選擇Generator MyBatis/iBATIS Artifacts即可生成orm映射文件
![avatar]
image - 如果出現(xiàn)錯(cuò)誤:Unexpected error while running MyBatis Generator. Exception getting JDBC Driver
- 就把mysql-connector-java-5.0.5-bin.jar添加到Referenced Libraries中景东。
接下來(lái)就說(shuō)說(shuō)生成的mapper接口方法的解析,大家好好理解下奔誓。
- mapper接口中的函數(shù)及方法
一:方法功能說(shuō)明
- int countByExample(UserExample example) thorws SQLException 按條件計(jì)數(shù)
- int deleteByPrimaryKey(Integer id) thorws SQLException 按主鍵刪除
- int deleteByExample(UserExample example) thorws SQLException 按條件查詢
- String/Integer insert(User record) thorws SQLException 插入數(shù)據(jù)(返回值為ID)
- User selectByPrimaryKey(Integer id) thorws SQLException 按主鍵查詢
- ListselectByExample(UserExample example) thorws SQLException 按條件查詢
- ListselectByExampleWithBLOGs(UserExample example) thorws SQLException 按條件查詢(包括BLOB字段)斤吐。只有當(dāng)數(shù)據(jù)表中的字段類型有為二進(jìn)制的才會(huì)產(chǎn)生。
- int updateByPrimaryKey(User record) thorws SQLException 按主鍵更新
- int updateByPrimaryKeySelective(User record) thorws SQLException 按主鍵更新值不為null的字段
- int updateByExample(User record, UserExample example) thorws SQLException 按條件更新
- int updateByExampleSelective(User record, UserExample example) thorws SQLException 按條件更新值不為null的字段
二:example實(shí)例解析
- mybatis的逆向工程中會(huì)生成實(shí)例及實(shí)例對(duì)應(yīng)的example厨喂,example用于添加條件和措,相當(dāng)where后面的部分
- xxxExample example = new xxxExample();
Criteria criteria = new Example().createCriteria();
- 方法 說(shuō)明
- example.setOrderByClause(“字段名 ASC”); 添加升序排列條件,DESC為降序
- example.setDistinct(false) 去除重復(fù)蜕煌,boolean型派阱,true為選擇不重復(fù)的記錄。
- criteria.andXxxIsNull 添加字段xxx為null的條件
- criteria.andXxxIsNotNull 添加字段xxx不為null的條件
- criteria.andXxxEqualTo(value) 添加xxx字段等于value條件
- criteria.andXxxNotEqualTo(value) 添加xxx字段不等于value條件
- criteria.andXxxGreaterThan(value) 添加xxx字段大于value條件
- criteria.andXxxGreaterThanOrEqualTo(value) 添加xxx字段大于等于value條件
- criteria.andXxxLessThan(value) 添加xxx字段小于value條件
- criteria.andXxxLessThanOrEqualTo(value) 添加xxx字段小于等于value條件
- criteria.andXxxIn(List<斜纪?>) 添加xxx字段值在List<贫母?>條件
- criteria.andXxxNotIn(List<?>) 添加xxx字段值不在List<盒刚?>條件
- criteria.andXxxLike(“%”+value+”%”) 添加xxx字段值為value的模糊查詢條件
- criteria.andXxxNotLike(“%”+value+”%”) 添加xxx字段值不為value的模糊查詢條件
- criteria.andXxxBetween(value1,value2) 添加xxx字段值在value1和value2之間條件
- criteria.andXxxNotBetween(value1,value2) 添加xxx字段值不在value1和value2之間條件
三:應(yīng)用舉例
1.查詢
① selectByPrimaryKey()
User user = XxxMapper.selectByPrimaryKey(100); //相當(dāng)于select * from user where id = 100
② selectByExample() 和 selectByExampleWithBLOGs()
UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("wyw");
criteria.andUsernameIsNull();
example.setOrderByClause("username asc,email desc");
List<?>list = XxxMapper.selectByExample(example);
//相當(dāng)于:select * from user where username = 'wyw' and username is null order by username asc,email desc注:在iBator逆向工程生成的文件XxxExample.Java中包含一個(gè)static的內(nèi)部類Criteria腺劣,Criteria中的方法是定義SQL 語(yǔ)句where后的查詢條件。
- 2.插入數(shù)據(jù)
①insert()
User user = new User();
user.setId("dsfgsdfgdsfgds");
user.setUsername("admin");
user.setPassword("admin")
user.setEmail("wyw@163.com");
XxxMapper.insert(user);
//相當(dāng)于:insert into user(ID,username,password,email) values ('dsfgsdfgdsfgds','admin','admin','wyw@126.com');
- 3.更新數(shù)據(jù)
①u(mài)pdateByPrimaryKey()
User user =new User();
user.setId("dsfgsdfgdsfgds");
user.setUsername("wyw");
user.setPassword("wyw");
user.setEmail("wyw@163.com");
XxxMapper.updateByPrimaryKey(user);
//相當(dāng)于:update user set username='wyw', password='wyw', email='wyw@163.com' where id='dsfgsdfgdsfgds'
②updateByPrimaryKeySelective()
User user = new User();
user.setId("dsfgsdfgdsfgds");
user.setPassword("wyw");
XxxMapper.updateByPrimaryKey(user);
//相當(dāng)于:update user set password='wyw' where id='dsfgsdfgdsfgds'
③ updateByExample() 和 updateByExampleSelective()
UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("admin");
User user = new User();
user.setPassword("wyw");
XxxMapper.updateByPrimaryKeySelective(user,example);
//相當(dāng)于:update user set password='wyw' where username='admin'
updateByExample()更新所有的字段因块,包括字段為null的也更新橘原,建議使用 updateByExampleSelective()更新想更新的字段
4.刪除數(shù)據(jù)
①deleteByPrimaryKey()
XxxMapper.deleteByPrimaryKey(1); //相當(dāng)于:delete from user where id=1
②deleteByExample()
UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("admin");
XxxMapper.deleteByExample(example);
//相當(dāng)于:delete from user where username='admin'
數(shù)量①countByExample()
UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("wyw");
int count = XxxMapper.countByExample(example);
//相當(dāng)于:select count(*) from user where username='wyw'