Mybatis-Plus中的and和or用法

矫情吗;* 2023-09-27 12:50 249阅读 0赞

先看Mybatis-Plus官网中对这两个关键字用法的介绍

c73f7d7b8b9840e08da24d8bd9c428ba.png

数据库文件:

链接:https://pan.baidu.com/s/1KzY32Jq0srDQU9m-a-YtBQ?pwd=rsdg
提取码:rsdg

表数据:

比如我们想查age等于23并且school_id等于300的

sql语句为:select * FROM student where age=’23’ or school_id=’300’

最直接的方法:

List students = studentMapper

.selectList(Wrappers.lambdaQuery(Student.class)

.eq(Student::getAge, “23”)

.or()

.eq(Student::getSchoolId,300));

也可以用下面的方法

用mybatis-plus则为:

  1. List<Student> students = studentMapper.selectList(Wrappers.lambdaQuery(Student.class)
  2. .eq(Student::getAge, "23")
  3. .or(s -> s.eq(Student::getSchoolId, 300)));

比如我们想查 age等于25或者姓张的同学

sql语句:select * FROM student where age=’25’ or `name` LIKE ‘张%’

用mybatis-plus最直接的方法

  1. List<Student> students = studentMapper
  2. .selectList(Wrappers.lambdaQuery(Student.class)
  3. .eq(Student::getAge, "25")
  4. .or()
  5. .likeRight(Student::getName, "张"));

也可以用以下方法

  1. List<Student> students = studentMapper
  2. .selectList(Wrappers.lambdaQuery(Student.class)
  3. .eq(Student::getAge, "25")
  4. .or(s->s.likeRight(Student::getName,"张")));

发表评论

表情:
评论列表 (有 0 条评论,249人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Python and or

    在Python中 None,False,空字符串””,0,空列表[],空字典{},空元组()都相当于False,在布尔上下文中为假;其它任何东西都为真 or:是从左到右...