最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Mybatis?Example的高級用法詳解

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

Mybatis Example的高級用法

近幾個項(xiàng)目一直使用的mybatis來對數(shù)據(jù)庫做查詢,期間用到了很多高效簡潔的查詢方法,特此記錄和分享。

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

方法名 功能
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)制的才會產(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ù)雜條件查詢都可以使用example來完成。

方法 說明
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,倒是可以簡便寫法。
            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只添加生成的第一個criteria,criteria1 將不會加到此條件中

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

說說Mybatis Example常見用法

一. 說明

我們在使用mybatis example做業(yè)務(wù) 增/刪/改/查時,會遇到一些場景。做一下記錄。

二. 排序查詢

使用mybatis example方式做查詢時候,業(yè)務(wù)需要按照條件排序,比如:創(chuàng)建時間倒序

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();

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

3.1 借助PageHelper

我們通過Pagehelper做分頁查詢,那么limit同樣可以使用Pagehelper。如下,查詢符合條件中的前50條。這里不會返回?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;
    }

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

相關(guān)文章

最新評論

井陉县| 饶阳县| 临泉县| 仁化县| 改则县| 孝义市| 城口县| 无为县| 公安县| 孟连| 台州市| 连山| 石狮市| 马龙县| 中西区| 晋中市| 图们市| 西乡县| 怀安县| 东海县| 拜泉县| 霍林郭勒市| 鄂州市| 那坡县| 虞城县| 辽源市| 中卫市| 宜君县| 象州县| 金湖县| 隆子县| 繁昌县| 茂名市| 云林县| 铜川市| 萨嘎县| 龙游县| 武川县| 广元市| 皋兰县| 多伦县|