当前位置:网站首页>Record: call mapper to report null pointer Foreach > the usage of not removing repetition;
Record: call mapper to report null pointer Foreach > the usage of not removing repetition;
2022-04-23 20:06:00 【qq_ forty-three million four hundred and thirty-two thousand an】
I have two problems today , Make a note of :
1. Call in implementation class Mapper Interface query sql, The null pointer is abnormal :
as a result of Mapper Forgot to annotate the interface @Autowired
@Autowired
private UserMapper userMapper;
2. Make a note of <foreach> Usage of :
A string of id Of list( There may be duplicate values ), Return an object list, Find out as many as you want , You can't get rid of it ;
@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;
}
<!-- Return to three -->
<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>
<!-- Return two -->
<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>
Incoming list Contains three id, Three pieces of data need to be returned ,separator The delimiter defined is union all, The box union all Connect each query .
PS: The following is the test process
<select id="test" parameterType="java.util.List" resultType="User">
<foreach collection="list" item="id">
select * from user where id =#{id}
</foreach>
</select>
Writing this directly will report an error , So here's how to write it :
<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>
It can work normally , final select * from user where 1=2 Never find the data , But you can guarantee that no error will be reported .(union all The fields before and after the query should be consistent ,union Will also go heavy )
So there is the writing method after the above optimization .
版权声明
本文为[qq_ forty-three million four hundred and thirty-two thousand an]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231946348773.html
边栏推荐
- @MapperScan与@Mapper
- 指针数组与数组指针的区分
- Fundamentals of programming language (2)
- C语言的十六进制printf为何输出有时候输出带0xFF有时没有
- [text classification cases] (4) RNN and LSTM film evaluation Tendency Classification, with tensorflow complete code attached
- Mysql database - basic operation of database and table (II)
- C6748 软件仿真和硬件测试 ---附详细FFT硬件测量时间
- Electron入门教程4 —— 切换应用的主题
- Why is the hexadecimal printf output of C language sometimes with 0xff and sometimes not
- 【数值预测案例】(3) LSTM 时间序列电量预测,附Tensorflow完整代码
猜你喜欢
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
【文本分类案例】(4) RNN、LSTM 电影评价倾向分类,附TensorFlow完整代码
aqs的学习
Understanding various team patterns in scrum patterns
Project training of Software College of Shandong University - Innovation Training - network security shooting range experimental platform (6)
Distinction between pointer array and array pointer
Building googlenet neural network based on pytorch for flower recognition
STM32基础知识
[numerical prediction case] (3) LSTM time series electricity quantity prediction, with tensorflow complete code attached
Class loading mechanism
随机推荐
MySQL advanced lock - overview of MySQL locks and classification of MySQL locks: global lock (data backup), table level lock (table shared read lock, table exclusive write lock, metadata lock and inte
【h264】libvlc 老版本的 hevc h264 解析,帧率设定
[webrtc] add x264 encoder for CEF / Chromium
LeetCode异或运算
IIS data conversion problem: 16bit to 24bit
Grafana 分享带可变参数的链接
Lpc1768 optimization comparison of delay time and different levels
Mysql database - single table query (I)
使用 WPAD/PAC 和 JScript在win11中进行远程代码执行
Virtual machine performance monitoring and fault handling tools
R语言使用timeROC包计算无竞争风险情况下的生存资料多时间AUC值、使用confint函数计算无竞争风险情况下的生存资料多时间AUC指标的置信区间值
还在用 ListView?使用 AnimatedList 让列表元素动起来
视频理解-Video Understanding
PHP reference manual string (7.2000 words)
filebeat、logstash配置安装
Kubernetes入门到精通-KtConnect(全称Kubernetes Toolkit Connect)是一款基于Kubernetes环境用于提高本地测试联调效率的小工具。
本地调用feign接口报404
Devops integration - environment variables and building tools of Jenkins service
NC basic usage 4
Software College of Shandong University Project Training - Innovation Training - network security shooting range experimental platform (8)