元素内容必须由格式正确的字符数据或标记组成。
问题:偷懒用注解方式写sql的时候,遇到了这个错误。如下图:@Select({"<script> ","select * from coupon where over<=#{price} and startTime<=now() and now()<=endTime and id in ","<foreach item='item' index='index'
·
问题:
偷懒用注解方式写sql的时候,遇到了这个错误。如下图:
@Select({"<script> ",
"select * from coupon where over<=#{price} and startTime<=now() and now()<=endTime and id in ",
"<foreach item='item' index='index' collection='couponIds' open='(' separator=',' close=')'>",
"#{item}",
"</foreach>",
" order by discount desc",
"</script>"})
原因分析:
经过查找,使用<script>标签时,内部语句会以xml的形式解析,而 < 或者 > 会被系统认为是标签,而不是大于小于号,所以才会出现问题。
解决方案:
使用<![CDATA[ SQL语句 ]]> 将含有<、>、<=、>=的sql语句包含进去。
如下:
@Select({"<script> ",
"select * from coupon where <![CDATA[over<=#{price} and startTime<=now() and now()<=endTime]]> and id in ",
"<foreach item='item' index='index' collection='couponIds' open='(' separator=',' close=')'>",
"#{item}",
"</foreach>",
" order by discount desc",
"</script>"})
第二种方法:使用转移字符。
符号 | 转义字符 |
---|---|
< | < |
<= | <= |
> | > |
>= | >= |
如下:
@Select({"<script> ",
"select * from coupon where over <= #{price} and startTime <= now() and now() <= endTime and id in ",
"<foreach item='item' index='index' collection='couponIds' open='(' separator=',' close=')'>",
"#{item}",
"</foreach>",
" order by discount desc",
"</script>"})
更多推荐
已为社区贡献2条内容
所有评论(0)