当前位置:网站首页>tkMapper之使用Weekend拼接条件进行条件查询
tkMapper之使用Weekend拼接条件进行条件查询
2022-07-13 17:47:00 【Kuo-Teng】
tkMapper之使用Weekend拼接条件进行条件查询
0. 引入
我们在这里使用StudentInfo实体类来进行举例:
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class StudentInfo {
@Id
private Long id;
/** * 姓名 */
private String name;
/** * 手机号 */
private String mobile;
/** * 学号 */
private String studentId;
/** * 班级编号 */
private String classId;
/** * 创建时间 */
private Date ctime;
/** * 更新时间 */
private Date mtime;
/** * 删除标志 0:未删除 1:删除 */
private Integer deleted_status;
}
1. andEqualTo
使用andEqualTo语句,相当于使用where 字段名 = 某值的SQL语句
如果我们想查询班级编号为12345678的学生,可以这样写:
Weekend<StudentInfo> weekend = Weekend.of(StudentInfo.class);
WeekendCriteria<StudentInfo, Object> weekendCriteria = weekend.weekendCriteria();
weekendCriteria.andEqualTo(StudentInfo::getClassId, "12345678");
weekendCriteria.andEqualTo(StudentInfo::getdeletedStatus, 0);
List<StudentInfo> infoList = studentInfoMapper.selectByExample(weekend);
因为预期能够查询到多名学生信息,因此应该使用selectByExample,并使用List来接收返回信息;
2. andIn
使用andIn语句,相当于使用where 字段名 in 某值区间的SQL语句
如果我们想在学生班级编号为12345678的基础上,查询出学号为0001、0002和0003的同学,那么可以这样写:
Weekend<StudentInfo> weekend = Weekend.of(StudentInfo.class);
WeekendCriteria<StudentInfo, Object> weekendCriteria = weekend.weekendCriteria();
weekendCriteria.andEqualTo(StudentInfo::getClassId, "12345678");
List<String> studentIdList = Arrays.asList("0001", "0002", "0003");
weekendCriteria.andIn(StudentInfo::getStudentId, studentIdList);
weekendCriteria.andEqualTo(StudentInfo::getdeletedStatus, 0);
List<StudentInfo> infoList = studentInfoMapper.selectByExample(weekend);
3. andNotEqualTo
刚好与andEqualTo相反;
4. andNotIn
刚好与andIn相反;
5. andGreaterThanOrEqualTo
顾名思义,这个是大于等于,也可以用来按照数据的修改日期进行过滤,写法如下:
weekendCriteria.andGreaterThanOrEqualTo(StudentInfo::getMtime, DateUtils.transferString2Date(LocalDate.now().toString()));
其中,DateUtils.transferString2Date方法可以参考我的另一篇文章:将String转为date,方便快捷;
6. andLike
可以使用andLike进行模糊查询,但是要注意,模糊查询会进行全表扫描,速度缓慢,极易造成慢查询(关于慢查询,可以参考我的另一篇文章:什么是慢查询?如何优化?);
如果我们想查找所有的名字中带有红的同学信息,可以这样写:
Weekend<StudentInfo> weekend = Weekend.of(StudentInfo.class);
WeekendCriteria<StudentInfo, Object> weekendCriteria = weekend.weekendCriteria();
weekendCriteria.andLike(StudentInfo::getName, "%" + "红" + "%");
weekendCriteria.andEqualTo(StudentInfo::getdeletedStatus, 0);
List<StudentInfo> infoList = studentInfoMapper.selectByExample(weekend);
如果数据库中要进行大量的模糊搜索、文本匹配,可以考虑使ElasticSearch,ElasticSearch是一个分布式、高性能、高可用、可伸缩、RESTful 风格的搜索和数据分析引擎。
7. 总结
在查询条件比较多的时候,我们可以灵活地将各个条件拼在一起,tkMapper让我们在最大程度上避免了手写SQL语句,极大地提升了我们的开发效率。
边栏推荐
- ES6 modular
- Analyse, capture et simulation du Protocole ARP par sniffer pro
- PLSQL problem solving ora-12154: tns: unable to resolve the specified connection identifier
- sniffer Pro对ARP协议的分析、捕获与模拟攻击
- sniffer Pro對ARP協議的分析、捕獲與模擬攻擊
- MySQL function string function / numeric function / date function / process control function
- 内网渗透笔记——:)一个笑脸
- [prettier] the code automatically formatted by prettier does not take effect
- Network security emergency response - common tools
- Idea send email
猜你喜欢

Intranet penetration notes - layer 2 Discovery

Intranet penetration notes - Sticky Keys and system command information collection

BUUCTF 荷兰宽带数据泄露

ES6--Set
![[NCTF2019]Fake XML cookbook](/img/d4/e5ff896108c86179526a06911abd38.png)
[NCTF2019]Fake XML cookbook

Compilation principle - lexical analyzer design

Garbage collection mechanism

sniffer Pro對ARP協議的分析、捕獲與模擬攻擊

JS downloads files according to binary data

Vue+axios+mysql realizes paging query, condition query and batch deletion
随机推荐
BUUCTF webshell后门
Idea send email
网络通信安全部分笔记二
DVA data flow
BUUCTF 面具下的flag
Create / delete tablespaces / users
[an Xun cup 2019]easy_ web
Open source internship | compiler sig internship task is officially released. Welcome to apply!
Flag under buuctf mask
ES6 -- arrow function
MSF Foundation
微信支付退款 PHP
Buuctf nine links
Componentized coding process -- todo list case
网络通信安全部分笔记一
Garbage collection mechanism
FTP upload file script description
[安洵杯 2019]easy_web
Intranet penetration notes --:) a smiling face
摩斯隐私计算一体机全项通过深圳国金测评中心83项测评