处理大于小于号的方法:
https://www.cnblogs.com/winner-0715/p/6132755.html
第一种方法:
用转义字符把">"和"<"替换掉&#xff0c;就没有问题了。
<if test&#61;"startTime !&#61; null ">
AND order_date >&#61; #{startTime,jdbcType&#61;DATE}
if>
<if test&#61;"endTime !&#61; null ">
AND order_date <&#61; #{endTime,jdbcType&#61;DATE}
if>
注意下&#xff0c;这里的startTime&#xff0c;endTime都是Date类型的~
附&#xff1a;XML转义字符
< | < | 小于号 |
> | > | 大于号 |
& | & | 和 |
' | ’ | 单引号 |
" | " | 双引号 |
第二种方法&#xff1a;
因为这个是xml格式的&#xff0c;所以不允许出现类似">"这样的字符&#xff0c;但是可以使用符号进行说明&#xff0c;将此类符号不进行解析
mapper文件示例代码
<if test&#61;"startTime !&#61; null ">
AND <![CDATA[ order_date >&#61; #{startTime,jdbcType&#61;DATE} ]]>
if>
<if test&#61;"endTime !&#61; null ">
AND <![CDATA[ order_date <&#61; #{endTime,jdbcType&#61;DATE} ]]>
if>
&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;
附带问题&#xff1a;
使用情况&#xff1a;mybatis xml中写的mapper 对接的是postgresql数据库
问题&#xff1a;在同一个项目中不同的mapper.xml文件中&#xff0c;分别出现了>&#61; 和<&#61;的比较运算符&#xff0c;但是在一个xml中需要额外处理才能使用&#xff0c;一个xml文件中不需要额外处理>或者<符号可以直接使用
下面附上两个xml文件代码和截图&#xff0c;
1.
<?xml version&#61;"1.0" encoding&#61;"UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace&#61;"com.rollong.chinatower.server.persistence.mapper.FinanceReportMapper">
<resultMap id&#61;"financeBriefDataResultMap"
type&#61;"com.rollong.chinatower.server.api.payload.finance.FinanceBriefData">
<id property&#61;"id" column&#61;"id"/>
resultMap>
<select id&#61;"search" resultMap&#61;"financeBriefDataResultMap">
SELECT
*
FROM
(
SELECT
( CAST ( partner.id AS VARCHAR ) || &#39;&#64;partner&#39; ) 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 &#61; construction.id
LEFT JOIN "project"."projects" AS project ON construction.project_id &#61; project.id
LEFT JOIN "finance"."finance_reports" as report on partner.id &#61; report.partner_id
WHERE
#{isConstruction} &#61; TRUE
AND construction.deleted_at IS NULL
<if test&#61;"null !&#61; partnerType">
AND partner.name &#61; &#39;${partnerType}&#39;
if>
<if test&#61;"null !&#61; provinceId">
AND project.province_id &#61; #{provinceId}
if>
<if test&#61;"null !&#61; cityId">
AND project.city_id &#61; #{cityId}
if>
<if test&#61;"null !&#61; districtId">
AND project.district_id &#61; #{districtId}
if>
<if test&#61;"null !&#61; subCompanyId">
AND project.sub_company_id &#61; #{subCompanyId}
if>
<if test&#61;"null !&#61; thirdPartCompanyId">
AND partner.third_part_id &#61; #{thirdPartCompanyId}
if>
<if test&#61;"null !&#61; leaderEmployeeId">
AND partner.leader_employee_id &#61; #{leaderEmployeeId}
if>
<if test&#61;"null !&#61; workerEmployeeId">
AND exists(
select 1 from "third_part"."worker_involved_constructions" as involved
where involved.worker_id &#61; #{workerEmployeeId} AND involved.project_partner_id &#61; partner.id
)
if>
<if test&#61;"null !&#61; reportStatus">
AND exists(
select 1 from "finance"."finance_reports" as r
where r.partner_id &#61; partner.id
<choose>
<when test&#61;"&#39;Submit&#39; &#61;&#61; reportStatus">
and r.approved_at is null and r.rejected_at is null and r.paid_at is null
when>
<when test&#61;"&#39;Approved&#39; &#61;&#61; reportStatus">
and r.approved_at is not null and r.rejected_at is null and r.paid_at is null
when>
<when test&#61;"&#39;Rejected&#39; &#61;&#61; reportStatus">
and r.approved_at is null and r.rejected_at is not null and r.paid_at is null
when>
<when test&#61;"&#39;Paid&#39; &#61;&#61; reportStatus">
and r.paid_at is not null
when>
choose>
)
if>
<if test&#61;"null !&#61; keyword">
AND project.name like #{keyword}
if>
GROUP BY partner.id,project.id,construction.id
UNION
SELECT
( CAST ( taker.ID AS VARCHAR ) || &#39;&#64;maintenance&#39; ) 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 &#61; maintenance.id
LEFT JOIN "project"."projects" AS project ON maintenance.project_id &#61; project.id
LEFT JOIN "finance"."finance_reports" as report on taker.id &#61; report.taker_id
WHERE
#{isMaintenance} &#61; TRUE
AND maintenance.deleted_at IS NULL
AND taker.deleted_at IS NULL
<if test&#61;"null !&#61; provinceId">
AND project.province_id &#61; #{provinceId}
if>
<if test&#61;"null !&#61; cityId">
AND project.city_id &#61; #{cityId}
if>
<if test&#61;"null !&#61; districtId">
AND project.district_id &#61; #{districtId}
if>
<if test&#61;"null !&#61; subCompanyId">
AND project.sub_company_id &#61; #{subCompanyId}
if>
<if test&#61;"null !&#61; thirdPartCompanyId">
AND taker.third_part_id &#61; #{thirdPartCompanyId}
if>
<if test&#61;"null !&#61; leaderEmployeeId">
AND taker.leader_employee_id &#61; #{leaderEmployeeId}
if>
<if test&#61;"null !&#61; workerEmployeeId">
AND exists(
select 1 from "third_part"."worker_involved_maintenance" as involved
where involved.worker_id &#61; #{workerEmployeeId} AND involved.taker_id &#61; taker.id
)
if>
<if test&#61;"null !&#61; reportStatus">
AND exists(
select 1 from "finance"."finance_reports" as r
where r.taker_id &#61; taker.id
<choose>
<when test&#61;"&#39;Submit&#39; &#61;&#61; reportStatus">
and r.approved_at is null and r.rejected_at is null and r.paid_at is null
when>
<when test&#61;"&#39;Approved&#39; &#61;&#61; reportStatus">
and r.approved_at is not null and r.rejected_at is null and r.paid_at is null
when>
<when test&#61;"&#39;Rejected&#39; &#61;&#61; reportStatus">
and r.approved_at is null and r.rejected_at is not null and r.paid_at is null
when>
<when test&#61;"&#39;Paid&#39; &#61;&#61; reportStatus">
and r.paid_at is not null
when>
choose>
)
if>
<if test&#61;"null !&#61; keyword">
AND project.name like #{keyword}
if>
GROUP BY maintenance.id,project.id,taker.id
) AS table_all
WHERE TRUE
<if test&#61;"minReportsCount !&#61; null">
AND table_all.reportsCount >&#61; #{minReportsCount}
if>
<if test&#61;"maxReportsCount !&#61; null">
AND table_all.reportsCount <&#61; #{maxReportsCount}
if>
<choose>
<when test&#61;"sort &#61;&#61; &#39;Latest&#39;">
ORDER BY table_all.reportLatestUpdatedAt DESC
when>
<when test&#61;"sort &#61;&#61; &#39;AmountAsc&#39;">
ORDER BY table_all.totalAmount ASC
when>
<when test&#61;"sort &#61;&#61; &#39;AmountDesc&#39;">
ORDER BY table_all.totalAmount DESC
when>
choose>
select>
mapper>
View Code
2.
<?xml version&#61;"1.0" encoding&#61;"UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace&#61;"com.rollong.chinatower.server.persistence.mapper.OrderMealTimeMapper">
<resultMap id&#61;"OrderMealResult"
type&#61;"com.rollong.chinatower.server.api.payload.canteen.OrderMealTime" autoMapping&#61;"true">
<result property&#61;"years" column&#61;"years"/>
<result property&#61;"months" column&#61;"months"/>
<result property&#61;"days" column&#61;"days"/>
<result property&#61;"counts" column&#61;"counts"/>
<result property&#61;"checkOut" column&#61;"checkOut"/>
resultMap>
<select id&#61;"search" resultMap&#61;"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&#61;"null !&#61; canteenId">
AND ors.canteen_id &#61; #{canteenId}
if>
<if test&#61;" null !&#61; startY and null !&#61; startM and null !&#61; startD" >
AND <![CDATA[ (ors.year_meal *10000 &#43; ors.month_meal *100 &#43; ors.day_meal) >&#61; (#{startY}*10000&#43;#{startM}*100&#43;#{startD}) ]]>
if>
<if test&#61;" null !&#61; endY and null !&#61; endM and null !&#61; endD" >
AND <![CDATA[ (ors.year_meal *10000 &#43; ors.month_meal *100 &#43; ors.day_meal) <&#61; (#{endY}*10000&#43;#{endM}*100&#43;#{endD}) ]]>
if>
GROUP BY ors.year_meal,ors.month_meal,ors.day_meal
select>
mapper>
View Code
【究竟是xml的问题/还是对接的数据库的问题/还是数据库中对于某些类型字段处理不一样】
如果有兴趣或者刚好知道&#xff0c;遇到过这种情况的 希望大家能给个反馈&#xff0c;多多交流&#xff01;&#xff01;