一、简单类型
8种简单类型+String
mapper.xml
<select id="queryStudentCount" resultType="int">
select count(*) from student
</select>
Java接口中
int queryStudentCount();
Test.java测试类
//查询学生总数
public static void queryStudentCount() throws IOException {
Reader reader = Resources.getResourceAsReader("conf.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sessionFactory.openSession();
StudentMapper studentMapper = session.getMapper(StudentMapper.class);
System.out.println(studentMapper.queryStudentCount());
session.close();
}
二、对象类型
a.返回值为单个对象时,输出参数为实体对象类型
b.返回值为对象集合时,在mapper.xml中的resultType是返回值集合的对象类型
在java类中,返回值为List<对象类型>
mapper.xml
<select id="queryStuByStuno" parameterType="int" resultType="student">
select * from student where stuno = #{stuno}
</select>
<select id="queryAllStudents" resultType="student">
select * from student
</select>
Java接口
Student queryStuByStuno(int stuNo);//查询单个学生
List<Student> queryAllStudents();//查询多个学生的集合
Test.java测试类
//查询单个学生
public static void queryStuByStuno() throws IOException {
Reader reader = Resources.getResourceAsReader("conf.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sessionFactory.openSession();
StudentMapper studentMapper = session.getMapper(StudentMapper.class);
System.out.println(studentMapper.queryStuByStuno(1).toString());
session.close();
}
//查询全部学生
public static void queryAllStudents() throws IOException {
Reader reader = Resources.getResourceAsReader("conf.xml");
SqlSessionFactory sessionFactoty = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sessionFactoty.openSession();
StudentMapper studentMapper = session.getMapper(StudentMapper.class);
List<Student> students = studentMapper.queryAllStudents();
System.out.println(students.toString());
session.close();
}
三、HashMap
HashMap本身是一个集合,可以存放多个数据。
但是根据提示发现,返回值为HashMap是,查询结果只能是一个学生,此时是一个HashMap对象对应一个学生(no ,name)
结论----》一个HashMap对应一个学生的多个元素(多个属性),对此可以用List<HashMap>存储
HashMapA— no-----1
name—zs
age–22
HashMapB— no-----2
name—ww
age–23
mapper.xml
<select id="queryStudentOutByHashMap" resultType="HashMap">
<!-- sql语句中字段的别名对应HashMap的key -->
select stuno "no", stuname "name" from student
</select>
Java接口
List<HashMap<String, Object>> queryStudentOutByHashMap();
Test.java测试类
//查询全部学生返回值用HashMap存储
public static void queryStudentOutByHashMap() throws IOException {
Reader reader = Resources.getResourceAsReader("conf.xml");
SqlSessionFactory sessionFactoty = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sessionFactoty.openSession();
StudentMapper studentMapper = session.getMapper(StudentMapper.class);
List<HashMap<String, Object>> studentsMap = studentMapper.queryStudentOutByHashMap();
System.out.println(studentsMap.toString());
session.close();
}
四、resultMap
resultMap:实体类的属性、数据表的字段:类型、名字不同时(stuno–id)
注意:当属性名与字段名不一致时,除了使用resultMap以外,还可以使用resultType+HashMap
- resultMap
<select id="queryStudentById" parameterType="int" resultMap="queryStudent">
select id , name from student where id = #{id}
</select>
<resultMap type="student" id="queryStudent">
<!-- 指定类中的属性和表中的字段对应关系 -->
<id property="stuNo" column="id" />
<result property="stuName" column="name"/>
</resultMap>
- resultType+HashMap
<select id="queryStudentByIdWithHashMap" parameterType="int" resultType="student">
<!-- 引号内的别名对应类中的属性名 -->
select id "stuNo" , name "stuName" from student where id = #{id}
</select>
注意:如果一个字段的结果始终为默认值(0、0.0、null),则可能是表的字段和类的属性名写错