1. 概念
- mybatis的传参即通过dao的方法映射mapper.xml配置文件中的方法操作数据库
无论传递的参数是什么样的,最后mybtis都会将传入的转换为map
- mybatis传参可以分为两个部分
- 参数的数量
- 参数的类型
2. 按参数的数量
2.1 单个参数传递
@param
可以不用写,如果写上就要求和mapper文件中的参数一致public List<XXBean> getXXBeanList(@param("id")String id); <select id="getXXXBeanList" parameterType="java.lang.String" resultType="XXBean"> select * from tableName t where t.id= #{id} </select>
2.2 多个参数传递
方案一
:多个参数传递不能使用parameterType
,#{index}是第几个就用第几个的索引,索引从0开public List<XXXBean> getXXXBeanList(String xxId, String xxCode); <select id="getXXXBeanList" resultType="XXBean"> select t.* from tableName where id = #{0} and name = #{1} </select>
方案二
:基于注解public List<XXXBean> getXXXBeanList(@Param("id")String id, @Param("code")String code); <select id="getXXXBeanList" resultType="XXBean"> select t.* from tableName where id = #{id} and name = #{code} </select>
方案三
:map封装参数public List<XXXBean> getXXXBeanList(HashMap map); //#{}中的值为map中的key <select id="getXXXBeanList" parameterType="hashmap" resultType="XXBean"> select 字段... from XXX where id=#{xxId} code = #{xxCode} </select>
方案四
:list封装参数public List<XXXBean> getXXXBeanList(List<String> list); <select id="getXXXBeanList" resultType="XXBean"> select * from XXX where id in <foreach item="item" index="index" collection="list" open="(" separator="," close=")"> #{item} </foreach> </select>
方案五
:传递string和list两种参数//将参数放入map map.put("name",kasi); map.put("list",list); <select id="getSysInfo" parameterType="java.util.Map" resultType="XXBean"> select * from user WHERE name = #{name } and id in <foreach collection="list" item="item" index="index" open="(" close=")" separator=","> #{item} </foreach> </select>
3. 按传递参数的类型
- Mybatis的parameterType属性,用于对应的mapper接口方法接受的参数类型
3.1 简单数据类型
- 传递简单参数常用写法如下
public List<XXBean> getXXBeanList(Integer id); <select id="getXXXBeanList" parameterType="java.lang.Integer " resultType="XXBean"> select * from tableName t where t.id= #{id} </select>
- 当有
if
语句时,需要修改参数名<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select * from tb_user <if test="_parameter != null"> //将原本的"userId"换成"_parameter",此处还有其他方法,参见其他文章 where id = #{userId,jdbcType=INTEGER} </if> </select>
3.2 POJO类型
- 普通POJO
<select id="getXXXBeanList" parameterType="com.voion.shiro.User" resultType="XXBean"> select * from table_t where id= #{id} //直接写user的属性值即可 </select>
- 包装POJO类
public class UserVo { private User user; //封装user类 private String name; //普通属性 } <select id="getXXXBeanList" parameterType="com.voion.shiro.UserVo" resultType="XXBean"> select * from table_t where id= #{user.id} and name=#{name} </select>
3.3 集合
- list和array
- list和array的区别只在于collection为
list
或者array
<select id="selectUserInList" resultType="User"> select * from table where id in <foreach item="item" index="index" collection="list" open="(" separator="," close=")"> #{item} </foreach> </select>
- list和array的区别只在于collection为
- map
- 注意:当map中有List或者array集合时,在遍历时collection的值为list或array在map中的key
//将参数放入map map.put("name",kasi); map.put("nameList",nameList); //list为List<String>集合 <select id="getSysInfo" parameterType="java.util.Map" resultType="XXBean"> select * from user WHERE name = #{name } and id in <foreach collection="nameList" item="item" index="index" open="(" close=")" separator=","> #{item} </foreach> </select>
- 此处还有另一种写法,将dao接口传参用@Param标注时,mapper中的参数都需要写成"标注的值.key"形式
//dao接口 void findUser(@Param("params")Map<String,Object> map) //mapper.xml <select id="getSysInfo" parameterType="java.util.Map" resultType="XXBean"> select * from user WHERE name = #{params.name} and id in <foreach collection="params.nameList" item="item" index="index" open="(" close=")" separator=","> #{item} </foreach> </select>
- 注意:当map中有List或者array集合时,在遍历时collection的值为list或array在map中的key
原文地址:https://www.cnblogs.com/kasi/p/10293944.html
时间: 2024-10-30 19:56:40