11、mybatis返回List
生活随笔
收集整理的這篇文章主要介紹了
11、mybatis返回List
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1、EmployeeMapper接口
- 2、EmployeeMapper.xml
- 3、Test
- 4、測試結果
1、EmployeeMapper接口
package com.mi.dao;import com.mi.pojo.Employee; import org.apache.ibatis.annotations.Param;import java.util.List; import java.util.Map;public interface EmployeeMapper {//返回Listpublic List<Employee> getEmployeeListByName(Map map); }2、EmployeeMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!--指定為接口的全類名--> <mapper namespace="com.mi.dao.EmployeeMapper"><!--返回List,resultType 返回集合中元素的類型--><select id="getEmployeeListByName" resultType="com.mi.pojo.Employee">select * from employee where last_name like #{lastName}</select></mapper>3、Test
@Testpublic void testGetListByName() throws IOException {//1、獲取SqlSessionFactory對象SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();//2、獲取Sqlsesion對象SqlSession sqlSession = sqlSessionFactory.openSession();try {//3、獲取接口的實現類對象//會為接口自動創(chuàng)建一個代理對象,代理對象去執(zhí)行增刪改查方法EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);Map<String,Object> map = new HashMap<String, Object>();map.put("lastName","%e%");List<Employee> list = mapper.getEmployeeListByName(map);for (Employee employee : list){System.out.println(employee);}}finally {sqlSession.close();}}4、測試結果
Employee{id=3, lastName='jerry', gender='0'} Employee{id=4, lastName='kite', gender='1'}Process finished with exit code 0總結
以上是生活随笔為你收集整理的11、mybatis返回List的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 10、mybatis参数处理
- 下一篇: 12、mybatis返回map单条及多条