MyBatisselect返回list数据

Posted 司徒二条

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatisselect返回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]

 

以上是关于MyBatisselect返回list数据的主要内容,如果未能解决你的问题,请参考以下文章

mybatis select返回多条记录即返回类型为list时

mybatis selectone 返回为空的问题

滑动到下一个片段并返回后的片段不显示任何内容

mybatisselect语句

未来不返回任何东西

TP5报如下的错误 Indirect modification of overloaded element of thinkpaginatorCollection has no effect(代码片段