当前位置:网站首页>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
边栏推荐
- Speex Wiener filter and rewriting of hypergeometric distribution
- Go modules daily use
- MySQL syntax collation (2)
- Design of library management database system
- Use test of FFT and IFFT library functions of TI DSP
- 音频编辑生成软件
- 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
- 视频理解-Video Understanding
- antd dropdown + modal + textarea导致的textarea光标不可被键盘控制问题
- Project training of Software College of Shandong University - Innovation Training - network security shooting range experimental platform (6)
猜你喜欢
Deep learning -- Summary of Feature Engineering
Unity创建超写实三维场景的一般步骤
Decompile and get the source code of any wechat applet - just read this (latest)
山大网安靶场实验平台项目—个人记录(四)
Class loading mechanism
Class loading process of JVM
基于pytorch搭建GoogleNet神经网络用于花类识别
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
Software College of Shandong University Project Training - Innovation Training - network security shooting range experimental platform (8)
如何在BNB链上创建BEP-20通证
随机推荐
Lottery applet, mother no longer have to worry about who does the dishes (assign tasks), so easy
Project training of Software College of Shandong University - Innovation Training - network security shooting range experimental platform (VII)
C6748 software simulation and hardware test - with detailed FFT hardware measurement time
Audio editing generation software
山大网安靶场实验平台项目-个人记录(五)
Mysql database - connection query
如何在BNB鏈上創建BEP-20通證
腾讯邱东洋:深度模型推理加速的术与道
[2022] regard 3D target detection as sequence prediction - point2seq: detecting 3D objects as sequences
[numerical prediction case] (3) LSTM time series electricity quantity prediction, with tensorflow complete code attached
Class loading mechanism
仓库管理数据库系统设计
VeraCrypt文件硬盘加密使用教程
山东大学软件学院项目实训-创新实训-网络安全靶场实验平台(五)
MySQL lock
Mysql database - single table query (III)
Efficient serial port cyclic buffer receiving processing idea and code 2
IIS数据转换问题16bit转24bit
Go modules daily use
【webrtc】Add x264 encoder for CEF/Chromium