04-SQL结果集映射

nobility 发布于 2022-04-10 1074 次阅读


SQL结果集映射

resultType

xml方式

<select id="selectEntity" resultType="com.Student">
  select * from students
</select>
<select id="selectMap" resultType="java.util.LinkedHashMap">	<!--LinkedHashMap可保证查询结果有序-->
  select * from students
</select>

用于测试的关键代码片段,sqlSession为创建的SqlSession对象,其他代码省略

/*普通实体类映射方式*/
List<Student> students = sqlSession.selectList("student.selectEntity");
System.out.println(students);	 //输出结果

/*Map映射方式方式*/
List<LinkedHashMap> studentMap = sqlSession.selectList("student.selectMap");
System.out.println(studentMap);	 //输出结果

注解方式

package com;

import org.apache.ibatis.annotations.Select;

import java.util.LinkedHashMap;
import java.util.List;

public interface StudentMapper {
  @Select("select * from students")
  List<LinkedHashMap> selectMap();	//返回值自动映射,LinkedHashMap可保证查询结果有序

  @Select("select * from students")
  List<Student> selectEntity();	//返回值自动映射
}

用于测试关键代码片段,sqlSession为创建的SqlSession对象,其他代码省略

StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);

/*普通实体类映射方式*/
List<Student> students = studentMapper.selectEntity();
System.out.println(students);	 //输出结果

/*Map映射方式方式*/
List<LinkedHashMap> studentMaps = studentMapper.selectMap();
System.out.println(studentMaps);	 //输出结果

resultMap

xml方式

<resultMap id="selectResultMap" type="com.StudentAndCourse">	<!-- id属性用于快速查找到该结果映射,type属性标记映射的实体类-->
  <id property="student.stuId" column="stu_id"/>	<!-- id标签用于标记主键字段的映射规则-->
  <!-- property属性为当前实体类中的属性,若属性是对象可以使用点运算符向后取值-->
  <!-- column属性为SQL语句查询的结果集的列-->
  
  <!-- 下面是selectResultMap实体类中student对象属性的映射关系-->
  <result property="student.stuName" column="stu_name"/>
  <result property="student.stuSex" column="stu_sex"/>
  
  <!-- 下面是selectResultMap实体类中course对象属性的映射关系-->
  <result property="course.cId" column="c_id"/>
  <result property="course.cName" column="c_name"/>
</resultMap>
<select id="selectResultMap" resultMap="selectResultMap">
  select * from students left outer join courses using(c_id)
</select>

用于测试关键代码片段,sqlSession为创建的SqlSession对象,其他代码省略

List<StudentAndCourse> studentAndCourses = sqlSession.selectList("student.selectResultMap");
System.out.println(studentAndCourses);  //输出结果

注解方式

package com;

import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface StudentMapper {
  @Select("select * from students left outer join courses using(c_id)")
  @Results(id = "results", value = {  //Results写的过于麻烦,可为其起一个ID名,方便其他地方调用
      @Result(property = "student.stuId", column = "stu_id", id = true),  //指定主键字段将id属性设置为true
      @Result(property = "student.stuName", column = "stu_name"),
      @Result(property = "student.stuSex", column = "stu_sex"),
      @Result(property = "course.cId", column = "c_id"),
      @Result(property = "course.cName", column = "c_name")
  })
  List<StudentAndCourse> selectResultMap();

  @Select("select * from students left outer join courses using(c_id)")
  @ResultMap("com.StudentMapper.results")
  //ResultMap注解用于调用有ID值的Results注解,省略包名和接口名会从该接口中找
  List<StudentAndCourse> selectResultMapCopy();  //复制的方法用于测试
}

用于测试关键代码片段,sqlSession为创建的SqlSession对象,其他代码省略

StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);

List<StudentAndCourse> studentAndCourse = studentMapper.selectResultMap();
System.out.println(studentAndCourse);	 //输出结果
studentAndCourse = studentMapper.selectResultMapCopy();
System.out.println(studentAndCourse);	 //输出结果
此作者没有提供个人介绍
最后更新于 2022-04-10