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

mybatis plus條件構(gòu)造器queryWrapper、updateWrapper

 更新時(shí)間:2020年09月02日 11:50:56   作者:青蛙與大鵝  
這篇文章主要介紹了mybatis plus條件構(gòu)造器queryWrapper、updateWrapper,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

注明:上篇文章介紹了springboot+mybatis-plus通用CRUD的用法,這篇文章我們來介紹一下mybatis-plus強(qiáng)大的條件構(gòu)造器。mybatis-plus的版本為最新版3.0.3 。條件構(gòu)造器咱們講述queryWrapper和updateWrapper的用法、關(guān)系、以及強(qiáng)大之處。

首先在這里寫下官方文檔的鏈接位置,官方文檔說的很詳細(xì)。如果還想知道在項(xiàng)目中的具體用法請往下看。

一、條件構(gòu)造器關(guān)系介紹

介紹 :

1.上圖綠色框?yàn)槌橄箢恆bstract
2.藍(lán)色框?yàn)檎lass類,可new對象
3.黃色箭頭指向?yàn)楦缸宇愱P(guān)系,箭頭指向?yàn)楦割?br />

wapper介紹 :

1.Wrapper : 條件構(gòu)造抽象類,最頂端父類,抽象類中提供4個(gè)方法西面貼源碼展示
2.AbstractWrapper : 用于查詢條件封裝,生成 sql 的 where 條件
3.AbstractLambdaWrapper : Lambda 語法使用 Wrapper統(tǒng)一處理解析 lambda 獲取 column。
4.LambdaQueryWrapper :看名稱也能明白就是用于Lambda語法使用的查詢Wrapper
5.LambdaUpdateWrapper : Lambda 更新封裝Wrapper
6.QueryWrapper : Entity 對象封裝操作類,不是用lambda語法
7.UpdateWrapper : Update 條件封裝,用于Entity對象更新操作

二、項(xiàng)目實(shí)例

在這里我以QueryWrapper和UpdateWrapper為例,進(jìn)行測試講解。我會在上篇博客原有的基礎(chǔ)上進(jìn)行測試,如果不喜歡搭建項(xiàng)目的可直接下載我上個(gè)項(xiàng)目,上個(gè)項(xiàng)目的博客對應(yīng)上個(gè)項(xiàng)目的講解

上圖表格為條件構(gòu)造器使用中的各個(gè)方法格式和說明,如有不懂可參考官方文檔內(nèi)容

構(gòu)造器條件

package com.lqf.crud;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lqf.crud.bean.crm.User;
import com.lqf.crud.dao.crm.UserMapper;
import com.sun.org.apache.xerces.internal.util.EntityResolverWrapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.RowBounds;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.jsf.el.WebApplicationContextFacesELResolver;

import javax.naming.Name;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
public class QueryWrapperTests {

  @Autowired
  private UserMapper mapper;

  /**
   * <p>
   * 根據(jù)根據(jù) entity 條件,刪除記錄,QueryWrapper實(shí)體對象封裝操作類(可以為 null)
   * 下方獲取到queryWrapper后刪除的查詢條件為name字段為null的and年齡大于等于12的and email字段不為null的
   * 同理寫法條件添加的方式就不做過多介紹了。
   * </p>
   */
  @Test
  public void delete() {
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    queryWrapper
        .isNull("name")
        .ge("age", 12)
        .isNotNull("email");
    int delete = mapper.delete(queryWrapper);
    System.out.println("delete return count = " + delete);
  }


  /**
   * <p>
   * 根據(jù) entity 條件,查詢一條記錄,
   * 這里和上方刪除構(gòu)造條件一樣,只是seletOne返回的是一條實(shí)體記錄,當(dāng)出現(xiàn)多條時(shí)會報(bào)錯(cuò)
   * </p>
   */
  @Test
  public void selectOne() {
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("name", "lqf");

    User user = mapper.selectOne(queryWrapper);
    System.out.println(user);
  }


  /**
   * <p>
   * 根據(jù) Wrapper 條件,查詢總記錄數(shù)
   * </p>
   *
   * @param queryWrapper 實(shí)體對象
   */
  @Test
  public void selectCount() {
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("name", "lqf");

    Integer count = mapper.selectCount(queryWrapper);
    System.out.println(count);
  }


  /**
   * <p>
   * 根據(jù) entity 條件,查詢?nèi)坑涗?
   * </p>
   *
   * @param queryWrapper 實(shí)體對象封裝操作類(可以為 null)為null查詢?nèi)?
   */
  @Test
  public void selectList() {
    List<User> list = mapper.selectList(null);

    System.out.println(list);
  }

  /**
   * <p>
   * 根據(jù) Wrapper 條件,查詢?nèi)坑涗?
   * </p>
   *
   * @param queryWrapper 實(shí)體對象封裝操作類(可以為 null)
   */
  @Test
  public void selectMaps() {
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    queryWrapper.isNotNull("name");
    List<Map<String, Object>> maps = mapper.selectMaps(queryWrapper);
    for (Map<String, Object> map : maps) {
      System.out.println(map);
    }
  }

