Tk.mybatis零sql語句實(shí)現(xiàn)動(dòng)態(tài)sql查詢的方法(4種)
有時(shí)候,查詢數(shù)據(jù)需要根據(jù)條件使用動(dòng)態(tài)查詢,這時(shí)候需要使用動(dòng)態(tài)sql,通常我們會(huì)自己寫動(dòng)態(tài)sql來實(shí)現(xiàn),比如:
<select id="findStudentByCondition" resultType="com.example.service.entity.Student"> SELECT id, name, score FROM tbl_student <where> <if test="score !=null and score > 0"> score > #{score} </if> <if test="name !=null and name != ''"> <bind name="pattern" value=" '%' + name + '%' "/> AND name LIKE #{pattern} </if> </where> ORDER BY score DESc </select>
??????? 這個(gè)sql是查詢成績(jī)大于90并且名字包含“i”的學(xué)生信息。但是有時(shí)候又加了一個(gè)條件,又要去改sql,改入?yún)?,有沒有一種方式可以將寫動(dòng)態(tài)sql像寫代碼一樣實(shí)現(xiàn)呢?如果你有這個(gè)想法,推薦你了解一下Tk.mybatis。
????? 關(guān)于Tk.mybatis的介紹以及如何配置,本文暫不介紹,不了解的請(qǐng)自行百度,本文只介紹如何基于tk.mybatis實(shí)現(xiàn)不寫sql語句也能實(shí)現(xiàn)動(dòng)態(tài)sql查詢。
實(shí)現(xiàn)方式:
1. 使用Example實(shí)現(xiàn)
2. 使用Example.createCriteria
3. 使用Example.builder實(shí)現(xiàn)
4. 使用WeekendSqls實(shí)現(xiàn)
方式一:使用Example實(shí)現(xiàn)
/** * 第一種:使用example查詢 */ @Test public void testSelectByExample() { Example example = new Example(Student.class); // 設(shè)置查詢列 example.selectProperties("id","name","score"); // 動(dòng)態(tài)sql example.and() .andGreaterThan("score",90) .andLike("name", "%i%"); // 去重 example.setDistinct(true); // 排序 example.orderBy("score").desc(); List<Student> students = studentMapper.selectByExample(example); System.out.println(students); }
方式二:使用example.createCriteria實(shí)現(xiàn)
/** * 第二種:使用example.createCriteria查詢 */ @Test public void testSelectByExampleCriteria() { Example example = new Example(Student.class); // 設(shè)置查詢列 example.selectProperties("id","name","score"); // 動(dòng)態(tài)查詢 example.createCriteria() .andGreaterThan("score",90) .andLike("name", "%i%"); // 去重 example.setDistinct(true); // 排序 example.orderBy("score").desc(); List<Student> students = studentMapper.selectByExample(example); System.out.println(students); }
方式三:使用Example.builder實(shí)現(xiàn)
/** * 第三種:使用Example.builder實(shí)現(xiàn) */ @Test public void testSelectByExampleBuilder() { Example example = Example.builder(Student.class) // 查詢列 .select("id","name","score") // 動(dòng)態(tài)sql .where(Sqls.custom() .andGreaterThan("score",90) .andLike("name","%i%")) // 去重 .distinct() // 排序 .orderByDesc("score") .build(); List<Student> students = studentMapper.selectByExample(example); System.out.println(students); }
方式四:使用weekendSqls實(shí)現(xiàn)
/** * 第四種:使用weekendSqls實(shí)現(xiàn) */ @Test public void testSelectByWeekendSqls() { WeekendSqls<Student> sqls = WeekendSqls.custom(); sqls = sqls .andGreaterThan(Student::getScore,90) .andLike(Student::getName,"%i%"); List<Student> sysRoles = studentMapper.selectByExample(Example.builder(Student.class) .select("id","name","score") .where(sqls) .distinct() .orderByDesc("score") .build()); System.out.println(sysRoles); }
?到此這篇關(guān)于Tk.mybatis零sql語句實(shí)現(xiàn)動(dòng)態(tài)sql查詢的方法(4種)的文章就介紹到這了,更多相關(guān)Tk.mybatis 動(dòng)態(tài)sql查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java的四種常見線程池及Scheduled定時(shí)線程池實(shí)現(xiàn)詳解
這篇文章主要介紹了Java的四種常見線程池及Scheduled定時(shí)線程池實(shí)現(xiàn)詳解,在Java中,我們可以通過Executors類來創(chuàng)建ScheduledThreadPool,Executors類提供了幾個(gè)靜態(tài)方法來創(chuàng)建不同類型的線程池,包括ScheduledThreadPool,需要的朋友可以參考下2023-09-09Idea運(yùn)行單個(gè)main方法,不編譯整個(gè)工程的問題
這篇文章主要介紹了Idea運(yùn)行單個(gè)main方法,不編譯整個(gè)工程的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04Spring實(shí)戰(zhàn)之Bean的作用域singleton和prototype用法分析
這篇文章主要介紹了Spring實(shí)戰(zhàn)之Bean的作用域singleton和prototype用法,結(jié)合實(shí)例形式分析了Bean的作用域singleton和prototype相關(guān)使用方法及操作注意事項(xiàng),需要的朋友可以參考下2019-11-11Mybatis批量插入返回成功的數(shù)目實(shí)例
這篇文章主要介紹了Mybatis批量插入返回成功的數(shù)目實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12Java觀察者模式之實(shí)現(xiàn)對(duì)象間的一對(duì)多依賴
這篇文章主要介紹了Java觀察者模式之實(shí)現(xiàn)對(duì)象間的一對(duì)多依賴的方法,Java觀察者模式是一種行為型設(shè)計(jì)模式,用于實(shí)現(xiàn)對(duì)象之間的消息傳遞和通信,文中有詳細(xì)的實(shí)現(xiàn)步驟和代碼示例,,需要的朋友可以參考下2023-05-05Spring中@ConditionalOnProperty注解的作用詳解
這篇文章主要介紹了Spring中@ConditionalOnProperty注解的作用詳解,@ConditionalOnProperty注解主要是用來判斷配置文件中的內(nèi)容來決定配置類是否生效用的,如果條件不匹配,則配置類不生效,需要的朋友可以參考下2024-01-01