标签:primary 形式 不能 object for 两种 ring inf 配置文件
无论传递的参数是什么样的,最后mybtis都会将传入的转换为map
@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>
方案一
:多个参数传递不能使用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>
传递简单参数常用写法如下
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>
<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>
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>
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>
标签:primary 形式 不能 object for 两种 ring inf 配置文件
原文地址:https://www.cnblogs.com/kasi/p/10293944.html