欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis?Example的高級(jí)用法詳解

 更新時(shí)間:2021年12月14日 10:16:24   作者:弦上的夢(mèng)  
這篇文章主要介紹了Mybatis?Example的高級(jí)用法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Mybatis Example的高級(jí)用法

近幾個(gè)項(xiàng)目一直使用的mybatis來(lái)對(duì)數(shù)據(jù)庫(kù)做查詢(xún),期間用到了很多高效簡(jiǎn)潔的查詢(xún)方法,特此記錄和分享。

一. mapper接口中的函數(shù)及方法

方法名 功能
int countByExample(UserExample example) 按條件計(jì)數(shù)
int deleteByPrimaryKey(Integer id) 按主鍵刪除
int deleteByExample(UserExample example) 按條件查詢(xún)
String/Integer insert(User record) 插入數(shù)據(jù)(返回值為ID)
User selectByPrimaryKey(Integer id) 按主鍵查詢(xún)
ListselectByExample(UserExample example) 按條件查詢(xún)
ListselectByExampleWithBLOGs(UserExample example) 按條件查詢(xún)(包括BLOB字段)。只有當(dāng)數(shù)據(jù)表中的字段類(lèi)型有為二進(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實(shí)例方法

example 用于添加條件,相當(dāng)于where后面的部分,理論上單表的任何復(fù)雜條件查詢(xún)都可以使用example來(lái)完成。

方法 說(shuō)明
example.setOrderByClause(“字段名 ASC”); 添加升序排列條件,DESC為降序
example.setDistinct(false) 去除重復(fù),boolean型,true為選擇不重復(fù)的記錄。
example.and(Criteria criteria) 為example添加criteria查詢(xún)條件,關(guān)系為與
example.or(Criteria criteria) 為example添加criteria查詢(xún)條件,關(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的模糊查詢(xún)條件
criteria.andXxxNotLike(“%”+value+”%”) 添加xxx字段值不為value的模糊查詢(xún)條件
criteria.andXxxBetween(value1,value2) 添加xxx字段值在value1和value2之間條件
criteria.andXxxNotBetween(value1,value2) 添加xxx字段值不在value1和value2之間條件

三. 使用案例

1.基本字段查詢(xún)

      // 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 查詢(xún)

  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ù)的條件查詢(xún)

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} ,'%') )
}

說(shuō)說(shuō)Mybatis Example常見(jiàn)用法

一. 說(shuō)明

我們?cè)谑褂胢ybatis example做業(yè)務(wù) 增/刪/改/查時(shí),會(huì)遇到一些場(chǎng)景。做一下記錄。

二. 排序查詢(xún)

使用mybatis example方式做查詢(xún)時(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();

三. 查詢(xún)limit, 只返回前50條數(shù)據(jù)

3.1 借助PageHelper

我們通過(guò)Pagehelper做分頁(yè)查詢(xún),那么limit同樣可以使用Pagehelper。如下,查詢(xún)符合條件中的前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 + "%");
        }
        // 這里限制查詢(xún)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;
    }

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java滑動(dòng)窗口算法題目練習(xí)(附詳細(xì)圖文及代碼)

    Java滑動(dòng)窗口算法題目練習(xí)(附詳細(xì)圖文及代碼)

    在數(shù)據(jù)處理和算法中,滑動(dòng)窗口是一種常見(jiàn)的技術(shù),用于解決一些數(shù)組或字符串相關(guān)的問(wèn)題,這篇文章主要介紹了Java滑動(dòng)窗口算法題目練習(xí)的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-10-10
  • java中常用注解@TableId的詳細(xì)解析

    java中常用注解@TableId的詳細(xì)解析

    @TableId注解是一種常見(jiàn)的Java注解,通常在使用關(guān)系型數(shù)據(jù)庫(kù)進(jìn)行對(duì)象關(guān)系映射(ORM)時(shí)使用,這篇文章主要介紹了java中常用注解@TableId的詳細(xì)解析,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-06-06
  • 基于spring+springmvc+hibernate 整合深入剖析

    基于spring+springmvc+hibernate 整合深入剖析

    這篇文章主要介紹了于spring+springmvc+hibernate整合實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • JAVA設(shè)計(jì)模式零基礎(chǔ)解析之單例模式的八種方式

    JAVA設(shè)計(jì)模式零基礎(chǔ)解析之單例模式的八種方式

    設(shè)計(jì)模式(Design pattern)是一套被反復(fù)使用、多數(shù)人知曉的、經(jīng)過(guò)分類(lèi)編目的、代碼設(shè)計(jì)經(jīng)驗(yàn)的總結(jié)。使用設(shè)計(jì)模式是為了可重用代碼、讓代碼更容易被他人理解、保證代碼可靠性
    2021-10-10
  • Jenkins Host key verification failed問(wèn)題解決

    Jenkins Host key verification failed問(wèn)題解決

    這篇文章主要介紹了Jenkins Host key verification failed問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • MyBatis批量插入大量數(shù)據(jù)(1w以上)

    MyBatis批量插入大量數(shù)據(jù)(1w以上)

    MyBatis進(jìn)行批量插入數(shù)時(shí),一次性插入超過(guò)一千條的時(shí)候MyBatis開(kāi)始報(bào)錯(cuò),本文主要介紹了MyBatis批量插入大量數(shù)據(jù)的解決方法,感興趣的可以了解一下
    2022-01-01
  • 微服務(wù)通過(guò)Feign調(diào)用進(jìn)行密碼安全認(rèn)證操作

    微服務(wù)通過(guò)Feign調(diào)用進(jìn)行密碼安全認(rèn)證操作

    這篇文章主要介紹了微服務(wù)通過(guò)Feign調(diào)用進(jìn)行密碼安全認(rèn)證操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • java中數(shù)組的定義及使用方法(推薦)

    java中數(shù)組的定義及使用方法(推薦)

    下面小編就為大家?guī)?lái)一篇java中數(shù)組的定義及使用方法(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-07-07
  • Java通過(guò)驅(qū)動(dòng)包(jar包)連接MySQL數(shù)據(jù)庫(kù)的步驟總結(jié)及驗(yàn)證方式

    Java通過(guò)驅(qū)動(dòng)包(jar包)連接MySQL數(shù)據(jù)庫(kù)的步驟總結(jié)及驗(yàn)證方式

    本文詳細(xì)介紹如何使用Java通過(guò)JDBC連接MySQL數(shù)據(jù)庫(kù),包括下載驅(qū)動(dòng)、配置Eclipse環(huán)境、檢測(cè)數(shù)據(jù)庫(kù)連接等關(guān)鍵步驟,感興趣的朋友一起看看吧
    2025-07-07
  • java將XML文檔轉(zhuǎn)換成json格式數(shù)據(jù)的示例

    java將XML文檔轉(zhuǎn)換成json格式數(shù)據(jù)的示例

    本篇文章主要介紹了java將XML文檔轉(zhuǎn)換成json格式數(shù)據(jù)的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12

最新評(píng)論