Mybaits查询返回值是List类型的
查询返回值是list类型的
1 首先在接口中写方法
public interface EmployeeMapper {
public List<Employee> getEmpsByLastNameLike(String lastName);
}
2 在映射文件xml配置方法
<!-- public List<Employee> getEmpsByLastNameLike(String lastName); -->
<!--resultType:如果返回的是一个集合,要写集合中元素的类型 -->
<select id="getEmpsByLastNameLike" resultType="com.atguigu.mybatis.bean.Employee">//注意这里返回值类型为bean类型,而不是list
select * from tbl_employee where last_name like #{lastName}
</select>
3在junit方法里进行测试
@Test
public void test04() throws IOException{
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
//1、获取到的SqlSession不会自动提交数据
SqlSession openSession = sqlSessionFactory.openSession();
try{
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
List<Employee> like = mapper.getEmpsByLastNameLike("%e%");
for (Employee employee : like) {
System.out.println(employee);
}
}finally{
openSession.close();
}
}