< where>标签
- 配合使用
标签需搭配标签使用,否则无意义,每个if语句中有test条件,只有当条件为true时才会拼接该语句。
一般每个 里都会加个and,这样就保证如果多个if语句拼接,中间会有and连接。
@Select("<script>" +
" ... " +
" <where> " +
" <if test='dto.platformId != null and dto.platformId != \"\"'> " +
" and cip.platformId = #{dto.platformId} " +
" </if>" +
" <if test='dto.landingPageNo != null and dto.landingPageNo != \"\"'> " +
" and JSON_CONTAINS(sp.plan_info, JSON_OBJECT('planName', #{dto.landingPageNo})) " +
" </if>" +
" </where>" +
"</script>")
标签会自动去掉where语句中首位and或or,因此不用担心会出现
where and cip.platformId = #{dto.platformId}
有些人会使用下面这种写法来确保这个问题不会出现,但其实没必要:
where 1 = 1
<if test='dto.platformId != null and dto.platformId != \"\"'>
and cip.platformId = #{dto.platformId}
</if>
- 语句一定要加< script>标签,不然语法稍微复杂点就不解析
- < where>标签不解析 ->没加< script>标签
sql没问题也会报错
确定sql没有问题,表没问题,Java代码没问题,仍然报错:Failed to process, please exclude the tableName or statementId.
可以加@SqlParser(filter = true)注解,让该条sql不参与解析,同时需要指定数据源@DynamicDataSource(指定数据源)。如果需要拼接业务线,手动拼一下。
@TableLogic
只有在实体类某字段加了该注解,mybatis-plus逻辑删除才会生效。
其余会直接物理删除。
最常见的报错
Failed to process, please exclude the tableName or statementId.
可能原因:多加逗号;少加逗号;传入空值;打错表名;打错列名…
提交之前在query console执行以下,能解决90%问题…