记录:调用mapper报空指针;<foreach>不去重的用法;

今天遇到了两个问题,记录一下:
1.在实现类中调用Mapper接口查询sql,报空指针异常:
原因是Mapper接口上忘了加注解@Autowired

@Autowired
private UserMapper userMapper;

2.记录一下<foreach>的用法:
传入一串id的list(有可能有重复值),返回一个对象list,要求传入多少条就要查出多少条,不能去重;

@Override
 public List<User> test(){
     List<String> ids = new ArrayList<>();
     ids.add("1");
     ids.add("1");
     ids.add("2");
     List<User> users = userMapper.test(ids);
     return users;
 }
<!--返回三条-->
<select id="test" parameterType="java.util.List" resultType="User">
	<foreach collection="list" item="id" separator="union all">
		select * from user where id =#{id}
	</foreach>
</select>

<!--返回两条-->
<select id="test" parameterType="java.util.List" resultType="User">
	select * from user where id in
	<foreach collection="list" item="id" separator="," open="(" close=")">
		#{id}
	</foreach>
</select>

传入的list包含三个id,需要返回三条数据,separator定义的分隔符是union all,即用union all连接每个查询。

PS:以下是测试过程

<select id="test" parameterType="java.util.List" resultType="User">
	<foreach collection="list" item="id">
		select * from user where id =#{id}
	</foreach>
</select>

直接这样写会报错,于是有了下面的写法:

<select id="test" parameterType="java.util.List" resultType="User">
	<foreach collection="list" item="id" separator="union all">
		select * from user where id =#{id} union all
	</foreach>
	select * from user where 1=2
</select>

这样写是可以正常运行的,最后的select * from user where 1=2永远不会查到数据,但是可以保证不报错。(union all前后查询字段要一致,union也会去重)
于是有了上边优化之后的写法。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在使用<foreach>标签传递List<String>参数时,可以使用以下两种方式。 第一种方式是在Mapper接口的方法参数前面添加@Param注解,并将List<String>参数命名为list。例如: 引用\[1\]的示例代码: ```java public List<Map<String, Object>> dynamicForeachTest(@Param("list") List<String> ids); ``` 引用\[1\]的示例代码: ```xml <select id="dynamicForeachTest" resultType="java.util.Map"> select * from t_blog where id in <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select> ``` 第二种方式是直接将List<String>参数命名为ids,不需要添加@Param注解。例如: 引用\[2\]的示例代码: ```java public List<Map<String, Object>> dynamicForeach3Test(List<String> ids, @Param("contractId") String contractId); ``` 引用\[2\]的示例代码: ```xml <select id="dynamicForeach3Test" resultType="java.util.Map"> select * from xxx where cid=#{contractId} and fID in <foreach collection="ids" item="item" open="(" separator="," close=")"> #{item} </foreach> </select> ``` 无论使用哪种方式,都可以通过<foreach>标签遍历List<String>参数的每个元素,并将其作为SQL语句的参数进行查询。 #### 引用[.reference_title] - *1* *2* *3* [MyBatisin <foreach>的使用](https://blog.csdn.net/xujunkai66/article/details/81413818)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值