Mybatis 多参处理

一.Mybatis 极简入门

二.Mybatis 级联查询

三.Mybatis 延迟加载

四.Mybatis 缓存策略

五.Mybatis 动态SQL

本篇:Mybatis 多参处理

  1. 一个参数怎么处理?
<select id="findClassedByID" parameterType="long" resultType="com.ibuyi.mybatis.entity.Classes">
        select * from classes where id=#{hello}
</select>

当只有一个参数时,#{}里面可以任意填写都能够取到传递进来的参数。

  1. 两个或多个参数怎么处理?
import com.ibuyi.mybatis.entity.User;

public interface UserDAO {
    User findUserByCityAndType(String city,int Type);

}

第一种使用param1,param2…或arg0,arg1…

<select id="findUserByCityAndType" resultType="com.ibuyi.mybatis.entity.User">
    select * from user where location=#{param1} and type=#{param2}
</select>

第二种使用Map,传入一个Map即可

public interface UserDAO {
    User findUserByCityAndType(Map<String,Object> map);

}

<select id="findUserByCityAndType" resultType="com.ibuyi.mybatis.entity.User">
    select * from user where location=#{location} and type=#{type}
</select>

第三种使用注解:

public interface UserDAO {
    User findUserByCityAndType(@Param("location")  String location,@Param("type") int type);

}

<select id="findUserByCityAndType" resultType="com.ibuyi.mybatis.entity.User">
    select * from user where location=#{location} and type=#{type}
</select>

如果我们使用Collection或者List或者array传递参数,应该怎么取出来呢?

public interface UserDAO {
    User findUserByCityAndType(List<Integer> list);

}
<select id="findUserByCityAndType" resultType="com.ibuyi.mybatis.entity.User">
    select * from user where id=#{list[0]}
</select>
public interface UserDAO {
    User findUserByCityAndType(int[] ids);

}

<select id="findUserByCityAndType" resultType="com.ibuyi.mybatis.entity.User">
    select * from user where id=#{array[0]}
</select>
原文地址:https://www.cnblogs.com/hzcya1995/p/13309400.html