农民 发表于 2024-11-26 15:53:24

MyBatis框架-动态SQL-XML中的常用标签+特别字符在XML中的显示

一、if标签、where标签、trim标签、choose标签、set标签、foreach标签
1、题目引入:where关键字和and关键字在动态SQL内里应该如何添加?
(1)if标签:
test属性的值是判断条件
if标签内里的内容是条件成立时添加到SQL语句中的字符串
(2)代码一:是否添加where关键字
<select id="findStudents" parameterType="Student" resultType="Student">
      select * from student where
      <if test="name!=null">
            name = #{name}
      </if>/*条件一*/
      <if test="gender!=null">
             gender = #{gender}
      </if>/*条件二*/
      <if test="phone!=null">
             phone= #{phone}
      </if>/*条件三*/
    </select>

[*]如果添加where关键字,如果三个条件全部成立,则where关键字背面将会没有查询条件,得到不正确的SQL语句
[*]如果不添加where关键字,如果三个条件中有成立的条件,没有where关键字,也会得到不正确的SQL语句
(3)代码二:是否添加and关键字
<select id="findStudents" parameterType="Student" resultType="Student">
      select * from student
      <if test="name!=null">
            name = #{name}
      </if>/*条件一*/
      <if test="gender!=null">
             and gender = #{gender}
      </if>/*条件二*/
      <if test="phone!=null">
             and phone = #{phone}
      </if>/*条件三*/
    </select>

[*]如果添加and关键字,如果只有条件二和条件三成立,那么得到的SQL语句前面多了一个and关键字,这条SQL语句为不正确的SQL语句
[*]如果不添加and关键字,如果只有条件二和条件三成立,那么得到的SQL语句的两个查询条件之间没有关键字进行连接,这条SQL语句为不正确的SQL语句
2、where标签解决动态SQL语句中的where关键字和and关键字的添加题目
(1)where标签:


[*]特点:
A、当where标签中的if标签的条件只要有一个成立,会在SQL语句的查询条件最前面自动添加一个where关键字
B、当where标签中成立的条件的开头有and/or关键字时,and/or关键字会被自动去除
 (2)代码验证:从数据库中查询所有的女同学信息
焦点代码:
<select id="findStudents" parameterType="Student" resultType="Student">
      select * from student
      <where>
            <if test="name!=null">
                name = #{name}
            </if>
            <if test="gender!=null">
                and gender = #{gender}
            </if>
            <if test="phone!=null">
                and phone = #{phone}
            </if>
      </where>
    </select> 测试代码:
SqlSession sqlSession=MybatisUtil.getSqlSession();
      StudentDao studentDao=sqlSession.getMapper(StudentDao.class);
      Student student=new Student();
      student.setGender("女");
      List<Student>list=studentDao.findStudents(student);
      System.out.println(list);
      sqlSession.close(); 结果输出:
https://i-blog.csdnimg.cn/direct/0d756960bda94ce5a84f7b943cd5da97.png
3、trim标签解决动态SQL语句中的where关键字和and关键字的添加题目
(1)trim标签:


[*]prefix属性:其值是要在条件前面添加的指定的前缀,只有当有条件成立时该前缀才会自动添加到条件前面
[*]prefixOverrides属性:其值是条件语句开头要覆盖掉的关键字,如果条件语句开头存在该关键字,那么该关键字将会被去除
(2)代码:查询电话号为123456的学生的信息
焦点代码:
<select id="findStudents" parameterType="Student" resultType="Student">
      select * from student
      <trim prefix="where" prefixOverrides="and|or">
            <if test="name!=null">
                name = #{name}
            </if>
            <if test="gender!=null">
                and gender = #{gender}
            </if>
            <if test="phone!=null">
                or phone = #{phone}
            </if>
      </trim>
    </select>  测试代码:
SqlSession sqlSession=MybatisUtil.getSqlSession();
      StudentDao studentDao=sqlSession.getMapper(StudentDao.class);
      Student student=new Student();
      student.setPhone("123456");
      List<Student>list=studentDao.findStudents(student);
      System.out.println(list);
      sqlSession.close(); 结果输出:
https://i-blog.csdnimg.cn/direct/d99a87dc594046a4b78818731c084045.png
4、choose标签解决简化不同条件查询同一类对象的题目
(1)choose标签:choose标签内里有when标签和otherwise标签两种标签。其中when标签有一个test属性,其值为判断条件,当该条件满足时会将when标签中的内容作为结果进行返回。而otherwise标签则是当其前面的when标签中的条件都不被满足时才会将otherwise标签中的内容作为结果进行返回。choose标签所实现的作用等效于if-else if-else
(3)代码:根据指定的姓名或电话号查询学生表
焦点代码:
<select id="findStudents" parameterType="Student" resultType="Student">
      select * from student
      <trim prefix="where" prefixOverrides="and|or">
            <choose>
                <when test="name!=null">
                  name = #{name}
                </when>
                <when test="phone!=null">
                  phone = #{phone}
                </when>
                <otherwise>
                  gender = '男'
                </otherwise>
            </choose>
      </trim>
    </select> 测试代码1:
