Mybatis自定义的AbstractMethod方法注入后调用提示 Parameter ‘XXXXX‘ not found

因为业务需要,mybatis自带的逻辑删除is_delete,领导在这个基础上要求所有的表统一添加删除时间delete_at记录什么时候删除的,自带的boolean removeById(Serializable id) 方法并不能满足业务需求。

结合之前记录的文章《Mybatis Plus自定义IService与BaseMapper 》,用这个方式统一添加了一个自定义的方法:

/**
     * 逻辑删除并且标记删除时间
     * @param id
     * @return
     */
    boolean removeByIdMarkDeleteAt(Serializable id);

相关实现:

public class RemoveByIdMarkDeleteAt extends AbstractMethod {

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        String sql;
        String sqlFormat = "<script>\nUPDATE %s %s, delete_at=#{deleteAt} WHERE %s=#{%s} %s\n</script>";
        String methodName = "removeByIdMarkDeleteAt";
        if (tableInfo.isLogicDelete()) {
            sql = String.format(sqlFormat, tableInfo.getTableName(), sqlLogicSet(tableInfo),
                    tableInfo.getKeyColumn(), tableInfo.getKeyProperty(),
                    tableInfo.getLogicDeleteSql(true, true));
            SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, Object.class);
            return addUpdateMappedStatement(mapperClass, modelClass, methodName, sqlSource);
        } else {
            SqlMethod sqlMethod = SqlMethod.DELETE_BY_ID;
            sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), tableInfo.getKeyColumn(),
                    tableInfo.getKeyProperty());
            SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, Object.class);
            return this.addDeleteMappedStatement(mapperClass, methodName, sqlSource);
        }
    }
}
``

```java
public class MySqlInjector extends DefaultSqlInjector {

    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass) {

        //拿到父类的getMethodList方法
        List<AbstractMethod> methodList = super.getMethodList(mapperClass);

        //自定义包含逻辑删除的更新
        methodList.add(new MyUpdateById());
        methodList.add(new MySelectByIdWithOutLogic());
        methodList.add(new MyListByIdsWithOutLogic());
        methodList.add(new RemoveByIdMarkDeleteAt());
        methodList.add(new RemoveByIdsMarkDeleteAt());


        return methodList;
    }
}
public interface MyBaseMapper<T> extends BaseMapper<T> {

    int updateByIdWithLogicDelete(@Param(Constants.ENTITY) T entity);

    T selectByIdWithOutLogic(Serializable id);

    List<T> listByIdsWithOutLogic(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);

    int removeByIdMarkDeleteAt(Serializable id, Date deleteAt);

    int removeByIdsMarkDeleteAt(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList, Date deleteAt);

}
public interface IHnCmpBaseService<T> extends IService<T> {

    /**
     * 对updateById的升级,包含逻辑删除字段的更新
     * @param entity
     * @return
     */
    boolean updateByIdWithLogicDelete(T entity);


    /**
     * 对list()方法的升级
     * @param entity
     * @return
     */
    List<T> listByEntity(T entity);

    /**
     * 获取详情
     * @param entity
     * @return
     */
    T getOne(T entity);

    /**
     * 逻辑删除并且标记删除时间
     * @param id
     * @return
     */
    boolean removeByIdMarkDeleteAt(Serializable id);

    /**
     * 逻辑删除并且标记删除时间
     *
     * @param idList 主键ID列表
     */
    boolean removeByIdsMarkDeleteAt(Collection<? extends Serializable> idList);


}

在使用的时候,由于调用的表比较特殊没有使用自增主键id,而是使用的自定义主键productCode,导致调用的时候,提示org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter ‘productCode’ not found. Available parameters are [deleteAt, id, param1, param2]

解决方式,其他逻辑保持不变,在该表的mapper.java类做如下处理:

@Override
    int removeByIdMarkDeleteAt(@Param("productCode") Serializable id, Date deleteAt);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值