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 = "";
        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);

你可能感兴趣的:(mybatis,java,mysql)