当前位置: 首页 > news >正文

JavaEE简单示例——动态SQL元素<where>

简单介绍:

在我们之前使用where关键字进行查询的时候,总是会在后面添加一个1=1恒等式,并且在每一个可能拼接的SQL语句前面都加上一个and关键字,防止当后续的所有条件都不满足的时候,where关键字在最后直接跟and的时候也能不报错。在本章节的学习中,我么将要学习一个新的标签<where>可以帮助我们在我们拼接SQL语句的时候i,灵活的添加或者不添加where关键字。

使用方法:

<select id="唯一标识" resultType="结果集映射的实体类">

        select * from student

        <where>

                <if test="判断条件">

                        and 需要拼接的SQL语句

                </if>

                <if test="判断条件">

                        and 需要拼接的SQL语句

                </if>

        </where>

</select>

代码实现:

SQL映射文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="Mappers.dynamicSql"><select id="selectByIdOrName" parameterType="student" resultType="student">select * from student where 1=1
#                             当id的值不等于null并且id的值不是空字符的时候,就会拼接这个SQL语句<if test="id != null and id != ''">and id = #{id}</if>
#                             当name的值不等于null的时候并且name的值不是空字符串的时候,就会拼接这个SQL语句<if test="name != null and name != ''">
#                             注意这个地方是使用了一个concat函数将模糊匹配的百分号和参数进行拼接,在使用的时候注意这个地方不要写错and name like concat ('%',#{name},'%')</if></select><select id="selectAll" resultType="student">select * from student;</select>
<!--    动态SQL中的choose元素-->
<!--    当查询的条件满足第一个when的时候,就拼接第一个when里面的SQL语句-->
<!--    当查询的条件满足第二个when的时候,就拼接第二个when里面的SQL语句-->
<!--    当所有的when都不满足的时候,就拼接otherwise里面的SQL语句-->
<!--    当有多个when里面的条件都满足的时候,就拼接最靠上的一条SQL语句,并且不会执行其他when里面的语句--><select id="selectStudentByIdAndName" resultType="student" >select * from student where 1=1<choose><when test="name != null and name != ''">and name like concat('%',#{name},'%')</when><when test="id != null and id != ''">and id = #{id}</when><otherwise>and password is not null</otherwise></choose></select>
<!--    使用<where>来动态的处理where关键字是否添加--><select id="selectByIdAndWhere" resultType="student">select * from student<where><if test="name != null and name !=''">and name like concat('%',#{name},'%')</if><if test="id != null and id !=''">and id = #{id}</if></where></select>
</mapper>

接口文件:

package Mappers;import com.mybatis.POJO.student;import java.util.List;public interface dynamicSql {List<student> selectByIdOrName(student s);List<student> selectStudentByIdAndName(student s);List<student> selectByIdAndWhere(student s);
}

测试类:

package Mappers;import com.mybatis.POJO.Tools.createSqlSession;
import com.mybatis.POJO.student;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;import java.util.List;public class dynamicSqlTest {@Testpublic void selectByIdOrName(){SqlSession sqlSession = new createSqlSession().create();dynamicSql dynamicSql = new createSqlSession().createdynamicSql();student s = new student();s.setId(1);s.setName("张三");List<student> stu = sqlSession.selectList("Mappers.dynamicSql.selectByIdOrName", s);for(student student : stu){System.out.println(student.toString());}}@Testpublic void selectStudentByIdAndName(){dynamicSql dynamicSql = new createSqlSession().createdynamicSql();student s =new student();
//        s.setId(1);
//        s.setName("张三");for (student student : dynamicSql.selectStudentByIdAndName(s)) {System.out.println(student);}}@Testpublic void selectAll(){SqlSession sqlSession = new createSqlSession().create();dynamicSql dynamicSql = new createSqlSession().createdynamicSql();List<student> list = sqlSession.selectList("Mappers.dynamicSql.selectAll");for(student student : list){System.out.println(student.toString());}}@Testpublic void selectByIdAndWhere(){SqlSession sqlSession = new createSqlSession().create();dynamicSql dynamicSql = new createSqlSession().createdynamicSql();student s = new student();
//        s.setId(1);s.setName("张三");for (student student : dynamicSql.selectByIdAndWhere(s)) {System.out.println(student.toString());}}
}

运行结果:

当我们使用where标签的时候,即使SQL语句中没有where关键字,MyBatis也会自动的帮助我们进行添加,同样的,如果我们的where关键字后面没有任何的查询语句,MyBatis会帮我们删除多余的where关键字。

当我们正常进行查询的时候:

可以看到我们的查询语句后面是没有where关键字的,接着我们来运行程序:

 

可以看到是可以正常的查询的,说明MyBatis在我们的查询语句后面插入了一个where关键字帮我们构建出了一个完整的SQL语句

如果我们if条件都达不到的时候,也就是说where后面没有任何需要拼接的时候,where还会不会自动添加?:

运行结果:

 

结果依然正常的显示了,说明where关键字并没有添加,这就证明了<where>标签可以动态的自动识别是否应该添加where关键字。 

注意点:

在这个案例中,需要注意的就是<where>标签的位置和<select>的嵌套关系,以及在注意在拼接SQL关键字的时候需要遗漏and关键字。如果出现了运行报错的情况,优先考虑是否是<where>的位置和SQL拼接的问题。

如果出现了查询结果不符合猜想的情况优先考虑是否是因为在SQL拼接的时候遗漏了某些关键字或者是条件判断的错误

http://www.lryc.cn/news/15942.html

相关文章:

  • 本地事务详解
  • e2e测试-Cypress 使用
  • 20230222 【梳理】肿瘤检测 预处理+ML+DL
  • 经典文献阅读之--MSC-VO(曼哈顿和结构约束VIO)
  • 华为OD机试真题Python实现【字母计数】真题+解题思路+代码(20222023
  • 在中国市场,假如Teradata像Nutanix那样“退出操作”,谁来“接盘”呢?
  • 使用vs2022编译yolov5+tensorRT+cuda+cudnn代码进行混合编译
  • 记一次:request请求总结
  • 2023年全国最新会计专业技术资格精选真题及答案2
  • 每日英语-20230221
  • 学习系统编程No.4【环境变量】
  • 通过Docker部署rancher
  • 【二叉树】
  • 华为OD机试 - 入栈出栈(C++) | 附带编码思路 【2023】
  • 【设计模式】对象行为型模式
  • 「TCG 规范解读」第11章 TPM工作组 TCG算法注册表
  • 华为OD机试 - 事件推送(C++) | 附带编码思路 【2023】
  • Java ”框架 = 注解 + 反射 + 设计模式“ 之 注解详解
  • 【拦截器、过滤器、springAop】那些不为人知的隐秘
  • 记录charles手机端配置https的成功过程
  • 你知道这几种常见的JVM调优场景吗?
  • 华为OD机试真题Python实现【最长连续子串】真题+解题思路+代码(20222023)
  • Vue使用distpicker插件实现省市级下拉框三级联动
  • Unity Avatar Foot IK - Avatar Foot Placement Resolution
  • 是时候告别这些 Python 库了
  • nodejs基于vue论坛交流管理系统
  • 企业电子招投标采购系统源码之系统的首页设计
  • 华为OD机试真题Python实现【竖直四子棋】真题+解题思路+代码(20222023)
  • LeetCode 73. 矩阵置零
  • 「TCG 规范解读」第10章 TPM工作组 保护你的数字环境