Mybatis查询多条记录并返回List集合的方法
作者:走路的猫头鹰 时间:2023-08-08 05:16:48
实体对象如下:
/**
使用lobmok插件
*/
@Getter
@Setter
@NoArgsConstructor
@ToString
@EqualsAndHashCode
public class Vendor {
private String vend_id;
private String vend_name;
private String vend_address;
private String vend_city;
private String vend_state;
private String vend_zip;
private String vend_country;
}
XML映射文件如下:
<select id="findVendorAll" resultType="vendor">
select * from Vendors
</select>
接口文件方法如下:
//查询所有记录
List<Vendor> findVendorAll();
测试文件如下:
try {
String resource = "mybatis-config.xml";
InputStream resourceAsStream = Resources.getResourceAsStream(resource);
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream,"development2");
//获取SQLSession
SqlSession openSession = build.openSession();
VendorMapper mapper = openSession.getMapper(VendorMapper.class);
List<Vendors> findVendorAll = mapper.findVendorAll();
System.out.println(findVendorAll);
} catch (IOException e) {
System.out.println("加载配置文件失败");
e.printStackTrace();
}
笔记:
XML中只需resultType属性值为实体对象别名或全路径名。
mybatis会通过接口文件的返回值类型来判断返回的是集合还是对象。如果是对象,则按常规查询并返回;如果是List集合,mybatis则会将查询到的多条记录设置进集合中并返回。
来源:https://blog.csdn.net/u014268482/article/details/80570508
标签:Mybatis,查询,返回List
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java中的javaBean、vo、entity、domain和pojo
2023-03-30 21:41:20
![](https://img.aspxhome.com/file/2023/5/61605_0s.png)
Java关键字volatile详析
2023-01-21 21:51:11
![](https://img.aspxhome.com/file/2023/5/129185_0s.gif)
Java对象传递与返回的细节问题详析
2023-04-07 16:42:50
![](https://img.aspxhome.com/file/2023/3/121933_0s.png)
Mybatis映射文件实例详解
2023-06-12 13:08:42
C#子线程执行完后通知主线程的方法
2022-02-26 20:15:40
![](https://img.aspxhome.com/file/2023/4/88654_0s.jpg)
java明文密码三重加密方法
2022-09-01 05:59:26
![](https://img.aspxhome.com/file/2023/1/115891_0s.jpg)
Android实现ViewPage轮播图效果
2023-08-12 16:25:43
![](https://img.aspxhome.com/file/2023/1/138961_0s.png)
Android实现代码画虚线边框背景效果
2022-11-03 10:48:42
![](https://img.aspxhome.com/file/2023/8/123298_0s.png)
Android实现图片自动轮播并且支持手势左右无限滑动
2021-11-04 22:56:46
Java之OutputStreamWriter流案例详解
2023-11-11 13:03:55
java新人基础入门之递归调用
2023-11-10 01:15:16
![](https://img.aspxhome.com/file/2023/1/90981_0s.png)
Java调用Shell命令的方法
2022-01-06 20:01:34
spring boot 常见http请求url参数获取方法
2023-01-26 13:59:43
Android编写简单的聊天室应用
2023-12-19 10:49:38
![](https://img.aspxhome.com/file/2023/7/110737_0s.jpg)
Android UI设计与开发之实现应用程序只启动一次引导界面
2022-03-01 19:47:29
![](https://img.aspxhome.com/file/2023/2/91652_0s.jpg)
Service Activity的三种交互方式(详解)
2022-03-16 20:55:08
C#装箱和拆箱的原理介绍
2022-06-18 02:33:17
![](https://img.aspxhome.com/file/2023/1/88311_0s.jpg)
SpringCloud服务注册和发现组件Eureka
2023-10-26 06:07:51
![](https://img.aspxhome.com/file/2023/2/79952_0s.png)
C#6 null 条件运算符
2022-03-04 19:42:14
java 获取字节码文件的几种方法总结
2023-11-29 15:17:57