引用地址:https://blog.csdn.net/qq_45228323/article/details/117024332
1. 单参数方法
单参数方法的参数详解, 主要介绍不同类型的参数,在Mapper映射文件中的配置方式.
参数为: 简单类型(基本数据类型+String)
参数为: 对象类型
参数为: List集合类型-List
参数为: List集合类型-List
参数为: 数组类型
参数为: Set集合类型
参数为: Map集合类型
1.1 简单类型
1.1.1 方法示例
public User findUserById(int id);
1.1.2 映射配置
<!-- public User findUserById(int id); -->
<select id="findUserById" resultType="com.shuai.domain.User">
select * from user where id = #{id}
</select>
<!-- 如果只有一个参数,且参数是基本数据类型,则#{}中,可以随便写. 如: #{aaa}也可以 -->
1.2 对象类型
1.2.1 方法示例
public List<User> findUserByUsernameAndPassword(User user);
1
1.2.2 映射配置
<!-- public List<User> findUserByUsernameAndPassword(User user); -->
<select id="findUserByUsernameAndPassword" resultType="com.shuai.domain.User">
select * from user where username=#{username} and password=#{password}
</select>
<!-- 如果只有一个参数,且参数是对象类型,则#{}中,必须书写对象的属性名称 -->
1.3 List集合-List
1.3.1 方法示例
public List<User> findUserByList(List<Integer> ids);
1
1.3.2 映射配置
<!-- List<User> findUserByList(List<Integer> ids); -->
<select id="findUserByList" resultType="com.shuai.domain.User">
select * from user
<where>
<if test="_parameter != null and _parameter.size() > 0">
<foreach collection="list" open="and id in (" close=")" item="id" separator=",">
#{id}
</foreach>
</if>
</where>
</select>
1.4 List集合-List
1.4.1 方法示例
List<User> findByListUser(List<User> users);
1
1.4.2 映射配置
<!-- List<User> findByListUser(List<User> users); -->
<select id="findByListUser" resultType="com.shuai.domain.User" >
select * from user
<where>
<if test="_parameter != null and _parameter.size() > 0">
<foreach collection="list" open="and id in (" close=")" item="u" separator=",">
#{u.id}
</foreach>
</if>
</where>
</select>
1.5 数组类型
1.5.1 方法示例
List<User> findUserByArray(Integer[] ids);
1.5.2 映射配置
<!-- List<User> findUserByArray(Integer[] ids); -->
<select id="findUserByArray" resultType="com.shuai.domain.User" >
select * from user
<where>
<if test="_parameter != null">
<foreach collection="array" open="and id in (" close=")" item="id" separator=",">
#{id}
</foreach>
</if>
</where>
</select>
1.6 Set集合
1.6.1 方法示例
List<User> findUserBySet(@Param("set") Set<Integer> ids)
1.6.2 映射配置
<!-- List<User> findUserBySet(@Param("set") Set<Integer> ids); -->
<select id="findUserBySet" resultType="com.shuai.domain.User" >
select * from user
<where>
<if test="_parameter != null and _parameter.size() > 0">
<foreach collection="set" open="and id in (" close=")" item="id" separator=",">
#{id}
</foreach>
</if>
</where>
</select>
1.7 Map集合
1.7.1 方法示例
List<User> findUserByMap(Map<Integer,Object> maps);
1.7.2 映射配置
<!-- List<User> findUserByMap(Map<Integer,Object> maps); -->
<select id="findUserByMap" resultType="com.shuai.domain.User" >
select * from user
<where>
<if test="_parameter != null and _parameter.size() > 0">
<foreach collection="_parameter.keySet()" open="and id in (" close=")" item="id" separator=",">
#{id}
</foreach>
</if>
</where>
</select>
2. 多参数方法
多参数方法每个参数的处理方案个上述单参数方法一样,多参数方法的核心点在于如何获取每个参数.
获取每个参数的方法有很多, 大致可以有三种方案:
使用#{arg*}获取
使用#{param*}获取
使用@Param注解固定参数名
2.1 使用#{arg*}获取
2.1.1 方法示例
//根据"姓名和年龄"查找用户
public User selectUser(String name,int age);
2.1.2 映射配置
<!-- public User selectUser(String name,int age); -->
<select id="selectUser" resultType="com.shuai.domain.User" >
select * from user where name = #{arg0} and age=#{arg1}
</select>
<!-- #{arg0}代表接收方法的第一个参数,#{arg1}代表接收方法的第二参数,以此类推 -->
2.2 使用#{param*}获取
2.2.1 方法示例
//根据"姓名和年龄"查找用户
public User selectUser(String name,int age);
2.2.2 映射配置
<!-- public User selectUser(String name,int age); -->
<select id="selectUser" resultType="com.shuai.domain.User">
select * from user where name = #{param1} and age=#{param2}
</select>
<!-- #{param1}代表接收方法的第一个参数,#{param2}代表接收方法的第二参数,以此类推 -->
2.3 使用@Param注解固定参数名
2.3.1 方法示例
//根据"姓名和年龄"查找用户
public User selectUser(@Param("username") String name,@Param("userage") int age);
2.3.2 映射配置
<select id="selectUser" resultType="com.shuai.domain.User">
select * from user where name = #{username} and age=#{userage}
</select>
<!-- 此时,#{}中内容,以@Param()注解中的参数名为准 -->
文章知识点与官方知识档案匹配,可进一步学习相关知识
————————————————
原文链接:https://blog.csdn.net/qq_45228323/article/details/117024332