使用场景:
//虽然有>=和<= 的转义,但是我在实际使用过程中,会出现“org.xml.sax.SAXParseException: 引用了实体 "ge", 但未声明它。”的错误,后来使用>=和<=解决问题。
@Select({“<script>
+"<if test=\"startDt != null and startDt !='' and startDt.length() > 0 \">" +
" AND to_char(a.crt_time, 'YYYY-MM-DD') >= #{startDt} \n" +
"</if>" +
"<if test=\"endDt != null and endDt !='' and endDt.length() > 0 \">" +
" AND to_char(a.crt_time, 'YYYY-MM-DD') <= #{endDt} \n" +
"</if>" +
</script>“})
//如果不想转义,也可以用这种方式
and <![CDATA[to_char(a.crt_time, 'YYYY-MM-DD') >= #{startDt}]]>
在这种场景下,直接使用>或<出现“Caused by: org.xml.sax.SAXParseException: 元素内容必须由格式正确的字符数据或标记组成。”的错误,需要将>或< 进行转义。
| 显示结果 | 实体名称 |
实体编号 |
描述 |
|---|---|---|---|
| < | < | < | 小于号 |
| > | > | > | 大于号 |
| ≤ | ≤ | ≤ | 小于等于号 |
| ≥ | ≥ | ≥ | 大于等于号 |
| & | & | & | 和号 |
| " | " | " | 引号 |
| ' | ' | ' | 单引号(英文、IE不支持) |
| “ | “ | “ | 左双引号 |
| ” | ” | ” | 右双引号 |
| ‘ | ‘ | ‘ | 左单引号 |
| ’ | ’ | ’ | 右单引号 |
| | | | | 竖线Vertical bar | |
在@Select注解中采用script标签包围拼接SQL语句时不能在标签里有>大于或<小于符号出现,但是=好像是可以出现的。
//据说in要传list或者数组,还没验证
@Select({
"<script>",
"select",
"ip, content, note",
"from t_log",
"where note in",
"<foreach collection='notes' item='note' open='(' separator=',' close=')'>",
"#{note}",
"</foreach>",
"</script>"
})
List<TLog> getDataByNotes(@Param("notes") List<String> notes);
@Select({
"<script>",
"select",
"ip, content, note",
"from t_log",
"where note in",
"<foreach collection='notes' item='note' open='(' separator=',' close=')'>",
"#{note}",
"</foreach>",
"</script>"
})
List<TLog> getDataByNotes2(@Param("notes") String[] notes);
<choose></choose> 的等于的使用,
test='userType == "admin"'中,用==判断参数userType是否等于字符串 "admin"- 字符串常量需用双引号
"包裹(因外层用单引号'包裹整个test属性值)
@Select({
"<script>",
"SELECT id, username, user_type FROM sys_user",
"<choose>",
// 当 userType 等于 "admin" 时
"<when test='userType == \"admin\"'>",
"WHERE user_type = '管理员'",
"</when>",
// 当 userType 等于 "normal" 时
"<when test='userType == \"normal\"'>",
"WHERE user_type = '普通用户'",
"</when>",
// 其他情况(默认查询所有状态正常的用户)
"<otherwise>",
"WHERE status = 1",
"</otherwise>",
"</choose>",
"</script>"
})
List<User> selectByUserType(@Param("userType") String userType);
文章讲述了在使用Java的@Select注解构建SQL语句时,遇到的关于`>`和`<`符号转义问题,以及如何避免`org.xml.sax.SAXParseException`,确保正确处理实体引用和HTML标签的嵌入。
中大于小于号转义问题&spm=1001.2101.3001.5002&articleId=135928180&d=1&t=3&u=fcd7a3dacf0843548163b72b3c632a21)
950

被折叠的 条评论
为什么被折叠?



