MyBatis(五)select返回list数据
(1)接口中编写方法
public List<Emp> getEmps(String lastName);
(2)编写Mapper文件
<select id="getEmps" resultType="com.eu.bean.Emp"> select id,last_name lastName,gender geder,email from Emp where last_name like #{lastName } </select>
(3)编写测试
public SqlSessionFactory getSqlSessionFactory() throws IOException { String resource = "conf/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); return new SqlSessionFactoryBuilder().build(inputStream); } @Test public void testMapperM() throws IOException { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); //1.获取到sqlsession 不会自动提交数据 SqlSession openSession = sqlSessionFactory.openSession(); EmpDao mapper = openSession.getMapper(EmpDao.class); List<Emp> emps = mapper.getEmps("%e%"); for (Emp emp : emps) { System.out.println(emp); } //手动提交数据 openSession.commit(); openSession.close(); }
(4)测试结果
DEBUG 04-15 22:43:38,139 ==> Preparing: select id,last_name lastName,gender geder,email from Emp where last_name like ? (BaseJdbcLogger.java:145) DEBUG 04-15 22:43:38,187 ==> Parameters: %e%(String) (BaseJdbcLogger.java:145) DEBUG 04-15 22:43:38,226 <== Total: 3 (BaseJdbcLogger.java:145) Emp [id=5, lastName=ae, geder=男, email=dd] Emp [id=6, lastName=je, geder=男, email=dd] Emp [id=7, lastName=jed, geder=男, email=dd]