1_sqlSession查询的三种方式
![](https://img2023.cnblogs.com/blog/2531222/202303/2531222-20230312081940570-542252715.gif)
![](https://img2023.cnblogs.com/blog/2531222/202303/2531222-20230312081940570-542252715.gif)
![](https://img2023.cnblogs.com/blog/2531222/202303/2531222-20230312081940570-542252715.gif)
![](https://img2023.cnblogs.com/blog/2531222/202303/2531222-20230312081940570-542252715.gif)
SqlSession对象本身的API中就有三个查询方法,分别能够实现如下查询方式
1返回单个对象 selectOne
2返回对象List集合 selectList
3返回对象Map集合 selectMap
接下来我们就对这三个方法进行一个快速的学习
创建Emp实体类
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Emp implements Serializable {
private Integer empno;
private String ename;
private String job;
private Integer mgr;
private Date hiredate;
private Double sal;
private Double comm;
private Integer deptno;
}
![](https://img2023.cnblogs.com/blog/2531222/202303/2531222-20230312081940570-542252715.gif)
![点击并拖拽以移动](https://img2023.cnblogs.com/blog/2531222/202303/2531222-20230312081940570-542252715.gif)
准备Mapper映射文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="EmpMapper">
<!--
返回单个对象
public Emp findOne();
id 相当于方法名
resultType 相当于返回值类型
sql语句的查询结果用哪个类来进行封装 如果返回值类型是集合,这里写的也是集合中的元素对应的类,不是集合本身作为类型
paramaterType 参数类型
SQL语句就是具体的方法体的实现
-->
<select id="findOne" resultType="emp" >
select * from emp where empno = 7499
</select>
<!--
返回多个对象List集合
查询全部的员工信息
public List<Emp> findAll()
-->
<select id="findAll" resultType="emp">
select * from emp
</select>
<!--返回多个对象的Map集合
把查询出来的数据中的某一列作为键,整条数据封装的对象作为值
public Map<key,Emp> findEmpMap()
<empno,Emp>
<key,Emp>
-->
<select id="findEmpMap" resultType="map">
select * from emp
</select>
</mapper>
![](https://img2023.cnblogs.com/blog/2531222/202303/2531222-20230312081940570-542252715.gif)
![点击并拖拽以移动](https://img2023.cnblogs.com/blog/2531222/202303/2531222-20230312081940570-542252715.gif)
sqlMapConfig中导入EmpMapper映射文件
<!--加载mapper映射文件-->
<mappers>
<mapper resource="com/msb/mapper/DeptMapper.xml"/>
<mapper resource="com/msb/mapper/EmpMapper.xml"/>
</mappers>
![](https://img2023.cnblogs.com/blog/2531222/202303/2531222-20230312081940570-542252715.gif)
![点击并拖拽以移动](https://img2023.cnblogs.com/blog/2531222/202303/2531222-20230312081940570-542252715.gif)
测试代码
package com.msb.test;
import com.msb.pojo.Dept;
import com.msb.pojo.Emp;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @Author: Ma HaiYang
* @Description: MircoMessage:Mark_7001
*/
public class Test2 {
private SqlSession sqlSession;
@Before
public void init(){
SqlSessionFactoryBuilder ssfb =new SqlSessionFactoryBuilder();
InputStream resourceAsStream = null;
try {
resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
} catch (IOException e) {
e.printStackTrace();
}
SqlSessionFactory factory=ssfb.build(resourceAsStream) ;
sqlSession=factory.openSession();
}
@Test
public void testSelectOne(){
// 查询单个对象
System.out.println("sqlSession查询单个对象");
Emp emp = sqlSession.selectOne("findOne");
System.out.println(emp);
}
@Test
public void testSelectList(){
// 查询多个对象的List集合
System.out.println("sqlSession查询对象List集合");
List<Emp> emps = sqlSession.selectList("EmpMapper.findAll");
emps.forEach(System.out::println);
}
@Test
public void testSelectMap(){
// 查询多个对象的Map集合
System.out.println("sqlSession查询对象Map集合");
Map<Integer, Emp> empMap = sqlSession.selectMap("findEmpMap", "EMPNO");
Set<Integer> empnos = empMap.keySet();
for (Integer empno : empnos) {
System.out.println(empno+" :" +empMap.get(empno));
}
}
@After
public void release(){
// 关闭SQLSession
sqlSession.close();
}
}
![](https://img2023.cnblogs.com/blog/2531222/202303/2531222-20230312081940570-542252715.gif)
![点击并拖拽以移动](https://img2023.cnblogs.com/blog/2531222/202303/2531222-20230312081940570-542252715.gif)