Mybatis查詢多條記錄并返回List集合的方法
實體對象如下:
/**使用lobmok插件*/@Getter@Setter@NoArgsConstructor@ToString@EqualsAndHashCodepublic 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 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則會將查詢到的多條記錄設置進集合中并返回。到此這篇關于Mybatis查詢多條記錄并返回List集合的方法的文章就介紹到這了,更多相關Mybatis查詢多條記錄返回List內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: