当前位置:网站首页>记录:调用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
边栏推荐
- Executor、ExecutorService、Executors、ThreadPoolExecutor、Future、Runnable、Callable
- IIS data conversion problem: 16bit to 24bit
- IIS数据转换问题16bit转24bit
- Building googlenet neural network based on pytorch for flower recognition
- 音频编辑生成软件
- LPC1768 关于延时Delay时间与不同等级的优化对比
- 2021-2022-2 ACM training team weekly Programming Competition (8) problem solution
- Kubernetes entry to mastery - bare metal loadbalance 80 443 port exposure precautions
- How about Bohai futures. Is it safe to open futures accounts?
- Kubernetes入门到精通-裸机LoadBalence 80 443 端口暴露注意事项
猜你喜欢
Esp8266 - beginner level Chapter 1
Build intelligent garbage classification applet based on Zero
Application of DCT transform
JVM的类加载过程
如何在BNB鏈上創建BEP-20通證
php参考手册String(7.2千字)
【文本分类案例】(4) RNN、LSTM 电影评价倾向分类,附TensorFlow完整代码
MySQL 进阶 锁 -- MySQL锁概述、MySQL锁的分类:全局锁(数据备份)、表级锁(表共享读锁、表独占写锁、元数据锁、意向锁)、行级锁(行锁、间隙锁、临键锁)
Is meituan, a profit-making company with zero foundation, hungry? Coupon CPS applet (with source code)
Garbage collector and memory allocation strategy
随机推荐
goroutine
@MapperScan与@Mapper
指针数组与数组指针的区分
MySQL数据库 - 单表查询(三)
Devops integration - environment variables and building tools of Jenkins service
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
Pit encountered using camera x_ When onpause, the camera is not released, resulting in a black screen when it comes back
Scrum Patterns之理解各种团队模式
The difference between underline and dot of golang import package
Kubernetes entry to mastery - bare metal loadbalance 80 443 port exposure precautions
Distinction between pointer array and array pointer
深度分析数据恢复原理——那些数据可以恢复那些不可以数据恢复软件
程序设计语言基础(2)
数据库查询 - 选课系统
Project training of Software College of Shandong University - Innovation Training - network security shooting range experimental platform (V)
MySQL syntax collation (3)
Kubernetes getting started to proficient - install openelb on kubernetes
Golang timer
JVM的类加载过程
Web Security