在最近的實習生學習中,漸漸使用到了通用Mapper,但是之前根本就沒接觸過。其實同效果的SQL不難,但是改成相應的example難免有些不熟悉,在這裏收集一些方法的使用.
一、mapper接口中的方法解析
mapper接口中的函數及方法
方法 | 功能說明 |
---|---|
int countByExample(UserExample example) thorws SQLException | 按條件計數 |
int deleteByPrimaryKey(Integer id) thorws SQLException | 按主鍵刪除 |
int deleteByExample(UserExample example) thorws SQLException | 按條件查詢 |
String/Integer insert(User record) thorws SQLException | 插入數據(返回值爲ID) |
User selectByPrimaryKey(Integer id) thorws SQLException | 按主鍵查詢 |
ListselectByExample(UserExample example) thorws SQLException | 按條件查詢 |
ListselectByExampleWithBLOGs(UserExample example) thorws SQLException | 按條件查詢(包括BLOB字段)。只有當數據表中的字段類型有爲二進制的纔會產生。 |
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實例
mybatis的逆向工程中會生成實例及實例對應的example,example用於添加條件,相當where後面的部分.
Example爲我們創建的實例 Example.createCriteria()爲我們創建了條件容器,然後將我們的篩選條件一一添加到裏面,最後用這個實例去查詢.
Example example = new xxxExample(entity.class);
Criteria criteria = new Example().createCriteria();
方法 | 說明 |
---|---|
example.setOrderByClause(“字段名 ASC”) | 添加升序排列條件,DESC爲降序 |
example.setDistinct(false) | 去除重複,boolean型,true爲選擇不重複的記錄。 |
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的模糊查詢條件 |
三、應用舉例
1.查詢
① selectByPrimaryKey()
User user = XxxMapper.selectByPrimaryKey(100); //相當於select * from user where id = 100
② selectByExample() 和 selectByExampleWithBLOGs()
Example example = new Example(entity.class);
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("wyw");
criteria.andUsernameIsNull();
example.setOrderByClause("username asc,email desc");
List<?>list = XxxMapper.selectByExample(example);
//相當於:select * from user where username = 'wyw' and username is null order by username asc,email desc
注:在iBator逆向工程生成的文件XxxExample.java中包含一個static的內部類Criteria,Criteria中的方法是定義SQL 語句where後的查詢條件。
2.插入數據
①insert()
User user = new User();
user.setId("dsfgsdfgdsfgds");
user.setUsername("admin");
user.setPassword("admin")
user.setEmail("[email protected]");
XxxMapper.insert(user);
//相當於:insert into user(ID,username,password,email) values ('dsfgsdfgdsfgds','admin','admin','[email protected]');
3.更新數據
①updateByPrimaryKey()
User user =new User();
user.setId("dsfgsdfgdsfgds");
user.setUsername("wyw");
user.setPassword("wyw");
user.setEmail("[email protected]");
XxxMapper.updateByPrimaryKey(user);
//相當於:update user set username='wyw', password='wyw', email='[email protected]' where id='dsfgsdfgdsfgds'
②updateByPrimaryKeySelective()
User user = new User();
user.setId("dsfgsdfgdsfgds");
user.setPassword("wyw");
XxxMapper.updateByPrimaryKey(user);
//相當於:update user set password='wyw' where id='dsfgsdfgdsfgds'
③ updateByExample() 和 updateByExampleSelective()
Example example = new Example(entity.class);
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("admin");
User user = new User();
user.setPassword("wyw");
XxxMapper.updateByPrimaryKeySelective(user,example);
//相當於:update user set password='wyw' where username='admin'
updateByExample()更新所有的字段,包括字段爲null的也更新,建議使用 updateByExampleSelective()更新想更新的字段
4.刪除數據
①deleteByPrimaryKey()
XxxMapper.deleteByPrimaryKey(1); //相當於:delete from user where id=1
②deleteByExample()
Example example = new Example(entity.class);
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("admin");
XxxMapper.deleteByExample(example);
//相當於:delete from user where username='admin'
5.查詢數據數量
①countByExample()
Example example = new Example(entity.class);
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("wyw");
int count = XxxMapper.countByExample(example);
//相當於:select count(*) from user where username='wyw'
四、實操實例
1.多條件排序
根據多條件排序直接就在後續添加排序字段(Entity是我的DO實體類)
Example example = new Example(Affiche.class);
Example.Criteria criteria = example.createCriteria();
example.orderBy(DataConstants.SHOW_TIME).desc();
example.orderBy(DataConstants.ID).desc();
2.or條件
or條件的查詢,必須建立兩個criteria對象,然後出了or條件字段,其他查詢條件字段必須相同,這裏是我的實例就不改了,DataConstants類存放的是我的常用常量字段,DbConstants和NumConstans是存放用的String類型和Integer類型的常用值。
Example example = new Example(Affiche.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo(DataConstants.AFFICHE_TYPE,type)
.andEqualTo(DataConstants.INSTITUTION_ID,institutionId)
.andEqualTo(DataConstants.DELETE_TIME,DbConstants.NO_DEL_VAL)
.andEqualTo(DataConstants.IF_DISPLAY,NumConstants.IS_DISPLAY)
.andEqualTo(DataConstants.SHOW_STATUS,NumConstants.IS_PUBLISHED)
.andLessThanOrEqualTo(DataConstants.SHOW_TIME,new Date());
if(StringUtils.isNotBlank(keyWord)){
criteria.andLike(DataConstants.AFFICHE_TITLE,keyWord);
}
Example.Criteria criteria1 = example.createCriteria();
criteria1.andEqualTo(DataConstants.AFFICHE_TYPE,type)
.andEqualTo(DataConstants.INSTITUTION_ID,institutionId)
.andEqualTo(DataConstants.DELETE_TIME,DbConstants.NO_DEL_VAL)
.andEqualTo(DataConstants.IF_DISPLAY,NumConstants.IS_DISPLAY)
.andEqualTo(DataConstants.SHOW_STATUS,NumConstants.IS_PUBLISHED)
.andLessThanOrEqualTo(DataConstants.SHOW_TIME,new Date());
if(StringUtils.isNotBlank(keyWord)){
criteria1.andLike(DataConstants.AFFICHE_CONTENT,keyWord);
}
example.or(criteria1);
List<Affiche> afficheList = afficheMapper.selectByExample(example);
3.IN條件
in條件入參可以是集合形式,如下的List ids具體的實現方法,目前只會用分析不了原理
Example example = new Example(Affiche.class);
example.createCriteria().andEqualTo(DbConstants.DELETE_TIME, DbConstants.NO_DEL_VAL)
.andEqualTo(DataConstants.INSTITUTION_ID, institutionId)
.andIn(DataConstants.ID, ids);
五、拓展
Criteria類
Criteria 內部類的每個屬性都包含 andXXX 方法,以及如下的標準的SQL查詢方法:
IS NULL - 指相關的列必須爲NULL
IS NOT NULL - 指相關的列必須不爲NULL
= (equal) - 指相關的列必須等於方法參數中的值
<> (not equal) - 指相關的列必須不等於方法參數中的值
(greater than) - 指相關的列必須大於方法參數中的值
= (greater than or equal) - 指相關的列必須大於等於方法參數中的值
< (less than) - 指相關的列必須小於於方法參數中的值
<= (less than or equal) - 指相關的列必須小於等於方法參數中的值
LIKE - 指相關的列必須 “like” 方法參數中的值. 這個方法不用必須加入 ‘%’, 您必須設置方法參數中的值.
NOT LIKE - 指相關的列必須 “not like” 方法參數中的值. 這個方法不用必須加入 ‘%’, 您必須設置方法參數中的值.
BETWEEN - 指相關的列必須在 “between” 方法參數中的兩個值之間.
NOT BETWEEN - 指相關的列必須不在 “not between” 方法參數中的兩個值之間.
IN - 指相關的列必須在傳入的方法參數的list中.
NOT IN - 指相關的列必須不在傳入的方法參數的list中
轉載請註明出處!