• [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

  • 相关阅读:
    RestTemplate的异常:Not enough variables available to expand
    WebApplicationContext类的作用
    select动态绑定vue.js
    spring的 @Scheduled的cron表达式
    Spring使用webjar
    ThreadLocal基本原理及运用
    mybatis choose标签的使用
    @RequestBody和@RequestParam区别
    js遍历 for-of
    MySql 模糊查询
  • 原文地址:https://www.cnblogs.com/heyang78/p/11689823.html
Copyright © 2020-2023  润新知