mapper接口层的代码:
@Select("<script>"
+ "SELECT DISTINCT(b.MSG_ID) from BD_APP_MSG_USER a "
+" LEFT JOIN BD_APP_MSG_SEND b ON A .MSG_ID = b.MSG_ID"
+" where A .RECV_CD = #{userName} "
+" <if test=\"userType != null\">and A.USR_TYP =#{userType}</if>"
+" and b.UNREAD ='0'"
+ "</script>")
@Options(flushCache=true,timeout=20000)
public List<Map<String, String>> queryUnreadMsg(String userName,String userType);
解决办法:
加一个@Param("userType")就好了:
@Select("<script>"
+ "SELECT DISTINCT(b.MSG_ID) from BD_APP_MSG_USER a "
+" LEFT JOIN BD_APP_MSG_SEND b ON A .MSG_ID = b.MSG_ID"
+" where A .RECV_CD = #{userName} "
+" <if test=\"userType != null\">and A.USR_TYP =#{userType}</if>"
+" and b.UNREAD ='0'"
+ "</script>")
@Options(flushCache=true,timeout=20000)
public List<Map<String, String>> queryUnreadMsg(String userName,@Param("userType") String userType);
关于参数是否参与过滤的写法:
1、if test中的参数名称不需要加${};
2、${}和#{}的区别
#{}是占位符,比${}更安全
如果是in查询,一定需要用${},注意构造参数的格式如单引号