欧美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)文章

  • 解決SpringMVC、tomcat、Intellij idea、ajax中文亂碼問(wèn)題

    解決SpringMVC、tomcat、Intellij idea、ajax中文亂碼問(wèn)題

    這篇文章主要介紹了解決SpringMVC、tomcat、Intellij idea、ajax中文亂碼問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-09-09
  • Spring boot如何通過(guò)@Scheduled實(shí)現(xiàn)定時(shí)任務(wù)及多線程配置

    Spring boot如何通過(guò)@Scheduled實(shí)現(xiàn)定時(shí)任務(wù)及多線程配置

    這篇文章主要介紹了Spring boot如何通過(guò)@Scheduled實(shí)現(xiàn)定時(shí)任務(wù)及多線程配置,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • 如何獲得spring代理對(duì)象的原對(duì)象

    如何獲得spring代理對(duì)象的原對(duì)象

    這篇文章主要介紹了如何獲得spring代理對(duì)象的原對(duì)象的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • SpringMvc獲取請(qǐng)求頭請(qǐng)求體消息過(guò)程解析

    SpringMvc獲取請(qǐng)求頭請(qǐng)求體消息過(guò)程解析

    這篇文章主要介紹了SpringMvc獲取請(qǐng)求頭請(qǐng)求體消息過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • SpringBoot整合flyway實(shí)現(xiàn)步驟解析

    SpringBoot整合flyway實(shí)現(xiàn)步驟解析

    這篇文章主要介紹了SpringBoot整合flyway實(shí)現(xiàn)步驟解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • SpringBoot超詳細(xì)講解yaml配置文件

    SpringBoot超詳細(xì)講解yaml配置文件

    這篇文章主要介紹了SpringBoot中的yaml配置文件問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • springboot?ErrorPageFilter的實(shí)際應(yīng)用詳解

    springboot?ErrorPageFilter的實(shí)際應(yīng)用詳解

    這篇文章主要介紹了springboot?ErrorPageFilter的實(shí)際應(yīng)用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • JVM的常用命令匯總

    JVM的常用命令匯總

    監(jiān)測(cè)java應(yīng)用,最方便的就是直接使用jdk提供的現(xiàn)成工具,在jdk的安裝的bin目錄下,已經(jīng)提供了多種命令行監(jiān)測(cè)工具。本文為大家總結(jié)了幾個(gè)JVM的常用命令,需要的可以參考一下
    2022-10-10
  • 詳解java解決分布式環(huán)境中高并發(fā)環(huán)境下數(shù)據(jù)插入重復(fù)問(wèn)題

    詳解java解決分布式環(huán)境中高并發(fā)環(huán)境下數(shù)據(jù)插入重復(fù)問(wèn)題

    這篇文章主要介紹了java解決并發(fā)數(shù)據(jù)重復(fù)問(wèn)題 ,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • JPA多數(shù)據(jù)源分布式事務(wù)處理方案

    JPA多數(shù)據(jù)源分布式事務(wù)處理方案

    這篇文章主要為大家介紹了JPA多數(shù)據(jù)源分布式事務(wù)處理的兩種事務(wù)方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-02-02

最新評(píng)論