首先说我的场景:我是在前台用layui框架的时间选择器,取到两个时间,开始时间和结束时间,在后台我把取到的两个日期2019-03-29 - 2019-03-31截取了出来,取成了两个字符分别是:2019-03-29,2019-03-31,然后转换成了Date,下面的代码图中能看到我转换之后的值是什么样子!然后报了一个莫名其妙的错误,很难受!是在是不知道哪里错了,求大神指教,看下图;这是后台接收到值转换并存入数据库的实现下面是mapper.xml中语句<!-- 模糊查询-->
<select id="fuzzySelectLogs" resultType="OaLog">
SELECT * FROM oa_log WHERE
<if test="name != null">
name LIKE CONCAT('%',#{name},'%')
</if>
<if test="startDate != null and endDate != null">
AND createdate <![CDATA[>=]]> #{startDate} AND createdate <![CDATA[<]]> #{endDate}
</if>
ORDER BY createdate DESC
<if test="limit!=null and page!=null">
LIMIT #{limit} OFFSET #{page}
</if>
</select>
<!-- 分页 -->
<select id="fuzzySelectLogsCount" resultType="int">
SELECT COUNT(*)FROM oa_log WHERE
<if test="name != null">
name LIKE CONCAT('%',#{name},'%')
</if>
<if test="startDate != null">
AND createdate <![CDATA[>=]]> #{startDate} AND createdate <![CDATA[<=]]> #{endDate}
</if>
</select>然后下面是报错!2019-03-31 05:04:56.652 DEBUG 1675 --- [nio-8081-exec-3] c.h.oasys.dao.IOaLogDao.fuzzySelectLogs : ==> Parameters:
(String), 2019-03-29 00:00:00.0(Timestamp), 2019-03-31 00:00:00.0(Timestamp), 10(Integer), 0(Integer)
2019-03-31 05:04:56,687 ERROR Statement:152 - {conn-10005, pstmt-20013} execute error. SELECT * FROM oa_log WHERE
name LIKE CONCAT('%',?,'%')
AND createdate >= ? AND createdate < ?
ORDER BY createdate DESC
LIMIT ? OFFSET ?
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near ' AND createdate >= '2019-03-29 00:00:00' AND createdate
< '2019-03-31 00' at line 6
这种报错实在是太诡异了!完全不知其所以然!求大神告知,重谢!!!!!!!!!!!!
3 回答

慕标琳琳
TA贡献1830条经验 获得超9个赞
<if test="startDate != null "> AND createdate >= #{startDate,jdbcType=DATE} </if> <if test="endDate != null "> AND createdate <= #{endDate,jdbcType=DATE} </if>
或者
<if test="startDate != null "> AND <![CDATA[ createdate >= #{startDate,jdbcType=DATE} ]]> </if> <if test="endDate != null "> AND <![CDATA[ createdate <= #{endDate,jdbcType=DATE} ]]> </if>
添加回答
举报
0/150
提交
取消