[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
1、当使用SQL语句查询某个时间段的数据时,我们很自然的会想到使用between…and..来操作,但是如果使用between...and...
这个方法来查询某个时间段的数据时是需要传入两个参数的,一个是起始时间,另一个是结束时间,且两个参数必须要同时存在才能使用between...and...,
而我们希望的是只传入一个参数(起始时间或者结束时间)就能进行查询。但是在使用MyBatis时如果只传入一个参数,则相应的SQL语句是不会执行的,所以不能使用between
...and... 来进行某个时间段数据的查询。
2、可以使用" >= ... AND ... <=" 来解决。
例如:SELECT * FROM o_info WHERE create_date >= '2019-03-10 17:04:04' AND
create_date <= '2019-03-15 17:04:28';
但是若要在MyBatis中使用,有一点需要注意:在MyBatis中使用">"和"<"时会提示"The content of elements must
consist of well-formed character data or markup.
"这个错误,原因是使用的">"和"<"不符合xml的书写规范,所以需要把">"和"<"使用<![CDATA[ ]]>标签括起来才行。
使用<![CDATA[ ]]>标签后的代码:
<if test="beginDate != null and beginDate != ''">AND stock_bill.bill_date
<![CDATA[>=]]> #{beginDate}</if> <if test="endDate != null and endDate !=
''">AND stock_bill.bill_date <![CDATA[<=]]>#{endDate}</if>
这样就可以实现只选择开始时间或者只选择结束时间,同时选择开始时间和结束时间来进行查询了。
3、如果查询时出现异常提示### Error querying database. Cause:
java.lang.IllegalArgumentException: invalid comparison: java.util.Date and
java.lang.String ,是因为传入的参数类型是日期类型,但是在<if test="beginDate != null and beginDate
!= ''">和<if test="endDate != null and endDate != ''">
却用这个日期类型的参数与空字符串进行了比较,所以出现了程序运行时异常,解决方法是将与空字符串比较的判断去掉,只保留非空判断即可。
4、最终的代码:
<if test="beginDate != null">AND stock_bill.bill_date <![CDATA[>=]]>
#{beginDate}</if> <if test="endDate != null">AND stock_bill.bill_date
<![CDATA[<=]]>#{endDate}</if>