当前位置:网站首页>记录:调用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
边栏推荐
- DevOps集成-Jenkins 服务的环境变量和构建工具 Tools
- Unity创建超写实三维场景的一般步骤
- Machine learning catalog
- Possible root causes include a too low setting for -Xss and illegal cyclic inheritance dependencies
- Design of library management database system
- 数据库查询 - 选课系统
- 对普通bean进行Autowired字段注入
- Electron入门教程4 —— 切换应用的主题
- What is a message queue
- 【webrtc】Add x264 encoder for CEF/Chromium
猜你喜欢

Class loading process of JVM

Lottery applet, mother no longer have to worry about who does the dishes (assign tasks), so easy

Kubernetes introduction to mastery - ktconnect (full name: kubernetes toolkit connect) is a small tool based on kubernetes environment to improve the efficiency of local test joint debugging.

@Mapperscan and @ mapper
![[webrtc] add x264 encoder for CEF / Chromium](/img/3f/9bf73d6d2aec14ba94dfc6734eb6ac.png)
[webrtc] add x264 encoder for CEF / Chromium

Possible root causes include a too low setting for -Xss and illegal cyclic inheritance dependencies

Is meituan, a profit-making company with zero foundation, hungry? Coupon CPS applet (with source code)
![[报告] Microsoft :Application of deep learning methods in speech enhancement](/img/c1/7bffbcecababf8dabf86bd34ab1809.png)
[报告] Microsoft :Application of deep learning methods in speech enhancement

MySQL 进阶 锁 -- MySQL锁概述、MySQL锁的分类:全局锁(数据备份)、表级锁(表共享读锁、表独占写锁、元数据锁、意向锁)、行级锁(行锁、间隙锁、临键锁)
![[transfer] summary of new features of js-es6 (one picture)](/img/45/76dba32e4fa7ed44a42e5f98ea8207.jpg)
[transfer] summary of new features of js-es6 (one picture)
随机推荐
Design of warehouse management database system
Redis core technology and practice 1 - start with building a simple key value database simplekv
Possible root causes include a too low setting for -Xss and illegal cyclic inheritance dependencies
基于pytorch搭建GoogleNet神经网络用于花类识别
命令-sudo
LPC1768 关于延时Delay时间与不同等级的优化对比
IIS数据转换问题16bit转24bit
Efficient serial port cyclic buffer receiving processing idea and code 2
MySQL syntax collation (3)
Prefer composition to inheritance
Data analysis learning directory
对普通bean进行Autowired字段注入
MySQL数据库 - 单表查询(一)
Go recursively loops through folders
Strange passion
Core concepts of rest
Application of DCT transform
Golang timer
Machine learning catalog
MySQL数据库 - 连接查询