单参数方法的参数详解, 主要介绍不同类型的参数,在Mapper映射文件中的配置方式.
- 参数为: 简单类型(基本数据类型+String)
- 参数为: 对象类型
- 参数为: List集合类型-List
- 参数为: List集合类型-List
- 参数为: 数组类型
- 参数为: Set集合类型
- 参数为: Map集合类型
public User findUserById(int id);
<select id="findUserById" resultType="com.itheima.domain.User">
select * from user where id = #{id}
select>
public List<User> findUserByUsernameAndPassword(User user);
<select id="findUserByUsernameAndPassword" resultType="com.itheima.domain.User">
select * from user where username=#{username} and password=#{password}
select>
public List<User> findUserByList(List<Integer> ids);
<select id="findUserByList" resultType="com.itheima.domain.User">
select * from user
<where>
<if test="_parameter != null and _parameter.size() > 0">
<foreach collection="list" open="and id in (" close=")" item="uid" separator=",">
#{uid}
foreach>
if>
where>
select>
List<User> findByListUser(List<User> users);
<select id="findByListUser" resultType="com.itheima.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.userId}
foreach>
if>
where>
select>
List<User> findUserByArray(Integer[] ids);
<select id="findUserByArray" resultType="com.itheima.domain.User" >
select * from user
<where>
<if test="_parameter != null">
<foreach collection="array" open="and id in (" close=")" item="uid" separator=",">
#{uid}
foreach>
if>
where>
select>
List<User> findUserBySet(@Param("set") Set<Integer> ids)
<select id="findUserBySet" resultType="com.itheima.domain.User" >
select * from user
<where>
<if test="_parameter != null and _parameter.size() > 0">
<foreach collection="set" open="and id in (" close=")" item="uid" separator=",">
#{uid}
foreach>
if>
where>
select>
List<User> findUserByMap(Map<Integer,Object> maps);
<select id="findUserByMap" resultType="com.itheima.domain.User" >
select * from user
<where>
<if test="_parameter != null and _parameter.size() > 0">
<foreach collection="_parameter.keySet()" open="and id in (" close=")" item="uid" separator=",">
#{uid}
foreach>
if>
where>
select>
多参数方法每个参数的处理方案个上述单参数方法一样,多参数方法的核心点在于如何获取每个参数.
获取每个参数的方法有很多, 大致可以有三种方案:
- 使用#{arg*}获取
- 使用#{param*}获取
- 使用@Param注解固定参数名
//根据"姓名和年龄"查找用户
public User selectUser(String name,int age);
<select id="selectUser" resultType="com.itheima.domain.User">
select * from user where name = #{arg0} and age=#{arg1}
select>
//根据"姓名和年龄"查找用户
public User selectUser(String name,int age);
<select id="selectUser" resultType="com.itheima.domain.User">
select * from user where name = #{arg0} and age=#{arg1}
select>
//根据"姓名和年龄"查找用户
public User selectUser(@Param("username") String name,@Param("userage") int age);
<select id="selectUser" resultType="com.itheima.domain.User">
select * from user where name = #{username} and age=#{userage}
select>
多参数方法每个参数的处理方案个上述单参数方法一样,多参数方法的核心点在于如何获取每个参数.
获取每个参数的方法有很多, 大致可以有三种方案:
- 使用#{arg*}获取
- 使用#{param*}获取
- 使用@Param注解固定参数名
//根据"姓名和年龄"查找用户
public User selectUser(String name,int age);
<select id="selectUser" resultType="com.itheima.domain.User">
select * from user where name = #{arg0} and age=#{arg1}
select>