mybatis:给mapper接口中的方法传多个参数

问题

下面给mapper接口中的方法selectManyUsers传两个参数(type 和homeTown),在sql provider类UserSqlBuilder的方法中直接引用#{type}、#{homeTown}报错:

package com.thb.mapper;

import java.util.HashMap;
import java.util.List;

import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.jdbc.SQL;

import com.thb.model.User;

public interface UserMapper {

    @Results(value = {
        @Result(property = "id", column = "id", id = true),
        @Result(property = "userName", column = "user_name"),
        @Result(property = "homeTown", column = "home_town"),
        @Result(property = "type", column = "type"),
    })
    @SelectProvider(type = UserSqlBuilder.class, method = "selectManyUsers")   
    List<User> selectManyUsers(int type , String homeTown);

    public static class UserSqlBuilder {
        public static String selectManyUsers(final int type, final String homeTown) {
            return new SQL() {{
                SELECT("id, user_name, home_town, type");
                FROM("user");
                WHERE("type = #{type}");
                WHERE("home_town = #{homeTown}");
                //OFFSET(rowBounds.getOffset());
                //LIMIT(rowBounds.getLimit());
            }}.toString();
        }
    }
}

运行报错:
在这里插入图片描述
意思是没有发现type这个参数,可用的参数是[arg1, arg0, param1, param2]。

解决方法一(不推荐):在代码中用param1、param2这样的名字来引用

在代码中引用的时候使用#{param1}、#{param2}这样的名字,这样是正确的。并且,mapper接口中定义的参数和sql provider中对应方法的参数必须对应,即,mapper接口中如果定义了2个参数,那么sql provider中对应方法也必须是2个参数,不能少。
显然,这种处理方法的缺点是不直观,所以不建议用。

例如,下面的代码:
在这里插入图片描述

现在运行就正确了:
在这里插入图片描述

解决方法二(推荐):在mapper接口方法中使用Param注解

在mapper接口方法中使用Param注解,指定参数的名字,在代码中就可以用参数的名字来引用了。
采用这种处理方法,sql provider方法参数的个数可以比mapper接口方法中参数的个数少。例如,mapper接口方法中定义了2个参数,在sql provider方法中可以用1个参数、2个参数。

在这里插入图片描述

package com.thb.mapper;

import java.util.HashMap;
import java.util.List;

import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.jdbc.SQL;

import com.thb.model.User;

public interface UserMapper {

    @Results(value = {
        @Result(property = "id", column = "id", id = true),
        @Result(property = "userName", column = "user_name"),
        @Result(property = "homeTown", column = "home_town"),
        @Result(property = "type", column = "type"),
    })
    @SelectProvider(type = UserSqlBuilder.class, method = "selectManyUsers")    
    List<User> selectManyUsers(@Param("type")int type, @Param("homeTown")String homeTown);

    public static class UserSqlBuilder {
        public static String selectManyUsers(@Param("type")final int type, @Param("homeTown")final String homeTown) {
            return new SQL() {{
                SELECT("id, user_name, home_town, type");
                FROM("user");
                WHERE("type = #{type}");
                WHERE("home_town = #{homeTown}");
            }}.toString();
        }
    }
}

下面的代码也是正确的(在sql provider的方法中只用到了1个参数):

package com.thb.mapper;

import java.util.HashMap;
import java.util.List;

import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.jdbc.SQL;

import com.thb.model.User;

public interface UserMapper {

    @Results(value = {
        @Result(property = "id", column = "id", id = true),
        @Result(property = "userName", column = "user_name"),
        @Result(property = "homeTown", column = "home_town"),
        @Result(property = "type", column = "type"),
    })
    @SelectProvider(type = UserSqlBuilder.class, method = "selectManyUsers")    
    List<User> selectManyUsers(@Param("type")int type, @Param("homeTown")String homeTown);

    public static class UserSqlBuilder {
        public static String selectManyUsers(@Param("type")final int type) {
            return new SQL() {{
                SELECT("id, user_name, home_town, type");
                FROM("user");
                WHERE("type = #{type}");
            }}.toString();
        }
    }
}
  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值