/*student.setName("tom");*/
student.setPhone("178942"); 运行结果1:
https://i-blog.csdnimg.cn/direct/cfb12458259448d2bc8f6c4c9482567d.png
测试代码2:
/*student.setName("tom");*/
student.setPhone("178942"); 运行结果2:
https://i-blog.csdnimg.cn/direct/1127feb6ec944f85ac3cfb88ca8a1122.png
测试代码3:
/*student.setName("tom");*/
/*student.setPhone("178942");*/ 运行结果3:
https://i-blog.csdnimg.cn/direct/2b7377ff817f4fa3afc72555943c543b.png
5、set标签解决updateSQL语句在实现多属性修改同类对象只需一个update标签所带来的开头缺set关键字和末了逗号多余的题目(实现修改同一类对象只必要一个update标签加一个修改方法)
(1)set标签:set标签的作用是当set标签中有结果返回时会在返回结果的开头加上set关键字并且去除返回结果末了的逗号
(2)代码:
焦点代码1:
<update id="updateStudent" parameterType="Student">
      update student
      <set>
            <if test="name!=null">
                name=#{name},
            </if>
            <if test="gender!=null">
                gender=#{gender},
            </if>
            <if test="phone!=null">
                phone=#{phone}
            </if>
      </set>
      where id=#{id}
    </update> 测试代码 1:修改学生的姓名和性别
SqlSession sqlSession=MybatisUtil.getSqlSession();
StudentDao studentDao=sqlSession.getMapper(StudentDao.class);
Student student = new Student();
student.setName("liming");
student.setGender("男");
student.setId(9);
studentDao.updateStudent(student);
sqlSession.commit();
sqlSession.close(); 运行结果1: 
https://i-blog.csdnimg.cn/direct/63f49d033fd14167b82151cc88f46afa.png
焦点代码2:
<update id="updateStudent" parameterType="Student">
      update student
      <trim prefix="set" suffixOverrides=",">
            <if test="name!=null">
                name=#{name},
            </if>
            <if test="gender!=null">
                gender=#{gender},
            </if>
            <if test="phone!=null">
                phone=#{phone}
            </if>
      </trim>
      where id=#{id}
    </update> 测试代码 1:修改学生的电话号码
SqlSession sqlSession=MybatisUtil.getSqlSession();
      StudentDao studentDao=sqlSession.getMapper(StudentDao.class);
      Student student = new Student();
      student.setPhone("24680");
      student.setId(9);
      studentDao.updateStudent(student);
      sqlSession.commit();
      sqlSession.close(); 运行结果2: 
https://i-blog.csdnimg.cn/direct/8da5c0a5a6aa4f19af39138ffd506b99.png
6、foreach标签解决在构建in条件时在SQL语句中迭代集合的题目
(1)foreach标签


[*]item属性的值是集合中的元素进行迭代时的别名
[*]index属性的值是集合中元素进行迭代时迭代到的位置
[*]collection属性的值是集合的类型:list/array
[*]open属性的值是语句的开始字符串
[*]separator属性的值是元素进行迭代时元素之间的分隔符
[*]close属性的值是语句的结束字符串
(2)代码:
焦点代码1:
<delete id="deleteStudents">
      delete from student
      <where>
            <foreach collection="list" open="id in(" item="id" separator="," close=")" >
                #{id}
            </foreach>
      </where>
    </delete> 测试代码1:在学生表中删除id为6,10的学生的信息
SqlSession sqlSession=MybatisUtil.getSqlSession();
      StudentDao studentDao=sqlSession.getMapper(StudentDao.class);
      List<Integer> ids = Arrays.asList(6,10);
      studentDao.deleteStudents(ids);
      sqlSession.commit();
      sqlSession.close(); 运行结果1:
https://i-blog.csdnimg.cn/direct/869efe82e9984a44a876471888c0055c.png
焦点代码2:
<select id="findStudents1" resultType="Student">
      select
      <foreach collection="array" item="colName" separator=",">
            ${colName}
      </foreach>
      from student
    </select> 测试代码2:查询学生的指定属性列(1个或多个属性列)
SqlSession sqlSession=MybatisUtil.getSqlSession();
      StudentDao studentDao=sqlSession.getMapper(StudentDao.class);
      String[] colNames = {"gender","phone"};
      List<Student>list=studentDao.findStudents1(colNames);
      System.out.println(list);
      sqlSession.close(); 运行结果2:
https://i-blog.csdnimg.cn/direct/2e8f166e6e9f4f488022b26e48450150.png
二、特别符号在XML文件中的显示
1、在MyBatis中的XML文件中,要想显示特别符号<、>、"、'、&,一种方法是对这些符号进行转义,以转义字符的情势在XML文件中进行显示
select * from student where id
      <if test="phone!=null &amp; gender!=null">
            > #{id}
      </if> 特别字符        转义字符<        <>>""'&pos;&&  另一种方法是利用<!]>包裹特别字符在XML文件中进行显示
select * from student where id <!]> 10 2、<!]>
(1)<!]>是XML语法,在CDATA中的所有内容都会被解析器忽略。
(2)如if标签、where标签等的标签如果在CDATA中都不会被解析,因此我们只把有特别字符且无标签的内容放在<!]>中。
(3)<!]>不能在双标签的头标签中被利用,否则程序会堕落。
<if test="phone!=null <!]> gender!=null">
            > #{id}
      </if> https://i-blog.csdnimg.cn/direct/b55e02e85be84405b61ae7d57aa9fe45.png

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: MyBatis框架-动态SQL-XML中的常用标签+特别字符在XML中的显示