mapper接口的使用

转自: https://blog.csdn.net/qq_36688928/article/details/82783392

一、mapper接口

/**
 * 根据剧典id list查询剧典
 */
public List<Drama> selectByIds(@Param("IIDD")List<Long> dramaIds);


二、mapper.xml文件

<!-- 根据剧典id list查询剧典 -->
<select id="selectByIds" resultMap="DramaImageResultMap">
    select * from drama where drama_id in 
    <foreach collection="IIDD" item="ITCC" open="(" close=")" separator=",">
    #{ITCC}
   </foreach>
</select>

推荐的MyBatis传参方式List、数组等

转自:http://blog.51cto.com/13523664/2060417

MyBatis 推荐的传参方式

如果要详细的学习 MyBatis,推荐看这个教程: https://www.w3cschool.cn/mybatis/)

1. 单个参数

//接口方法
int getAgeById(Integer id);
//xml映射文件
<select id="getAgeById" resultType="Integer">
    select age from user where user_id = #{id}
</select>

2. 多个参数

//接口方法
 User login(@Param("username") String username, @Param("password") String password);
 //xml映射文件
 <select id="login" resultMap="BaseResultMap">
    select
    *
    from user
    where username = #{username} and password = #{password}
</select>

3. 数组参数

//接口方法
ArrayList<User> selectByIds(Integer [] ids);
//xml映射文件
<select id="selectByIds" resultMap="BaseResultMap">
    select
    *
    from user where id in
    <foreach item="item" index="index" collection="array" open="(" separator="," close=")">
        #{item}
    </foreach>
</select>

4.List参数

//接口方法
ArrayList<User> selectByIds(List<Integer> ids);
//xml映射文件
<select id="selectByIds" resultMap="BaseResultMap">
    Select
    <include refid="Base_Column_List" />
    from jria where ID in
    <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
          #{item}
      </foreach>
  </select> 

重点:

当传入多个参数,collection需要与@Param的名称一致,否则报错.

XXX<XXX>   selectByIds(
@Param("id")List<Integer> [] id,
@Param("array") Integer[] array,
Product body);
XML:
<select id ="selectByIds" resultMap="com.heiketu.test.pojo.Product">
	select * from Products where 
	id in
	<foreach collection ="id" item="item1" index= "index" open="(" separator="," close=")">
	#{item1}
	</foreach>
	and xx in
	<foreach collection ="array" item="item2" index= "index" open="(" separator="," close=")">
	#{item2}
	</foreach>
</select>

Mybatis通过出入Map参数作为条件进行查询

转自: https://blog.csdn.net/u014268482/article/details/80622216

映射文件中查询语句部分:

<!--通过map进行条件查询-->
<select id="selectByMap" resultType="com.heiketu.testpackage.pojo.Product">
    select * from Products where prod_price = #{prodPrice} and prod_desc = #{prodDesc}
</select>

接口文件中对应的查询方法:

//输入参数为Map的条件查询

Product selectByMap(Map<String,Object> map);

测试代码:

前面有创建sqlSessionFactory对象和SQLSession对象的代码

Map<String,Object> map = new HashMap<>();
map.put("prodPrice","11.99");
map.put("prodDesc","18 inch teddy bear, comes with cap and jacket");

ProductsMapper mapper = sqlSession.getMapper(ProductsMapper.class);
Product product = mapper.selectByMap(map);
System.out.println(product);

map中的key值与映射文件中的select语句#{}占位符中的值需要一一对应

在mybatis中,任何传入的参数都会被对应封装成Map集合,然后会以map的key=value形式取值。
对传入的参数是List集合,mybatis会对list集合进行特殊处理,其取值方式通过list[下标]或collection[下标]的方式入参取值。
对于数组,Mybatis同样会做特殊处理,它会对数组采用array[下标]的方式入参取值。

<!--如果是List集合(或set集合)就如下入参取值-->
<select id="selectByMap" resultType="com.heiketu.testpackage.pojo.Product">
    select * from Products where prod_price = #{list[0]} and prod_desc = #{collection[1]}
</select>
<!--如果是数组就如下入参取值-->
<select id="selectByMap" resultType="com.heiketu.testpackage.pojo.Product">
    select * from Products where prod_price = #{array[0]} and prod_desc = #{array[1]}
</select>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值