您好,登錄后才能下訂單哦!
這篇“mybatis QueryWrapper的apply、last、select怎么用”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“mybatis QueryWrapper的apply、last、select怎么用”文章吧。
場景: 查詢數據庫限制條數時
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);
建議自己寫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的用法:
查詢姓李的,并且出生日期范圍是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); }
以上就是關于“mybatis QueryWrapper的apply、last、select怎么用”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。