使用mybatis-plus需要知道的小点

< where>标签

  1. 配合使用
    标签需搭配标签使用,否则无意义,每个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>
  1. 语句一定要加< script>标签,不然语法稍微复杂点就不解析
  2. < 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%问题…

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值