首頁 > 軟體

mybatis QueryWrapper的條件構造之apply、last、select解析

2022-03-22 16:00:15

QueryWrapper條件構造之apply、last、select

場景: 查詢資料庫限制條數時

mysql上的limit使用 

QueryWrapper needSyn = new QueryWrapper();
needSyn.ge("ModifyDate",lmtSynRec.getLatestDate());
needSyn.last("limit 200");
List<CusCarInfo> needData = cusCarInfoMapper.selectList(needSyn);

如果是oracle則可以用apply

needSyn.apply("rownum < {0}" , 200);

如果是sqlserver

建議自己寫sql查詢語句

<select id="getSynList" resultType="com.xmeport.project.model.entity.CusCarInfo">
    select top ${top}  *  from CUS_CAR_INFO t where t.ModifyDate > cast(#{latestDate} as datetime) orderBy 
 t.ModifyDate asc
</select>
List<CusCarInfo> getSynList(int top, Date latestDate);

或者使用QueryWrapper的select

QueryWrapper<CusCarInfo> wrapper  = new QueryWrapper();
wrapper.select("top 200 *");
return this.list(wrapper);

以上就是根據最新時間查詢top條資料

傳入的欄位是時間型別,就用#{} 預留位置接收。如果是具體的一個數位或字元可以用 ${ }來接收。

條件構造器QueryWrapper範例

我們再搞幾個範例來深入理解下條件構造器QueryWrapper的用法:

案例一

查詢姓李的,並且出生日期範圍是1993-02-09到1994-04-09的員工

sql實現:

SELECT * FROM t_employee WHERE DATE_FORMAT(birthday,'%Y-%m-%d')>='1993-02-09' AND DATE_FORMAT(birthday,'%Y-%m-%d')<='1994-04-09' AND NAME LIKE '李%'

mp實現:

/**
 * 查詢姓李的,並且出生日期範圍是1993-02-09到1994-04-09的員工
 * sql:SELECT * FROM t_employee WHERE DATE_FORMAT(birthday,'%Y-%m-%d')>='1993-02-09' AND DATE_FORMAT(birthday,'%Y-%m-%d')<='1994-04-09' AND NAME LIKE '李%'
 */
@Test
public void selectByQueryWrapper3(){
  QueryWrapper<Employee> queryWrapper=new QueryWrapper();
  // QueryWrapper<Employee> queryWrapper2=Wrappers.<Employee>query();
  queryWrapper.apply("DATE_FORMAT(birthday,'%Y-%m-%d')>={0} and DATE_FORMAT(birthday,'%Y-%m-%d')<={1}","1993-02-09","1994-04-09").likeRight("name","李");
  List<Employee> employeeList = employeeMapper.selectList(queryWrapper);
  System.out.println(employeeList);
}

案例二

查詢姓李的或者郵箱不為空並且是女性的員工

sql實現:

SELECT * FROM t_employee WHERE NAME LIKE '李%' OR (email IS NOT NULL AND gender ='女')

mp實現:

/**
 * 查詢姓李的或者郵箱不為空並且是女性的員工
 * sql:SELECT * FROM t_employee WHERE NAME LIKE '李%' OR (email IS NOT NULL AND gender ='女')
 */
@Test
public void selectByQueryWrapper4(){
  QueryWrapper<Employee> queryWrapper=new QueryWrapper();
  // QueryWrapper<Employee> queryWrapper2=Wrappers.<Employee>query();
  queryWrapper.likeRight("name","李").or(wq->wq.isNotNull("email").eq("gender","女"));
  List<Employee> employeeList = employeeMapper.selectList(queryWrapper);
  System.out.println(employeeList);
}

案例三

查詢姓李的並且郵箱不為空或者是女性的員工

sql實現:

SELECT * FROM t_employee WHERE NAME LIKE '李%' AND (email IS NOT NULL OR gender ='女')

mp實現:

/**
 * 查詢姓李的並且郵箱不為空或者是女性的員工
 * sql:SELECT * FROM t_employee WHERE NAME LIKE '李%' AND (email IS NOT NULL OR gender ='女')
 */
@Test
public void selectByQueryWrapper5(){
  QueryWrapper<Employee> queryWrapper=new QueryWrapper();
  // QueryWrapper<Employee> queryWrapper2=Wrappers.<Employee>query();
  queryWrapper.likeRight("name","李").and(wq->wq.isNotNull("email").or().eq("gender","女"));
  List<Employee> employeeList = employeeMapper.selectList(queryWrapper);
  System.out.println(employeeList);
}

案例四

查詢屬於編號1,2,3部門的並且薪水小於等於3500的員工 根據年齡從大到小排序顯示

sql實現:

SELECT * FROM t_employee WHERE salary<=3500 AND departmentId IN (1,2,3) ORDER BY birthday ASC

mp實現:

/**
 * 查詢屬於編號1,2,3部門的並且薪水小於等於3500的員工 根據年齡從大到小排序顯示
 * sql:SELECT * FROM t_employee WHERE salary<=3500 AND departmentId IN (1,2,3) ORDER BY birthday ASC
 */
@Test
public void selectByQueryWrapper6(){
  QueryWrapper<Employee> queryWrapper=new QueryWrapper();
  // QueryWrapper<Employee> queryWrapper2=Wrappers.<Employee>query();
  queryWrapper.likeRight("name","李").and(wq->wq.isNotNull("email").or().eq("gender","女"));
  List<Employee> employeeList = employeeMapper.selectList(queryWrapper);
  System.out.println(employeeList);
}

以上為個人經驗,希望能給大家一個參考,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com