Student search = new Student(); search.setName("明");
System.out.println("只有名字时的查询"); List studentsByName = studentMapper.selectByStudentSelective(search); for (int i = 0; i < studentsByName.size(); i++) { System.out.println(ToStringBuilder.reflectionToString(studentsByName.get(i), ToStringStyle.MULTI_LINE_STYLE)); }
search.setName(null); search.setSex((byte) 1); System.out.println("只有性别时的查询"); List studentsBySex = studentMapper.selectByStudentSelective(search); for (int i = 0; i < studentsBySex.size(); i++) { System.out.println(ToStringBuilder.reflectionToString(studentsBySex.get(i), ToStringStyle.MULTI_LINE_STYLE)); }
System.out.println("姓名和性别同时存在的查询"); search.setName("明"); List studentsByNameAndSex = studentMapper.selectByStudentSelective(search); for (int i = 0; i < studentsByNameAndSex.size(); i++) { System.out.println(ToStringBuilder.reflectionToString(studentsByNameAndSex.get(i), ToStringStyle.MULTI_LINE_STYLE)); }
sqlSession.commit(); sqlSession.close(); }
只有名字时的查询, 发送的语句和结果查询的条件只发送了
where 1=1 and name like concat('%', ?, '%')
只有性别时的查询, 发送的语句和结果查询的条件只发送了
where 1=1 and sex=?
姓名和性别同时存在的查询, 发送的语句和结果查询条件
where 1=1 and name like concat('%', ?, '%') and sex=?
2.2 在 UPDATE 更新列中使用 if 标签
有时候我们不希望更新所有的字段, 只更新有变化的字段。
2.2.1 更新条件
只更新有变化的字段, 空值不更新。
2.2.1 动态 SQL
接口方法
/** * 更新非空属性 */ int updateByPrimaryKeySelective(Student record);
Student search = new Student(); search.setName("明");
System.out.println("只有名字时的查询"); List studentsByName = studentMapper.selectByStudentSelectiveWhereTag(search); for (int i = 0; i < studentsByName.size(); i++) { System.out.println(ToStringBuilder.reflectionToString(studentsByName.get(i), ToStringStyle.MULTI_LINE_STYLE)); }
search.setSex((byte) 1); System.out.println("姓名和性别同时存在的查询"); List studentsBySex = studentMapper.selectByStudentSelectiveWhereTag(search); for (int i = 0; i < studentsBySex.size(); i++) { System.out.println(ToStringBuilder.reflectionToString(studentsBySex.get(i), ToStringStyle.MULTI_LINE_STYLE)); }
System.out.println("姓名和性别都不存在时查询"); search.setName(null); search.setSex(null); List studentsByNameAndSex = studentMapper.selectByStudentSelectiveWhereTag(search); for (int i = 0; i < studentsByNameAndSex.size(); i++) { System.out.println(ToStringBuilder.reflectionToString(studentsByNameAndSex.get(i), ToStringStyle.MULTI_LINE_STYLE)); }
sqlSession.commit(); sqlSession.close(); }
只有名字时的查询, 有 where姓名和性别同时存在的查询, 有 where姓名和性别都不存在时查询, 此时, where 不会再出现了。
4.2 set
set 标签也类似, 在 [2.2 在 UPDATE 更新列中使用 if 标签] 中, 如果我们的方法 updateByPrimaryKeySelective 没有使用 标签, 那么我们就要想办法处理字段全为空的条件, 字段不为空的条件等。有了这个, 我们只需要写 if 标签即可, 不需要处理类似的问题。
4.3 trim
set 和 where 其实都是 trim 标签的一种类型, 该两种功能都可以使用 trim 标签进行实现。
4.3.1 trim 来表示 where
如以上的 where 标签, 我们也可以写成
"where" prefixOverrides="AND |OR">
表示当 trim 中含有内容时, 添加 where, 且第一个为 and 或 or 时, 会将其去掉。而如果没有内容, 则不添加 where。
4.3.2 trim 来表示 set
相应的, set 标签可以如下表示
"SET" suffixOverrides=",">
表示当 trim 中含有内容时, 添加 set, 且最后的内容为 , 时, 会将其去掉。而没有内容, 不添加 set
4.3.3 trim 的几个属性
prefix: 当 trim 元素包含有内容时, 增加 prefix 所指定的前缀
prefixOverrides: 当 trim 元素包含有内容时, 去除 prefixOverrides 指定的 前缀
suffix: 当 trim 元素包含有内容时, 增加 suffix 所指定的后缀
suffixOverrides:当 trim 元素包含有内容时, 去除 suffixOverrides 指定的后缀
List ids = new LinkedList<>(); ids.add(1); ids.add(3);
List students = studentMapper.selectByStudentIdList(ids); for (int i = 0; i < students.size(); i++) { System.out.println(ToStringBuilder.reflectionToString(students.get(i), ToStringStyle.MULTI_LINE_STYLE)); }
List students = new LinkedList<>(); Student stu1 = new Student(); stu1.setName("批量01"); stu1.setPhone("13888888881"); stu1.setLocked((byte) 0); stu1.setEmail("13888888881@138.com"); stu1.setSex((byte) 1); students.add(stu1);