目錄
- ResultMap(結果集映射)
-
- 1 解決資料庫與實體類屬性名不一緻問題
- 2 解決多對一結果集映射(association關聯屬性)
-
- 2.1 按查詢嵌套處理
- 2.2 按結果嵌套處理。
- 3解決一對多結果集映射(collection集合)
-
- 3.1 按查詢嵌套處理。
- 3.2 按結果嵌套處理
ResultMap(結果集映射)
- 為了解決資料映射問題而生
1 解決資料庫與實體類屬性名不一緻問題
解決的問題:屬性名和字段名不一緻
因為各種原因,資料庫的屬性字段無法和實體類的屬性字段一一對應。
舉例:
現在資料庫表屬性為:
實體類屬性值為:
查詢語句:
<select id="selectUserById" parameterType="int" resultType="com.lyj.entity.UserInf">
select id,name,pwd from user where id = #{id}
</select>
這樣查詢,因為pwd在實體類中并不存在,在注入值時無法注入。出現password屬性為空的情況。
解決方案
- 使用别名(可簡寫 pwd password)
<select id="selectUserById" parameterType="int" resultType="com.lyj.entity.UserInf"> select id,name,pwd as password from user where id = #{id} </select>
- 使用結果集映射->ResultMap 【推薦】
<resultMap id="UserMap" type="User">
<!-- id為主鍵 -->
<id column="id" property="id"/>
<!-- column是資料庫表的列名 , property是對應實體類的屬性名 -->
<result column="name" property="name"/>
<result column="pwd" property="password"/>
</resultMap>
<select id="selectUserById" parameterType="int" resultMap="UserMap">
select id,name,pwd from user where id = #{id}
</select>
2 解決多對一結果集映射(association關聯屬性)
多對一了解:
- 多個學生對應一個老師。
- 資料庫概念了解:學生擁有一個tid與教師id對應。
- 實體類概念了解:學生擁有一個教師對象。
2.1 按查詢嵌套處理
- 實體類student和teacher
public class Student { private int id; private String name; private Teacher teacher; }
public class Teacher { private int id; private String name; }
- StudentMapper增加接口
//查詢所有學生 public List<Student> queryAllStudents();
- StudentMapper.xml增加對應方法(對于對象屬性teacher采用關聯屬性)
<mapper namespace="com.lyj.mapper.StudentMapper"> <select id="queryAllStudents" resultMap="StudentTeacher"> select id, name, tid from student </select> <resultMap id="StudentTeacher" type="com.lyj.entity.Student"> <!--association關聯屬性 property屬性名 javaType屬性類型 column是将查詢到的tid指派給id并傳遞下去 select是對應的id--> <association property="teacher" column="{id=tid}" javaType="com.lyj.entity.Teacher" select="getTeacher"/> </resultMap> <!-- 這裡傳遞過來的id,隻有一個屬性的時候,下面可以寫任何值 association中column多參數配置: column="{key=value,key=value}" 其實就是鍵值對的形式,key是傳給下個sql的取值名稱,value是片段一中sql查詢的 字段名。--> <select id="getTeacher" resultType="com.lyj.entity.Teacher"> select id, name from teacher where id = #{id} </select> </mapper>
- 測試(對于對象屬性teacher的屬性采用關聯屬性)
@Test public void testGetStudents(){ SqlSession session = SqlSessionUtil.getSession(); StudentMapper mapper = session.getMapper(StudentMapper.class); List<Student> students = mapper.queryAllStudents(); for (Student student : students){ System.out.println( "學生名:"+ student.getName() +"\t老師:"+student.getTeacher().getName()); } }
2.2 按結果嵌套處理。
<!-- 按查詢結果嵌套處理 思路:1. 直接查詢出結果,進行結果集的映射 -->
<select id="getStudents2" resultMap="StudentTeacher2" >
select s.id sid, s.name sname , t.name tname from student s,teacher t where s.tid = t.id
</select>
<resultMap id="StudentTeacher2" type="com.lyj.entity.Student">
<id property="id" column="sid"/>
<result property="name" column="sname"/>
<!--關聯對象property 關聯對象在Student實體類中的屬性-->
<association property="teacher" javaType="com.lyj.entity.Teacher">
<id property="id" column="sid"/>
<result property="name" column="tname"/>
</association>
</resultMap>
3解決一對多結果集映射(collection集合)
一對多了解:
- 一個老師有多個學生
- 資料庫概念了解:教師擁有一個id與學生tid對應。
- 實體類概念了解:教師擁有一個學生對象List集合。
3.1 按查詢嵌套處理。
- 實體類Student和Teacher
public class Student { private int id; private String name; }
public class Teacher { private int id; private String name; private List<Student> students; }
- TeacherMapper增加方法
- TeacherMapper.xml增加實作
<select id="getTeacher" resultMap="TeacherStudent"> select id, name from teacher where id = #{id} </select> <resultMap id="TeacherStudent" type="com.lyj.entity.Teacher"> <!--column是一對多的外鍵 , 寫的是一的主鍵的列名--> <collection property="students" javaType="ArrayList" ofType="Student" column="{id=id}" select="getStudentByTeacherId"/> </resultMap> <select id="getStudentByTeacherId" resultType="com.lyj.entity.Student"> select id, name, tid from student where tid = #{id} </select>
- 測試
@Test public void testGetTeacher(){ SqlSession session = SqlSessionUtil.getSession(); TeacherMapper mapper = session.getMapper(TeacherMapper.class); Teacher teacher = mapper.getTeacher(1); System.out.println(teacher.getName()); System.out.println(teacher.getStudents()); }
3.2 按結果嵌套處理
<select id="getTeacher2" resultMap="TeacherStudent2">
select s.id sid, s.name sname , t.name tname, t.id tid from student s,teacher t where s.tid = t.id and t.id=#{id}
</select>
<resultMap id="TeacherStudent2" type="com.lyj.entity.Teacher">
<id property="id" column="tid"/>
<result property="name" column="tname"/>
<collection property="students" ofType="com.lyj.entity.Student">
<result property="id" column="sid" />
<result property="name" column="sname" />
<result property="tid" column="tid" />
</collection>
</resultMap>