mybatis——返回结果类型为集合类型(List、Map)
程序员文章站
2022-06-30 20:56:22
...
本章节主要讲解如果Mapper接口的返回类型为集合(List、Map)时,如何定义
一、返回结果类型为List
Mapper接口的方法:
List<Employee> getAll();
EmployeeMapper.xml映射文件:
<select id="getAll" resultType="com.wcg.mybatis.entity.Employee">
select id,last_name ,email,gender from tbl_employee
</select>
在resultType定义返回结果List中的元素类型即可:
List<Employee> --> resultType="com.wcg.mybatis.entity.Employee"
测试方法:
@Test
public void selectAllTest(){
// 获取 SqlSession 的实例 。SqlSession 完全包含了面向数据库执行 SQL 命令所需的所有方法。通过 SqlSession 实例来直接执行已映射的 SQL 语句
SqlSession sqlSession = null;
try {
sqlSession = sessionFactory.openSession();
// 通过获取接口代理对象来执行sql语句
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
List<Employee> employeeList = mapper.getAll();
for (Employee employee : employeeList) {
System.out.println("name: "+ employee.getLastName());
}
} finally {
// 资源关闭,放在finally中确保一定会执行
sqlSession.close();
}
}
结果:
DEBUG 05-07 23:46:31,854 ==> Preparing: select id,last_name ,email,gender from tbl_employee (BaseJdbcLogger.java:145)
DEBUG 05-07 23:46:31,905 ==> Parameters: (BaseJdbcLogger.java:145)
DEBUG 05-07 23:46:31,927 <== Total: 3 (BaseJdbcLogger.java:145)
name: BB
name: CC
name: DD
二、返回结果类型为Map
返回一行数据:
Mapper接口的方法:
Map<String, Object> getEmpById(@Param("id") Integer id);
EmployeeMapper.xml映射文件:
<select id="getEmpById" resultType="map">
select id,last_name ,email,gender from tbl_employee where id = #{id}
</select>
则Map<String, Object>中,String对应着字段名,Object对应着字段值
测试方法:
@Test
public void selectMapTest(){
// 获取 SqlSession 的实例 。SqlSession 完全包含了面向数据库执行 SQL 命令所需的所有方法。通过 SqlSession 实例来直接执行已映射的 SQL 语句
SqlSession sqlSession = null;
try {
sqlSession = sessionFactory.openSession();
// 通过获取接口代理对象来执行sql语句
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
Map<String, Object> empMap = mapper.getEmpById(2);
Set<Map.Entry<String, Object>> entries = empMap.entrySet();
Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
while (iterator.hasNext()){
Map.Entry<String, Object> next = iterator.next();
System.out.println(next.getKey()+" : "+next.getValue());
}
} finally {
// 资源关闭,放在finally中确保一定会执行
sqlSession.close();
}
}
结果:
DEBUG 05-07 23:45:03,837 ==> Preparing: select id,last_name ,email,gender from tbl_employee where id = ? (BaseJdbcLogger.java:145)
DEBUG 05-07 23:45:03,882 ==> Parameters: 2(Integer) (BaseJdbcLogger.java:145)
DEBUG 05-07 23:45:03,937 <== Total: 1 (BaseJdbcLogger.java:145)
gender : 0
last_name : BB
id : 2
email : [email protected]
返回多行数据:
Mapper接口的方法:
@MapKey("id")
Map<Integer, Employee> getAllEmp();
EmployeeMapper.xml映射文件:
<select id="getAllEmp" resultType="employee">
select id,last_name ,email,gender from tbl_employee
</select>
通过@MapKey定义返回Map集合的key是哪个字段的值,Map集合的Value则为每行数据的值
测试方法:
@Test
public void selectMapKeyTest(){
// 获取 SqlSession 的实例 。SqlSession 完全包含了面向数据库执行 SQL 命令所需的所有方法。通过 SqlSession 实例来直接执行已映射的 SQL 语句
SqlSession sqlSession = null;
try {
sqlSession = sessionFactory.openSession();
// 通过获取接口代理对象来执行sql语句
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
Map<Integer, Employee> empMap = mapper.getAllEmp();
Set<Map.Entry<Integer, Employee>> entries = empMap.entrySet();
Iterator<Map.Entry<Integer, Employee>> iterator = entries.iterator();
while (iterator.hasNext()){
Map.Entry<Integer, Employee> next = iterator.next();
System.out.println(next.getKey()+" : "+next.getValue().getLastName());
}
} finally {
// 资源关闭,放在finally中确保一定会执行
sqlSession.close();
}
}
结果:
DEBUG 05-07 23:30:54,624 ==> Preparing: select id,last_name ,email,gender from tbl_employee (BaseJdbcLogger.java:145)
DEBUG 05-07 23:30:54,712 ==> Parameters: (BaseJdbcLogger.java:145)
DEBUG 05-07 23:30:54,738 <== Total: 3 (BaseJdbcLogger.java:145)
2 : BB
4 : CC
5 : DD
上一篇: C++自我复习基础知识