  /**
   * 打印結(jié)果
   * {name=lqf, id=1046282328366391406, age=12, email=lqf@163.com, status=false}
   * {name=lqf, id=1046282328366391407, age=12, email=lqf@163.com, status=false}
   * {name=lqf, id=1046282328366391408, age=12, email=lqf@163.com, status=false}
   * {name=lqf, id=1046282328366391409, age=12, email=lqf@163.com, status=false}
   * {name=lqf, id=1046282328366391410, age=12, email=lqf@163.com, status=false}
   * {name=lqf, id=1046282328366391411, age=12, email=lqf@163.com, status=false}
   * {name=lqf, id=1046282328366391412, age=12, email=lqf@163.com, status=false}
   * {name=lqf, id=1046282328366391413, age=12, email=lqf@163.com, status=false}
   * {name=lqf, id=1046282328366391414, age=12, email=lqf@163.com, status=false}
   * {name=lqf, id=1046282328366391415, age=12, email=lqf@163.com, status=false}
   * {name=lqf, id=1046282328366391416, age=12, email=lqf@163.com, status=false}
   * {name=lqf, id=1046282328366391417, age=12, email=lqf@163.com, status=false}
   * {name=lqf, id=1046282328366391418, age=12, email=lqf@163.com, status=false}
   * json類型的鍵值對模式
   */

  /**
   * <p>
   * 根據(jù) entity 條件,查詢?nèi)坑涗洠ú⒎摚?
   * </p>
   *
   * @param page     分頁查詢條件(可以為 RowBounds.DEFAULT)
   * @param queryWrapper 實(shí)體對象封裝操作類(可以為 null)
   */
  @Test
  public void selectPage() {
    Page<User> page = new Page<>(1, 5);
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();

    IPage<User> userIPage = mapper.selectPage(page, queryWrapper);
    System.out.println(userIPage);
  }

  /**
   * 打印結(jié)果
   * ==> Preparing: SELECT COUNT(1) FROM user
   * ==> Parameters:
   * <==  Columns: COUNT(1)
   * <==    Row: 100
   * ==> Preparing: SELECT id,name,age,email,status FROM user LIMIT 0,5
   * ==> Parameters:
   * <==  Columns: id, name, age, email, status
   * <==    Row: 1046282328366391319, lqf, 12, lqf@163.com, 0
   * <==    Row: 1046282328366391320, lqf, 12, lqf@163.com, 0
   * <==    Row: 1046282328366391321, lqf, 12, lqf@163.com, 0
   * <==    Row: 1046282328366391322, lqf, 12, lqf@163.com, 0
   * <==    Row: 1046282328366391323, lqf, 12, lqf@163.com, 0
   * <==   Total: 5
   *
   *
   * 這里需要在項(xiàng)目中加入分頁插件
   *  @Bean
   *   public PaginationInterceptor paginationInterceptor() {
   *     return new PaginationInterceptor();
   *   }
   */


  /**
   * <p>
   * 根據(jù) Wrapper 條件,查詢?nèi)坑涗洠ú⒎摚?
   * </p>
   *
   * @param page     分頁查詢條件
   * @param queryWrapper 實(shí)體對象封裝操作類
   */
  @Test
  public void selectMapsPage() {
    Page<User> page = new Page<>(1, 5);
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();

    IPage<Map<String, Object>> mapIPage = mapper.selectMapsPage(page, queryWrapper);
    System.out.println(mapIPage);
  }

  /**
   * 和上個(gè)分頁同理只是返回類型不同
   */


  /**
   * <p>
   * 根據(jù) whereEntity 條件,更新記錄
   * </p>
   *
   * @param entity    實(shí)體對象 (set 條件值,不能為 null)
   * @param updateWrapper 實(shí)體對象封裝操作類(可以為 null,里面的 entity 用于生成 where 語句)
   */
  @Test
  public void update() {

    //修改值
    User user = new User();
    user.setStatus(true);
    user.setName("zhangsan");

    //修改條件s
    UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
    userUpdateWrapper.eq("name", "lqf");

    int update = mapper.update(user, userUpdateWrapper);

    System.out.println(update);
  }

  /**
   * 打印結(jié)果
   * ==> Preparing: UPDATE user SET name=?, status=? WHERE name = ?
   * ==> Parameters: zhangsan(String), true(Boolean), lqf(String)
   * <==  Updates: 100
   * Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@56a4f272]
   * 100
   * 2018-10-02 15:08:03.928 INFO 7972 --- [    Thread-2] o.s.w.c.s.GenericWebApplicationContext  : Closing org.springframework.web.context.support.GenericWebApplicationContext@37313c65: startup date [Tue Oct 02 15:08:00 CST 2018]; root of context hierarchy
   * 2018-10-02 15:08:03.937 INFO 7972 --- [    Thread-2] com.zaxxer.hikari.HikariDataSource    : HikariPool-1 - Shutdown initiated...
   * 2018-10-02 15:08:04.053 INFO 7972 --- [    Thread-2] com.zaxxer.hikari.HikariDataSource    : HikariPool-1 - Shutdown completed.
   *
   * Process finished with exit code 0
   */

}

