Tk.mybatis零sql语句实现动态sql查询的方法有哪些
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,这篇文章主要讲解了"Tk.mybatis零sql语句实现动态sql查询的方法有哪些",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Tk.mybatis零
千家信息网最后更新 2026年11月01日Tk.mybatis零sql语句实现动态sql查询的方法有哪些
这篇文章主要讲解了"Tk.mybatis零sql语句实现动态sql查询的方法有哪些",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Tk.mybatis零sql语句实现动态sql查询的方法有哪些"吧!
有时候,查询数据需要根据条件使用动态查询,这时候需要使用动态sql,通常我们会自己写动态sql来实现,比如:
这个sql是查询成绩大于90并且名字包含"i"的学生信息。但是有时候又加了一个条件,又要去改sql,改入参,有没有一种方式可以将写动态sql像写代码一样实现呢?如果你有这个想法,推荐你了解一下Tk.mybatis。
实现方式:
1. 使用Example实现
2. 使用Example.createCriteria
3. 使用Example.builder实现
4. 使用WeekendSqls实现
方式一:使用Example实现
/** * 第一种:使用example查询 */ @Test public void testSelectByExample() { Example example = new Example(Student.class); // 设置查询列 example.selectProperties("id","name","score"); // 动态sql example.and() .andGreaterThan("score",90) .andLike("name", "%i%"); // 去重 example.setDistinct(true); // 排序 example.orderBy("score").desc(); List students = studentMapper.selectByExample(example); System.out.println(students); } 方式二:使用example.createCriteria实现
/** * 第二种:使用example.createCriteria查询 */ @Test public void testSelectByExampleCriteria() { Example example = new Example(Student.class); // 设置查询列 example.selectProperties("id","name","score"); // 动态查询 example.createCriteria() .andGreaterThan("score",90) .andLike("name", "%i%"); // 去重 example.setDistinct(true); // 排序 example.orderBy("score").desc(); List students = studentMapper.selectByExample(example); System.out.println(students); } 方式三:使用Example.builder实现
/** * 第三种:使用Example.builder实现 */ @Test public void testSelectByExampleBuilder() { Example example = Example.builder(Student.class) // 查询列 .select("id","name","score") // 动态sql .where(Sqls.custom() .andGreaterThan("score",90) .andLike("name","%i%")) // 去重 .distinct() // 排序 .orderByDesc("score") .build(); List students = studentMapper.selectByExample(example); System.out.println(students); } 方式四:使用weekendSqls实现
/** * 第四种:使用weekendSqls实现 */ @Test public void testSelectByWeekendSqls() { WeekendSqls sqls = WeekendSqls.custom(); sqls = sqls .andGreaterThan(Student::getScore,90) .andLike(Student::getName,"%i%"); List sysRoles = studentMapper.selectByExample(Example.builder(Student.class) .select("id","name","score") .where(sqls) .distinct() .orderByDesc("score") .build()); System.out.println(sysRoles); } 感谢各位的阅读,以上就是"Tk.mybatis零sql语句实现动态sql查询的方法有哪些"的内容了,经过本文的学习后,相信大家对Tk.mybatis零sql语句实现动态sql查询的方法有哪些这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
查询
动态
方式
方法
语句
学习
排序
内容
有时候
条件
代码
信息
名字
学生
就是
思路
情况
想法
成绩
数据
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
耶加雪菲
互联网科技名片
新华互联网科技
江苏根荣互联网科技
中国互联网科技工作委员会毛军
深圳科技互联网
互联网络科技有限公司
互联网科技logo
海南购全球互联网科技
恒生科技互联网指数分析
互联网科技公司招聘
广州慧亚互联网科技怎么样
华服互联网科技官网
美国互联网科技实力
山东星卡互联网科技有限公司官网
南通创西澳互联网科技有限公司
保山互联网科技靠谱吗
数字科技和互联网金融哪个好
黑龙江互联网科技公司
优雇互联网科技有限公司
上海睿民互联网科技有限公司
互联网扶贫科技
互联网科技战争
天津摩尔互联网科技
融势互联网科技
有木互联网科技
阿大互联网科技有限公司
华中科技大学互联网的牛人
广东鹅鹅鹅互联网科技有限公司
互联网高科技绘画