当前位置:网站首页>记录:调用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
边栏推荐
- 山大网安靶场实验平台项目-个人记录(五)
- Audio editing generation software
- C学习完结
- Shanda Wangan shooting range experimental platform project - personal record (V)
- Hot reload debugging
- First experience of using fluent canvas
- MySQL syntax collation (5) -- functions, stored procedures and triggers
- MFC obtains local IP (used more in network communication)
- IIS数据转换问题16bit转24bit
- 【文本分类案例】(4) RNN、LSTM 电影评价倾向分类,附TensorFlow完整代码
猜你喜欢
Possible root causes include a too low setting for -Xss and illegal cyclic inheritance dependencies
Deep learning -- Summary of Feature Engineering
Shanda Wangan shooting range experimental platform project - personal record (V)
Prefer composition to inheritance
PHP reference manual string (7.2000 words)
FFT物理意义: 1024点FFT就是1024个实数,实际进入fft的输入是1024个复数(虚部为0),输出也是1024个复数,有效的数据是前512个复数
【webrtc】Add x264 encoder for CEF/Chromium
山东大学软件学院项目实训-创新实训-网络安全靶场实验平台(五)
深度学习——特征工程小总结
Esp8266 - beginner level Chapter 1
随机推荐
MySQL lock
C6748 software simulation and hardware test - with detailed FFT hardware measurement time
Comment créer un pass BEP - 20 sur la chaîne BNB
什么是消息队列
HTTP cache - HTTP authoritative guide Chapter VII
MySQL数据库 - 单表查询(一)
Lpc1768 optimization comparison of delay time and different levels
C6748 软件仿真和硬件测试 ---附详细FFT硬件测量时间
Project training of Software College of Shandong University - Innovation Training - network security shooting range experimental platform (V)
Class loading process of JVM
OpenHarmony开源开发者成长计划,寻找改变世界的开源新生力!
Golang timer
MySQL 进阶 锁 -- MySQL锁概述、MySQL锁的分类:全局锁(数据备份)、表级锁(表共享读锁、表独占写锁、元数据锁、意向锁)、行级锁(行锁、间隙锁、临键锁)
Mysql database - single table query (II)
MFC获取本机IP(网络通讯时用得多)
JVM的类加载过程
How to use go code to compile Pb generated by proto file with protoc Compiler Go file
精简CUDA教程——CUDA Driver API
ESP8266-入门第一篇
Main differences between go and PHP