背景
因需要链表操作,所以采用了@select注解来写sql,抛弃了传统的xml形式。
1.错误性示范代码
@Select({"<script>",
"select a.*,b.uuid,b.denoter_name as denoterName,b.spelling,b.public_name as publicName from t_denoter_qrcode a right join t_bz_denoter b on a.denoter_uuid=b.denoter_uuid ",
"where 1=1 ",
"<if test='qrcodeRequest.denoterAddress !=null and qrcodeRequest.denoterAddress!="" '>" ,
" and a.address like concat('%',#{qrcodeRequest.denoterAddress},'%')" ,
"</if> ",
"<if test='qrcodeRequest.denoterName !=null and qrcodeRequest.denoterName!="" '>" ,
" and b.denoter_name like concat('%',#{qrcodeRequest.denoterName},'%') " ,
"</if> ",
"<if test='qrcodeRequest.publicName !=null and qrcodeRequest.publicName!="" '> " ,
" and b.public_name like concat('%',#{qrcodeRequest.publicName},'%')" ,
"</if> ",
" <if test='qrcodeRequest.xzqhdm !=null and qrcodeRequest.xzqhdm!="" '>" ,
" and b.zzzz9998 like concat(#{qrcodeRequest.xzqhdm},'%') " ,
"</if> ",
"</script>"})
List<DenoterQrcodeEntity> denoterQueryList(@Param("qrcodeRequest") QrcodeRequest qrcodeRequest);
会报如下错误:
if元素类型必须后跟属性规范、">“或"/>”
这是因为我们在写sql时候没有转义导致的!
2、正确的写法,看仔细哦
@Select({"<script>",
"select a.*,b.uuid,b.denoter_name as denoterName,b.spelling,b.public_name as publicName from t_denoter_qrcode a right join t_bz_denoter b on a.denoter_uuid=b.denoter_uuid ",
"where 1=1 ",
"<if test='qrcodeRequest.denoterAddress !=null and qrcodeRequest.denoterAddress!=\"\" '>" ,
" and a.address like concat('%',#{qrcodeRequest.denoterAddress},'%')" ,
"</if> ",
"<if test='qrcodeRequest.denoterName !=null and qrcodeRequest.denoterName!=\"\" '>" ,
" and b.denoter_name like concat('%',#{qrcodeRequest.denoterName},'%') " ,
"</if> ",
"<if test='qrcodeRequest.publicName !=null and qrcodeRequest.publicName!=\"\" '> " ,
" and b.public_name like concat('%',#{qrcodeRequest.publicName},'%')" ,
"</if> ",
"<if test='qrcodeRequest.xzqhdm !=null and qrcodeRequest.xzqhdm!=\"\" '>" ,
" and b.zzzz9998 like concat(#{qrcodeRequest.xzqhdm},'%') " ,
"</if> ",
"</script>"})
List<DenoterQrcodeEntity> denoterQueryList(@Param("qrcodeRequest") QrcodeRequest qrcodeRequest);
可以看到上面写法中,在判断空字符串的时候加入了转义,这样就可以解决问题!