[MyBatis]org.apache.ibatis.binding.BindingException的避免

我遇到的org.apache.ibatis.binding.BindingException问题是因为Mapper.java中接口和SQL的参数多于一个,Mybatis不知道如何一一对应,解决方法是加上@param注解,手动告诉MyBatis如何去对应。代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hy.mapper.SweepawayMapper">
 
     <select id="selectCleanExpiredParams" resultType="java.lang.String">
        select name||':'||value from Clean_Expired_Params order by sort
    </select>
 
    <select id="findTableExist"  resultType="integer">
        SELECT COUNT (*) as cnt FROM ALL_TABLES WHERE table_name = UPPER(#{tableName})
    </select>
    
    <select id="getAllExpiredCount"  resultType="integer">
        SELECT COUNT (*) as cnt from ${tableName} where created_datetime&lt;to_date(#{date},'yyyy-MM-dd') 
    </select>
    
    <select id="getExpiredCount"  resultType="integer">
        SELECT COUNT (*) as cnt from ${tableName} where created_datetime&lt;to_date(#{date},'yyyy-MM-dd')  AND ROWNUM&lt;#{rowNum}
    </select>
    
    <delete id="sweepAwayExpired">
        delete from ${tableName} where created_datetime&lt;to_date(#{date},'yyyy-MM-dd') AND ROWNUM&lt;#{rowNum}
    </delete>  
  
</mapper>

而接口的写法是:

package com.hy.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Param;

@MyMapper
public interface SweepawayMapper {
    List<String> selectCleanExpiredParams();
    int findTableExist(String tableName);// 一个参数MyBati能认出来
    int getAllExpiredCount(@Param("tableName") String tableName,@Param("date") String date);// 多个参数得告诉MyBatis如何区分
    int getExpiredCount(@Param("tableName") String tableName,@Param("date") String date,@Param("rowNum") int rowNum);
    int sweepAwayExpired(@Param("tableName") String tableName,@Param("date") String date,@Param("rowNum") int rowNum);
}

--END-- 19/10/17 8:21

原文地址:https://www.cnblogs.com/heyang78/p/11689823.html