當前位置:
首頁 >
tkmybatis通用mapper实现在使用Example进行查询的几种方式
發布時間:2024/1/1
33
豆豆
生活随笔
收集整理的這篇文章主要介紹了
tkmybatis通用mapper实现在使用Example进行查询的几种方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
如下列舉四種方式,但是不止四種哦。
其中weekend方式需要升級jdk到1.8及以上。
廢話不代碼!
首先定義數據庫表映射類:
public class MybatisDemo {private Long id;private Long count;private String name;public Long getId() {return id;}public Long getCount() {return count;}public String getName() {return name;}// setter…… }此處省略了數據庫表映射和set方法。
接下來就是實現example查詢的幾種方式,核心代碼如下:
方式一:普通Example方式(從and方法開始可以實現動態sql拼接)
????Example example = new Example(CandidateBrandEntity.class);example//.selectProperties("cabId","cabName").and().andEqualTo("cabDeleted",0).andLike("cabName","%d%");// 排序example.orderBy("cabCreatedTime")/*.desc()*/.orderBy("cabId").desc();// 獲得結果List<CandidateBrandEntity> brands = brandEntityMapper.selectByExample(example);方式二:Criteria方式(可使用criteria完成動態sql拼接)
Example example = new Example(MybatisDemo.class); Example.Criteria criteria = example.createCriteria(); criteria.andEqualTo("count", 0).andLike("name", "%d%"); example.orderBy("count")//.desc().orderBy("name").desc(); List<MybatisDemo> demos = mybatisDemoMapper.selectByExample(example);方式三:Example.builder 方式(其中where從句中內容可以拿出來進行動態sql拼接)
Example example = Example.builder(MybatisDemo.class).select("cabId","cabName").where(Sqls.custom().andEqualTo("count", 0).andLike("name", "%d%")).orderByDesc("count","name").build(); List<MybatisDemo> demos = mybatisDemoMapper.selectByExample(example);方式四:Example.builder + Weekend方式,優勢:不用輸入屬性名,避免數據庫有變動或輸入錯誤就會出錯
//獲得seekendsql WeekendSqls<MybatisDemo> sqls = WeekendSqls.<MybatisDemo>custom();//可進行動態sql拼接 sqls = sqls.andEqualTo(MybatisDemo::getCount,0).andLike(MybatisDemo::getName,"%d%");//獲得結果 List<MybatisDemo> demos = mybatisDemoMapper.selectByExample(Example.builder(MybatisDemo.class).where(sqls).orderByDesc("count","name").build());?
?
參考內容:https://github.com/abel533/Mapper/wiki/6.example
總結
以上是生活随笔為你收集整理的tkmybatis通用mapper实现在使用Example进行查询的几种方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【ppt课件制作】Focusky教程 |
- 下一篇: 关于移动库存管理的思考