当前位置:网站首页>记录:调用mapper报空指针;<foreach>不去重的用法;
记录:调用mapper报空指针;<foreach>不去重的用法;
2022-04-23 19:46:00 【qq_43432057】
今天遇到了两个问题,记录一下:
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也会去重)
于是有了上边优化之后的写法。
版权声明
本文为[qq_43432057]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_43432057/article/details/124308161
边栏推荐
- JVM的类加载过程
- MySQL syntax collation (2)
- [报告] Microsoft :Application of deep learning methods in speech enhancement
- Scrum Patterns之理解各种团队模式
- Comment créer un pass BEP - 20 sur la chaîne BNB
- Mysql database - connection query
- kibana 报错 server is not ready yet 可能的原因
- The most detailed network counting experiment in history (2) -- rip experiment of layer 3 switch
- Common processing of point cloud dataset
- How to select the third-party package of golang
猜你喜欢
Shanda Wangan shooting range experimental platform project - personal record (IV)
Kubernetes入门到精通-裸机LoadBalence 80 443 端口暴露注意事项
Openharmony open source developer growth plan, looking for new open source forces that change the world!
Unity创建超写实三维场景的一般步骤
Esp8266 - beginner level Chapter 1
命令-sudo
5 minutes to achieve wechat cloud applet payment function (including source code)
Physical meaning of FFT: 1024 point FFT is 1024 real numbers. The actual input to FFT is 1024 complex numbers (imaginary part is 0), and the output is also 1024 complex numbers. The effective data is
Compact CUDA tutorial - CUDA driver API
Understanding various team patterns in scrum patterns
随机推荐
Inject Autowired fields into ordinary beans
The difference between underline and dot of golang import package
命令-sudo
基于pytorch搭建GoogleNet神经网络用于花类识别
The textarea cursor cannot be controlled by the keyboard due to antd dropdown + modal + textarea
Core concepts of rest
MySQL数据库 - 数据库和表的基本操作(二)
Kubernetes getting started to proficient - install openelb on kubernetes
Unity创建超写实三维场景的一般步骤
【文本分类案例】(4) RNN、LSTM 电影评价倾向分类,附TensorFlow完整代码
2021-2022-2 ACM training team weekly Programming Competition (8) problem solution
MFCC: Mel频率倒谱系数计算感知频率和实际频率转换
视频理解-Video Understanding
Leetcode XOR operation
MySQL 进阶 锁 -- MySQL锁概述、MySQL锁的分类:全局锁(数据备份)、表级锁(表共享读锁、表独占写锁、元数据锁、意向锁)、行级锁(行锁、间隙锁、临键锁)
MySQL syntax collation (3)
LPC1768 关于延时Delay时间与不同等级的优化对比
Possible root causes include a too low setting for -Xss and illegal cyclic inheritance dependencies
Video understanding
@Mapperscan and @ mapper