码迷,mamicode.com
首页 > 其他好文 > 详细

MyBatis(五)select返回list数据

时间:2019-04-15 23:13:52      阅读:389      评论:0      收藏:0      [点我收藏+]

标签:ast   测试   lis   div   conf   jdb   ike   提交数据   cep   

(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]

 

MyBatis(五)select返回list数据

标签:ast   测试   lis   div   conf   jdb   ike   提交数据   cep   

原文地址:https://www.cnblogs.com/wanerhu/p/10713864.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!