上方代碼對通過構(gòu)造器條件進(jìn)行的查詢、刪除、修改進(jìn)行是演示,構(gòu)造器方法沒有做過多演示,但是所有的構(gòu)造器方法同理使用,如果還有不會用的點(diǎn)開看官方文檔查看并按照上方例子使用即可。

源碼下載地址

到此這篇關(guān)于mybatis plus條件構(gòu)造器queryWrapper、updateWrapper的文章就介紹到這了,更多相關(guān)queryWrapper、updateWrapper內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 教你springboot+dubbo快速啟動的方法

    教你springboot+dubbo快速啟動的方法

    這篇文章主要介紹了springboot+dubbo快速啟動的方法,dubbo的角色廣泛的分為三類provider,comsumer,注冊中心,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下
    2022-04-04
  • 淺談Java成員變量與屬性的區(qū)別(簡單最易懂的解釋)

    淺談Java成員變量與屬性的區(qū)別(簡單最易懂的解釋)

    下面小編就為大家?guī)硪黄獪\談Java成員變量與屬性的區(qū)別(簡單最易懂的解釋)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-05-05
  • java實(shí)現(xiàn)voctor按指定方式排序示例分享

    java實(shí)現(xiàn)voctor按指定方式排序示例分享

    這篇文章主要介紹了java實(shí)現(xiàn)voctor按指定方式排序示例,需要的朋友可以參考下
    2014-03-03
  • Mybatis插入數(shù)據(jù)后自增id獲取方式

    Mybatis插入數(shù)據(jù)后自增id獲取方式

    在MyBatis中,獲取自增主鍵可以通過useGeneratedKeys屬性或selectKey節(jié)點(diǎn)實(shí)現(xiàn),useGeneratedKeys設(shè)置時(shí),需設(shè)置keyProperty指定主鍵字段,數(shù)據(jù)庫表也要相應(yīng)設(shè)置,selectKey節(jié)點(diǎn)可在插入操作后,通過特定SQL查詢獲得主鍵
    2024-09-09
  • mybatis中返回主鍵一直為1的問題

    mybatis中返回主鍵一直為1的問題

    這篇文章主要介紹了mybatis中返回主鍵一直為1的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • java實(shí)現(xiàn)Redisson的基本使用

    java實(shí)現(xiàn)Redisson的基本使用

    Redisson是一個(gè)在Redis的基礎(chǔ)上實(shí)現(xiàn)的Java駐內(nèi)存數(shù)據(jù)網(wǎng)格客戶端,本文主要介紹了java實(shí)現(xiàn)Redisson的基本使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-12-12
  • springMVC不掃描controller中的方法問題

    springMVC不掃描controller中的方法問題

    這篇文章主要介紹了springMVC不掃描controller中的方法問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 詳解java中BigDecimal精度問題

    詳解java中BigDecimal精度問題

    這篇文章主要介紹了java BigDecimal精度問題,對精確計(jì)算感興趣的同學(xué),可以參考下
    2021-05-05
  • MyBatis入門介紹(超簡單)

    MyBatis入門介紹(超簡單)

    mybatis是Java的持久層框架, JAVA操作數(shù)據(jù)庫是通過jdbc來操作的,而mybatis是對jdbc的封裝。下文給大家介紹mybatis入門知識,感興趣的朋友參考下吧
    2017-08-08
  • java實(shí)現(xiàn)excel自定義樣式與字段導(dǎo)出詳細(xì)圖文教程

    java實(shí)現(xiàn)excel自定義樣式與字段導(dǎo)出詳細(xì)圖文教程

    最近接到一個(gè)需求,客戶不滿意原本導(dǎo)出的csv文件,想要導(dǎo)出Excel文件,下面這篇文章主要給大家介紹了關(guān)于java實(shí)現(xiàn)excel自定義樣式與字段導(dǎo)出詳細(xì)圖文教程
    2023-09-09

最新評論

三江| 利辛县| 桃园市| 佛学| 南皮县| 兰考县| 乐清市| 南通市| 宣化县| 镇安县| 富平县| 兖州市| 肇东市| 三明市| 阳高县| 和平县| 崇阳县| 塔城市| 南充市| 六安市| 弥勒县| 抚顺市| 长葛市| 定日县| 渝中区| 正阳县| 文山县| 涞水县| 大田县| 麻城市| 拉萨市| 禹州市| 台山市| 边坝县| 游戏| 东阳市| 茶陵县| 三台县| 龙胜| 任丘市| 辽中县|