zoukankan      html  css  js  c++  java
  • 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]

     欢迎添加本人微信公众号,一个专注于做技术的有温度的公众号

  • 相关阅读:
    extern C的作用详解
    UIWindow in iOS
    iOS会议和组织
    Fantageek翻译系列之《使用Autolayout显示变化高度的UITableViewCell》
    KVO的概述的使用
    Struts2 基于XML校验(易百教程)
    Maven项目中添加JDBC驱动
    org.dom4j.DocumentException: null Nested exception: null解决方法
    struts2中的数据类型自动转换
    struts2的拦截器
  • 原文地址:https://www.cnblogs.com/wanerhu/p/10713864.html
Copyright © 2011-2022 走看看