您好,登錄后才能下訂單哦!
小編給大家分享一下Mybatis Example怎么用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
近幾個(gè)項(xiàng)目一直使用的mybatis來(lái)對(duì)數(shù)據(jù)庫(kù)做查詢,期間用到了很多高效簡(jiǎn)潔的查詢方法,特此記錄和分享。
方法名 | 功能 |
---|---|
int countByExample(UserExample example) | 按條件計(jì)數(shù) |
int deleteByPrimaryKey(Integer id) | 按主鍵刪除 |
int deleteByExample(UserExample example) | 按條件查詢 |
String/Integer insert(User record) | 插入數(shù)據(jù)(返回值為ID) |
User selectByPrimaryKey(Integer id) | 按主鍵查詢 |
ListselectByExample(UserExample example) | 按條件查詢 |
ListselectByExampleWithBLOGs(UserExample example) | 按條件查詢(包括BLOB字段)。只有當(dāng)數(shù)據(jù)表中的字段類型有為二進(jìn)制的才會(huì)產(chǎn)生。 |
int updateByPrimaryKey(User record) | 按主鍵更新 |
int updateByPrimaryKeySelective(User record) | 按主鍵更新值不為null的字段 |
int updateByExample(User record, UserExample example) | 按條件更新 |
int updateByExampleSelective(User record, UserExample example) | 按條件更新值不為null的字段 |
example 用于添加條件,相當(dāng)于where后面的部分,理論上單表的任何復(fù)雜條件查詢都可以使用example來(lái)完成。
方法 | 說(shuō)明 |
---|---|
example.setOrderByClause(“字段名 ASC”); | 添加升序排列條件,DESC為降序 |
example.setDistinct(false) | 去除重復(fù),boolean型,true為選擇不重復(fù)的記錄。 |
example.and(Criteria criteria) | 為example添加criteria查詢條件,關(guān)系為與 |
example.or(Criteria criteria) | 為example添加criteria查詢條件,關(guān)系為或 |
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之間條件 |
1.基本字段查詢
// 1.使用criteria Example example = new Example(User.class); Criteria criteria = example.createCriteria(); criteria.andEqualTo("name", name); criteria.andNotEqualTo("id", id); criteria.andEqualTo("userId", uid); List<User> list = userMapper.selectByExample(example); // 不使用criteria,實(shí)則example.and()本質(zhì)底層還是返回的criteria,倒是可以簡(jiǎn)便寫(xiě)法。 Example example = new Example(User.class); example.and() .andEqualTo("name", name) .andEqualTo("id", id) .andEqualTo("userId", uid); List<User> list = userMapper.selectByExample(example); 等效于:select * from user where name = #{name} and id = #{id} and uid = #{uid}
2. and or 查詢
Example example = new Example(User.getClass()); // where 條件 Criteria criteria = example.createCriteria(); Criteria criteria1 = example.createCriteria(); criteria.andIn("id", ids); criteria1.orLike("des", "%" + des + "%"); criteria1.orLike("name", "%" + name + "%"); example.and(criteria1); example.and().andEqualTo("status", staus) 等效于:where id in ( #{ids} ) and ( name like concat(‘%', #{name} ,'%') or des like concat(‘%', #{des} ,'%') ) and status = #{status}
注意:如果不加 example.and(criteria1);,則默認(rèn)example只添加生成的第一個(gè)criteria,criteria1 將不會(huì)加到此條件中
3. 數(shù)組參數(shù)的條件查詢
public Example test(List<String> names, String sex) { Example example = new Example(User.getClass()); example.and().andEqualTo("sex", sex) Example.Criteria criteria = example.createCriteria(); for (String str : names) { criteria.orLike("name", str); } example.and(criteria); List<User> list = userMapper.selectByExample(example); 等效于:where sex = #{sex} and ( name like concat(‘%', #{name1} ,'%') or name like concat(‘%', #{name2} ,'%') ) }
我們?cè)谑褂胢ybatis example做業(yè)務(wù) 增/刪/改/查時(shí),會(huì)遇到一些場(chǎng)景。做一下記錄。
使用mybatis example方式做查詢時(shí)候,業(yè)務(wù)需要按照條件排序,比如:創(chuàng)建時(shí)間倒序
example.setOrderByClause("create_time desc");
2.1 示例:
@Override public UpgradeNotifyInfoDTO queryLatestNotify(Integer appType) { UpgradeNotifyInfoDTO notifyDTO=new UpgradeNotifyInfoDTO(); SportAppUpgradeNotifyExample example = new SportAppUpgradeNotifyExample(); example.setOrderByClause("`create_time` desc"); SportAppUpgradeNotifyExample.Criteria criteria = example.createCriteria(); criteria.andAppTypeEqualTo(appType); // 0- 禁用 1-啟用 criteria.andStatusEqualTo(1); List<SportAppUpgradeNotify> list = upgradeNotifyMapper.selectByExample(example); if (!CollectionUtils.isEmpty(list)){ BeanUtils.copyProperties(list.get(0),notifyDTO); } return notifyDTO; }
注: 多條件排序?qū)懛ㄈ缦拢?/p>
ReservationProductOrderDetailExample example = new ReservationProductOrderDetailExample(); example.setOrderByClause("`reservate_time` desc,`reservate_start_time` desc, `create_time` desc"); ReservationProductOrderDetailExample.Criteria criteria = example.createCriteria();
3.1 借助PageHelper
我們通過(guò)Pagehelper做分頁(yè)查詢,那么limit同樣可以使用Pagehelper。如下,查詢符合條件中的前50條。這里不會(huì)返回?cái)?shù)據(jù)總數(shù) count
PageHelper.startPage(1, 50); public List<ShopCityInfoRespDTO> queryShopList(String shopCityName,String shopCityId) { List<ShopCityInfoRespDTO> shopCityList = new ArrayList<>(); ShopInfoExample example = new ShopInfoExample(); ShopInfoExample.Criteria criteria = example.createCriteria(); criteria.andStatusEqualTo("0"); if(!StringUtils.isEmpty(shopCityId)) { criteria.andShopIdEqualTo(shopCityId); } if(!StringUtils.isEmpty(shopCityName)) { criteria.andShopNameLike("%" + shopCityName + "%"); } // 這里限制查詢50條數(shù)據(jù),但不能返回總數(shù) PageHelper.startPage(1, 50); List<ShopInfo> shopInfoList = shopInfoMapper.selectByExample(example); if(CollectionUtils.isEmpty(shopInfoList)){ return shopCityList; } for (ShopInfo shopInfo : shopInfoList){ ShopCityInfoRespDTO respDTO = new ShopCityInfoRespDTO(); respDTO.setCompanyId(shopInfo.getCompanyId()); respDTO.setShopCityId(shopInfo.getShopId()); respDTO.setShopCityName(shopInfo.getShopName()); respDTO.setShopCityCode(shopInfo.getShopCode()); respDTO.setShopType(1); shopCityList.add(respDTO); } return shopCityList; }
以上是“Mybatis Example怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。