SpringBoot【十】mybatis之xml映射文件>、<=等特殊符号写法!

一、前言🔥

环境说明:Windows10 + Idea2021.3.2 + Jdk1.8 + SpringBoot 2.3.1.RELEASE

        在利用mybatis进行开发的时候,编写sql时可能少不了>=、<等比较符号,但是在mapper映射文件中直接使用是不行的,会报错,这时就需要将这些特殊符号进行一个转换,有两种转换方式,大家请看:

第一种:直接转义

大于  => &gt;
大于等于  => &gt;=
小于  => &lt;
小于等于  => &lt;=
不等于  => &lt;&gt;

具体咱们一个一个测试,好吧

原义sql:

select * from user where id>2;

则在UserMapper.xml 中,得这么写

select * from user where id &gt;2

测试一下 大于符号:

结果如下,控制台明显是进行了符号转义。

再测试一个 小于等于 符号:

原义sql:

select * from user where id<=2;

则在UserMapper.xml 中,得这么写

select * from user where id &lt;=2

结果如下,控制台明显也是进行了符号转义。

好啦,剩下的就你们自己写着试试啦,我这就不一一赘述了啊,好伐?

第二种:

1、先大致给大家介绍一下,如下判断符号对于的转义符.也很好记,大于小于跟数学符号一致,更换[ ]中的符号即可。

大于 =>  <![CDATA[ > ]]>

大于等于 =>  <![CDATA[ >= ]]>

小于 =>  <![CDATA[ < ]]>

 小于等于 =>  <![CDATA[ <= ]]>

不等于 => <![CDATA[ <> ]]>

2、给大家举例演示一下吧!

原义sql:

select * from user where id<=2;

.xml中使用:

select * from user where id  <![CDATA[ <= ]]> 2

执行结果如下:

如上执行sql打印结果与要执行的sql执行逻辑一致,说明转义成功了。再举例一个不等于 的sql吧,大家请看。

原义sql:

select * from user where id !=2;

.xml中使用:

select * from user where id  <![CDATA[ <> ]]> 2

执行结果如下:

       综上,使用如上两种,都很简单,大家根据自己的代码习惯选择而定,两种方式都没啥太大的区别,唯一区别就是写法略微不同而已啦。

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.qs.birp.jgAudit.JgAuditHeadDao"> <select id="getReqByReqNo" resultType="jg_audit_head" parameterType="String"> select * from jg_audit_head where request_no = #{request_no} limit 1; </select> <select id="showJgAuditHead" resultType="jg_audit_head"> SELECT * FROM jg_audit_head <where> and is_show = "1" <if test='request_no !=null and request_no !=""'>and request_no =#{request_no} </if> <if test='purchase_no !=null and purchase_no !=""'>and purchase_no =#{purchase_no} </if> <if test='project_number !=null and project_number !=""'>and project_number =#{project_number} </if> <if test='project_name !=null and project_name !=""'> and project_name like "%"#{project_name}"%"</if> <if test='company_code !=null and company_code !=""'>and company_code =#{company_code} </if> <if test='sssdwdm !=null and sssdwdm !=""'>and sssdwdm =#{sssdwdm}</if> <if test='ssxdwdm !=null and ssxdwdm !=""'>and ssxdwdm =#{ssxdwdm}</if> <if test='status !=null and status !=""'>and status =#{status}</if> <if test='company_code_values!=null and company_code_values.size()>0'> and company_code in <foreach collection="company_code_values" item="item" open="(" separator="," close=")"> #{item} </foreach> </if> </where> limit #{currIndex}, #{pageSize} ; </select> <select id="showJgAuditHeadCount" resultType="Integer"> SELECT count(*) FROM jg_audit_head <where> and is_show = "1" <if test='request_no !=null and request_no !=""'>and request_no =#{request_no} </if> <if test='purchase_no !=null and purchase_no !=""'>and purchase_no =#{purchase_no} </if> <if test='project_number !=null and project_number !=""'>and project_number =#{project_number} </if> <if test='project_name !=null and project_name !=""'> and project_name like "%"#{project_name}"%"</if> <if test='company_code !=null and company_code !=""'>and company_code =#{company_code} </if> <if test='sssdwdm !=null and sssdwdm !=""'>and sssdwdm =#{sssdwdm}</if> <if test='ssxdwdm !=null and ssxdwdm !=""'>and ssxdwdm =#{ssxdwdm}</if> <if test='status !=null and status !=""'>and status =#{status}</if> <if test='company_code_values!=null and company_code_values.size()>0'> and company_code in <foreach collection="company_code_values" item="item" open="(" separator="," close=")"> #{item} </foreach> </if> </where> </select> </mapper> 请你帮我分析 什么原因导致服务起不来
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值