处理大于小于号的方法:

https://www.cnblogs.com/winner-0715/p/6132755.html

第一种方法:
用转义字符把">"和"<"替换掉,就没有问题了。

<if test="startTime != null ">
AND order_date &gt;= #{startTime,jdbcType=DATE}
</if>
<if test="endTime != null ">
AND order_date &lt;= #{endTime,jdbcType=DATE}
</if>

注意下,这里的startTime,endTime都是Date类型的~

附:XML转义字符

&lt;      <    小于号   
&gt;      >    大于号   
&amp;      &    和   
&apos;      ’    单引号   
&quot;      "    双引号   

第二种方法:
因为这个是xml格式的,所以不允许出现类似">"这样的字符,但是可以使用<![CDATA[ ]]>符号进行说明,将此类符号不进行解析 
mapper文件示例代码

<if test="startTime != null ">
AND <![CDATA[ order_date >= #{startTime,jdbcType=DATE} ]]>
</if>
<if test="endTime != null ">
AND <![CDATA[ order_date <= #{endTime,jdbcType=DATE} ]]>
</if>

====================================

附带问题:

使用情况:mybatis  xml中写的mapper  对接的是postgresql数据库

问题:在同一个项目中不同的mapper.xml文件中,分别出现了>= 和<=的比较运算符,但是在一个xml中需要额外处理才能使用,一个xml文件中不需要额外处理>或者<符号可以直接使用

下面附上两个xml文件代码和截图,

1.

<?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.rollong.chinatower.server.persistence.mapper.FinanceReportMapper">
<resultMap id="financeBriefDataResultMap"
type="com.rollong.chinatower.server.api.payload.finance.FinanceBriefData">
<id property="id" column="id"/>
</resultMap>
<select id="search" resultMap="financeBriefDataResultMap">
SELECT
*
FROM
(
SELECT
( CAST ( partner.id AS VARCHAR ) || '@partner' ) AS ID,
partner.id AS partnerId,
project.id AS projectId,
construction.id AS constructionId,
NULL AS takerId,
NULL AS maintenanceId,
count(report.id) as reportsCount,
sum(report.amount) as totalAmount,
max(report.updated_at) as reportLatestUpdatedAt
FROM
"project"."project_construction_partners" AS partner
LEFT JOIN "project"."project_construction" AS construction ON partner.construction_id = construction.id
LEFT JOIN "project"."projects" AS project ON construction.project_id = project.id
LEFT JOIN "finance"."finance_reports" as report on partner.id = report.partner_id
WHERE
#{isConstruction} = TRUE
AND construction.deleted_at IS NULL
<if test="null != partnerType">
AND partner.name = '${partnerType}'
</if>
<if test="null != provinceId">
AND project.province_id = #{provinceId}
</if>
<if test="null != cityId">
AND project.city_id = #{cityId}
</if>
<if test="null != districtId">
AND project.district_id = #{districtId}
</if>
<if test="null != subCompanyId">
AND project.sub_company_id = #{subCompanyId}
</if>
<if test="null != thirdPartCompanyId">
AND partner.third_part_id = #{thirdPartCompanyId}
</if>
<if test="null != leaderEmployeeId">
AND partner.leader_employee_id = #{leaderEmployeeId}
</if>
<if test="null != workerEmployeeId">
AND exists(
select 1 from "third_part"."worker_involved_constructions" as involved
where involved.worker_id = #{workerEmployeeId} AND involved.project_partner_id = partner.id
)
</if>
<if test="null != reportStatus">
AND exists(
select 1 from "finance"."finance_reports" as r
where r.partner_id = partner.id
<choose>
<when test="'Submit' == reportStatus">
and r.approved_at is null and r.rejected_at is null and r.paid_at is null
</when>
<when test="'Approved' == reportStatus">
and r.approved_at is not null and r.rejected_at is null and r.paid_at is null
</when>
<when test="'Rejected' == reportStatus">
and r.approved_at is null and r.rejected_at is not null and r.paid_at is null
</when>
<when test="'Paid' == reportStatus">
and r.paid_at is not null
</when>
</choose>
)
</if>
<if test="null != keyword">
AND project.name like #{keyword}
</if>
GROUP BY partner.id,project.id,construction.id
UNION
SELECT
( CAST ( taker.ID AS VARCHAR ) || '@maintenance' ) AS ID,
NULL AS partnerId,
project.ID AS projectId,
NULL AS constructionId,
taker.ID AS takerId,
maintenance.ID AS maintenanceId,
count(report.id) as reportsCount,
sum(report.amount) as totalAmount,
max(report.updated_at) as reportLatestUpdatedAt
FROM
"project"."project_maintenance_takers" AS taker
LEFT JOIN "project"."project_maintenance" AS maintenance ON taker.maintenance_id = maintenance.id
LEFT JOIN "project"."projects" AS project ON maintenance.project_id = project.id
LEFT JOIN "finance"."finance_reports" as report on taker.id = report.taker_id
WHERE
#{isMaintenance} = TRUE
AND maintenance.deleted_at IS NULL
AND taker.deleted_at IS NULL
<if test="null != provinceId">
AND project.province_id = #{provinceId}
</if>
<if test="null != cityId">
AND project.city_id = #{cityId}
</if>
<if test="null != districtId">
AND project.district_id = #{districtId}
</if>
<if test="null != subCompanyId">
AND project.sub_company_id = #{subCompanyId}
</if>
<if test="null != thirdPartCompanyId">
AND taker.third_part_id = #{thirdPartCompanyId}
</if>
<if test="null != leaderEmployeeId">
AND taker.leader_employee_id = #{leaderEmployeeId}
</if>
<if test="null != workerEmployeeId">
AND exists(
select 1 from "third_part"."worker_involved_maintenance" as involved
where involved.worker_id = #{workerEmployeeId} AND involved.taker_id = taker.id
)
</if>
<if test="null != reportStatus">
AND exists(
select 1 from "finance"."finance_reports" as r
where r.taker_id = taker.id
<choose>
<when test="'Submit' == reportStatus">
and r.approved_at is null and r.rejected_at is null and r.paid_at is null
</when>
<when test="'Approved' == reportStatus">
and r.approved_at is not null and r.rejected_at is null and r.paid_at is null
</when>
<when test="'Rejected' == reportStatus">
and r.approved_at is null and r.rejected_at is not null and r.paid_at is null
</when>
<when test="'Paid' == reportStatus">
and r.paid_at is not null
</when>
</choose>
)
</if>
<if test="null != keyword">
AND project.name like #{keyword}
</if>
GROUP BY maintenance.id,project.id,taker.id
) AS table_all
WHERE TRUE
<if test="minReportsCount != null">
AND table_all.reportsCount &gt;= #{minReportsCount}
</if>
<if test="maxReportsCount != null">
AND table_all.reportsCount &lt;= #{maxReportsCount}
</if>
<choose>
<when test="sort == 'Latest'">
ORDER BY table_all.reportLatestUpdatedAt DESC
</when>
<when test="sort == 'AmountAsc'">
ORDER BY table_all.totalAmount ASC
</when>
<when test="sort == 'AmountDesc'">
ORDER BY table_all.totalAmount DESC
</when>
</choose>
</select>
</mapper>

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.rollong.chinatower.server.persistence.mapper.OrderMealTimeMapper">
<resultMap id="OrderMealResult"
type="com.rollong.chinatower.server.api.payload.canteen.OrderMealTime" autoMapping="true">
<result property="years" column="years"/>
<result property="months" column="months"/>
<result property="days" column="days"/>
<result property="counts" column="counts"/>
<result property="checkOut" column="checkOut"/>
</resultMap>
<select id="search" resultMap="OrderMealResult">
SELECT
ors.year_meal AS years,
ors.month_meal AS months,
ors.day_meal AS days,
SUM (ors.counts) AS counts,
SUM (ors.checkd_out) AS checkOut
FROM canteen.order_meal ors
WHERE TRUE
<if test="null != canteenId">
AND ors.canteen_id = #{canteenId}
</if>
<if test=" null != startY and null != startM and null != startD" >
AND <![CDATA[ (ors.year_meal *10000 + ors.month_meal *100 + ors.day_meal) >= (#{startY}*10000+#{startM}*100+#{startD}) ]]>
</if>
<if test=" null != endY and null != endM and null != endD" >
AND <![CDATA[ (ors.year_meal *10000 + ors.month_meal *100 + ors.day_meal) <= (#{endY}*10000+#{endM}*100+#{endD}) ]]>
</if>
GROUP BY ors.year_meal,ors.month_meal,ors.day_meal
</select>
</mapper>

【究竟是xml的问题/还是对接的数据库的问题/还是数据库中对于某些类型字段处理不一样】

如果有兴趣或者刚好知道,遇到过这种情况的 希望大家能给个反馈,多多交流!!

【Mybatis】 Mybatis在xml文件中处理大于号小于号的方法【问题】的更多相关文章

  1. MyBatis 3在XML文件中处理大于号小于号(<>)的方法(转)

    说明:以下方式支持XML和注解的方式. 一. 用了转义字符把>和<替换掉. AND start_date <= CURRENT_DATE AND end_date >= CUR ...

  2. Mybatis在xml文件中处理大于、小于、不等于号的方法

    在mapper.xml使用大于.小于等符号会和xml语法冲突,解决冲突有两种方式. 方法一: 使用转义字符: 字符名称 字符符号 转义字符 大于号 > > 小于号 < < 与 ...

  3. maven的setting.xml文件中只配置本地仓库路径的方法

    maven的setting.xml文件中只配置本地仓库路径的方法 即:settings标签下只有一个 localRepository标签,其他全部注释掉即可 <?xml version=&quo ...

  4. mybatis 基础(二) xml文件中的其他知识点

    mybatis xml文件中一些标签的使用 此标签主要用作 配置 "别名" 如果实体类与数据库中字段名在不区分大小写的情况下相同的话, 那就不需要配置resultMap,因为mys ...

  5. 转!!mybatis在xml文件中处理大于号小于号的方法

    第一种方法: 用了转义字符把>和<替换掉,然后就没有问题了. SELECT * FROM test WHERE 1 = 1 AND start_date  <= CURRENT_DA ...

  6. Mybatis在xml文件中处理大于号小于号的方法

    第一种方法:用了转义字符把">"和"<"替换掉,然后就没有问题了. AND start_date <= CURRENT_DATE AND en ...

  7. 【转】mybatis在xml文件中处理大于号小于号的方法

    http://blog.csdn.net/zheng0518/article/details/10449549 第一种方法: 用了转义字符把>和<替换掉,然后就没有问题了. SELECT ...

  8. 在AndroidManifest.xml文件中设置Android程序的启动界面方法

    从网上搜集了一堆的Android代码,比如Android的Login程序和Android的Helloworld程序,但是却总不能正确运行一个正确的程序,郁闷了很久,终于在一次一次的测试后成功的在And ...

  9. iBATIS sql(XML)中的大于、小于、like等符号写法

    其实就是xml的特殊符号,因为它的配置就是xml,所以可以用下面这种写法转义 <          <     >          >      <>   < ...

随机推荐

  1. 使用Spring Cloud需要了解一些概念

    Spring Cloud是一个基于Spring Boot实现的微服务架构开发工具,它为基于JVM的微服务开发中的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁.决策竞选.分布式会话和集 ...

  2. 【树形背包】bzoj4033: [HAOI2015]树上染色

    仔细思考后会发现和51nod1677 treecnt有异曲同工之妙 Description 有一棵点数为N的树,树边有边权.给你一个在0~N之内的正整数K,你要在这棵树中选择K个点,将其染成黑色,并 ...

  3. MySQL binlog-server搭建

    MySQL binlog-server搭建 binlog在备份中起着至关重要的作用,备份binlog文件时,只能先在本地备份,然后才能传送到远程服务器上.从MySQL5.6版本后,可以利用mysqlb ...

  4. (7)zabbix资产清单inventory管理

    概述 监控的设备越来越多,有时候搞不清楚哪台服务器是什么配置,大多公司有自己的资产清单,要去专门的系统查询显得多少有点麻烦.为此,zabbix专门设置了设备资产管理功能. 我们创建或编辑主机的时候,可 ...

  5. js常见面试题

    1.大小写转化,将字符串转化成驼峰的方法 例:border-bottom-color转化为:borderBottomColor var str="border-bottom-color&qu ...

  6. perl学习之:正则表达式

  7. leepcode作业解析-5-15日

    1.删除排序数组中的重复项 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外 ...

  8. 编辑器sublime(转)摘自网络

    一.下载和安装 Sublime Text2是一款开源的软件,不需要注册即可使用(虽然没有注册会有弹窗,但是基本不影响使用). 下载地址:http://www.sublimetext.com/,请自行根 ...

  9. app启动画面(prepo)

    IPhone启动画面以及图标的设置 目前IPhone的分辨率为:320X480.640X960.640X1136. Default.png                    320X480 iPh ...

  10. ubuntu 终端查看图片(eog)

    远程登陆服务器的话,是没有办法直接查看图片的,这时我们需要进入图片所在目录,然后通过终端命令查看图片. 想要查看图片,需要通过ssh -X登陆,然后在终端输入命令: eog 图片名