實體對象如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
/** 使用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映射文件如下:
1
2
3
|
< select id = "findVendorAll" resultType = "vendor" > select * from Vendors </ select > |
接口文件方法如下:
1
2
|
//查詢所有記錄 List<Vendor> findVendorAll(); |
測試文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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則會將查詢到的多條記錄設置進集合中并返回。
到此這篇關于Mybatis查詢多條記錄并返回List集合的方法的文章就介紹到這了,更多相關Mybatis查詢多條記錄返回List內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/u014268482/article/details/80570508