热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

【Mybatis】Mybatis在xml文件中处理大于号小于号的方法【问题】

处理大于小于号的方法:https:www.cnblogs.comwinner-0715p6132755.html第一种方法:用转义字符把和


处理大于小于号的方法:


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


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



<if test&#61;"startTime !&#61; null ">
AND order_date &gt;&#61; #{startTime,jdbcType&#61;DATE}
if>
<if test&#61;"endTime !&#61; null ">
AND order_date &lt;&#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 &gt;&#61; #{minReportsCount}
if>
<if test&#61;"maxReportsCount !&#61; null">
AND table_all.reportsCount &lt;&#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;


 


转载于:https://www.cnblogs.com/sxdcgaq8080/p/8672487.html



推荐阅读
  • 本文介绍了如何通过C#语言调用动态链接库(DLL)中的函数来实现IC卡的基本操作,包括初始化设备、设置密码模式、获取设备状态等,并详细展示了将TextBox中的数据写入IC卡的具体实现方法。 ... [详细]
  • 在1995年,Simon Plouffe 发现了一种特殊的求和方法来表示某些常数。两年后,Bailey 和 Borwein 在他们的论文中发表了这一发现,这种方法被命名为 Bailey-Borwein-Plouffe (BBP) 公式。该问题要求计算圆周率 π 的第 n 个十六进制数字。 ... [详细]
  • 二维码的实现与应用
    本文介绍了二维码的基本概念、分类及其优缺点,并详细描述了如何使用Java编程语言结合第三方库(如ZXing和qrcode.jar)来实现二维码的生成与解析。 ... [详细]
  • 问题描述现在,不管开发一个多大的系统(至少我现在的部门是这样的),都会带一个日志功能;在实际开发过程中 ... [详细]
  • 本文详细介绍了JQuery Mobile框架中特有的事件和方法,帮助开发者更好地理解和应用这些特性,提升移动Web开发的效率。 ... [详细]
  • CentOS下ProFTPD的安装与配置指南
    本文详细介绍在CentOS操作系统上安装和配置ProFTPD服务的方法,包括基本配置、安全设置及高级功能的启用。 ... [详细]
  • 深入理解:AJAX学习指南
    本文详细探讨了AJAX的基本概念、工作原理及其在现代Web开发中的应用,旨在为初学者提供全面的学习资料。 ... [详细]
  • HTML:  将文件拖拽到此区域 ... [详细]
  • 流处理中的计数挑战与解决方案
    本文探讨了在流处理中进行计数的各种技术和挑战,并基于作者在2016年圣何塞举行的Hadoop World大会上的演讲进行了深入分析。文章不仅介绍了传统批处理和Lambda架构的局限性,还详细探讨了流处理架构的优势及其在现代大数据应用中的重要作用。 ... [详细]
  • 本文介绍了如何在两个Oracle数据库(假设为数据库A和数据库B)之间设置DBLink,以便能够从数据库A中直接访问和操作数据库B中的数据。文章详细描述了创建DBLink前的必要准备步骤以及具体的创建方法。 ... [详细]
  • 本文详细探讨了在Web开发中常见的UTF-8编码问题及其解决方案,包括HTML页面、PHP脚本、MySQL数据库以及JavaScript和Flash应用中的乱码问题。 ... [详细]
  • 本文探讨了在SQL Server中处理几何类型列时遇到的INTERSECT操作限制,并提供了解决方案,包括通过转换数据类型和使用额外表结构的方法。 ... [详细]
  • 本文将从基础概念入手,详细探讨SpringMVC框架中DispatcherServlet如何通过HandlerMapping进行请求分发,以及其背后的源码实现细节。 ... [详细]
  • Bootstrap Paginator 分页插件详解与应用
    本文深入探讨了Bootstrap Paginator这款流行的JavaScript分页插件,提供了详细的使用指南和示例代码,旨在帮助开发者更好地理解和利用该工具进行高效的数据展示。 ... [详细]
  • 在尝试通过自定义端口部署Spring Cloud Eureka时遇到了连接失败的问题。本文详细描述了问题的现象,并提供了有效的解决方案,以帮助遇到类似情况的开发者。 ... [详细]
author-avatar
沈晓燕-东东
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有