Tk.mybatis零sql語句實現(xiàn)動態(tài)sql查詢的方法(4種)
有時候,查詢數(shù)據(jù)需要根據(jù)條件使用動態(tài)查詢,這時候需要使用動態(tài)sql,通常我們會自己寫動態(tài)sql來實現(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>
??????? 這個sql是查詢成績大于90并且名字包含“i”的學生信息。但是有時候又加了一個條件,又要去改sql,改入?yún)?,有沒有一種方式可以將寫動態(tài)sql像寫代碼一樣實現(xiàn)呢?如果你有這個想法,推薦你了解一下Tk.mybatis。
????? 關于Tk.mybatis的介紹以及如何配置,本文暫不介紹,不了解的請自行百度,本文只介紹如何基于tk.mybatis實現(xiàn)不寫sql語句也能實現(xiàn)動態(tài)sql查詢。
實現(xiàn)方式:
1. 使用Example實現(xiàn)
2. 使用Example.createCriteria
3. 使用Example.builder實現(xiàn)
4. 使用WeekendSqls實現(xiàn)
方式一:使用Example實現(xiàn)
/** * 第一種:使用example查詢 */ @Test public void testSelectByExample() { Example example = new Example(Student.class); // 設置查詢列 example.selectProperties("id","name","score"); // 動態(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實現(xiàn)
/** * 第二種:使用example.createCriteria查詢 */ @Test public void testSelectByExampleCriteria() { Example example = new Example(Student.class); // 設置查詢列 example.selectProperties("id","name","score"); // 動態(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實現(xiàn)
/** * 第三種:使用Example.builder實現(xiàn) */ @Test public void testSelectByExampleBuilder() { Example example = Example.builder(Student.class) // 查詢列 .select("id","name","score") // 動態(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實現(xiàn)
/** * 第四種:使用weekendSqls實現(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); }
?到此這篇關于Tk.mybatis零sql語句實現(xiàn)動態(tài)sql查詢的方法(4種)的文章就介紹到這了,更多相關Tk.mybatis 動態(tài)sql查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java的四種常見線程池及Scheduled定時線程池實現(xiàn)詳解
這篇文章主要介紹了Java的四種常見線程池及Scheduled定時線程池實現(xiàn)詳解,在Java中,我們可以通過Executors類來創(chuàng)建ScheduledThreadPool,Executors類提供了幾個靜態(tài)方法來創(chuàng)建不同類型的線程池,包括ScheduledThreadPool,需要的朋友可以參考下2023-09-09Spring實戰(zhàn)之Bean的作用域singleton和prototype用法分析
這篇文章主要介紹了Spring實戰(zhàn)之Bean的作用域singleton和prototype用法,結合實例形式分析了Bean的作用域singleton和prototype相關使用方法及操作注意事項,需要的朋友可以參考下2019-11-11Spring中@ConditionalOnProperty注解的作用詳解
這篇文章主要介紹了Spring中@ConditionalOnProperty注解的作用詳解,@ConditionalOnProperty注解主要是用來判斷配置文件中的內(nèi)容來決定配置類是否生效用的,如果條件不匹配,則配置類不生效,需要的朋友可以參考下2024-01-01