1、使用的數據腳本爲:
DROP DATABASE IF EXISTS mybatisdb ;
CREATE DATABASE mybatisdb CHARACTER SET UTF8 ;
USE mybatisdb ;
-- 創建數據表
CREATE TABLE type (
tid INT AUTO_INCREMENT ,
title VARCHAR(50) ,
CONSTRAINT pk_tid PRIMARY KEY(tid)
) ;
CREATE TABLE subtype (
stid INT AUTO_INCREMENT ,
title VARCHAR(50) ,
tid INT ,
CONSTRAINT pk_stid PRIMARY KEY(stid) ,
CONSTRAINT fk_tid FOREIGN KEY(tid) REFERENCES type(tid) ON DELETE CASCADE
) ;
INSERT INTO type(title) VALUES ('生活家居') ;
INSERT INTO subtype(title,tid) VALUES ('菜刀',1) ;
INSERT INTO subtype(title,tid) VALUES ('平底鍋',1) ;
INSERT INTO subtype(title,tid) VALUES ('電飯煲',1) ;
2、準備好對應的vo類
public class Type implements Serializable {
private Integer tid ;
private String title ;
private List<Subtype> subtypes ;
}
public class Subtype implements Serializable {
private Integer stid ;
private String title ;
private Type type ;
}
//省略set和get方法
3、準備MyBatis需要的配置文件;
①創建Type.xml 文件:
<?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="vo.mapping.TypeNS">
<resultMap type="Type" id="TypeMap">
<id property="tid" column="tild"/>
<result property="title" column="title"/>
</resultMap>
</mapper>
②創建MemberDetails.xml 文件:
<?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="vo.mapping.SubtypeNS">
<resultMap type="Subtype" id="SubtypeMap">
<id property="stid" column="stid"/>
<result property="title" column="title"/>
</resultMap>
</mapper>
③定義操作別名和映射路徑
<typeAliases>
<typeAlias type="vo.Type" alias="Type"/>
<typeAlias type="vo.Subtype" alias="Subtype"/>
</typeAliases>
<mapper namespace="vo/mapping/TypeNS"/>
<mapper namespace="vo/mapping/SubtypeNS"/>
4、下面進行配置實現數據的查詢;
如果查詢Type的時候應該顯示出所有的Subtype數據;
|- 在Type.xml文件裏面追加有一個根據id查詢的處理操作:
<select id="findById" parameterType="Integer" resultMap="TypeMap">
SELECT tid,title FROM type WHERE tid=#{tid}
</select>
<select id="findAllByType" parameterType="Integer" resultMap="SubtypeMap">
SELECT stid,title,tid FROM subtype WHERE tid=#{tid}
</select>
修改Type.xml文件中的resultMap定義,追加級聯關係:
resultMap type="Type" id="TypeMap">
<id property="tid" column="tild"/>
<result property="title" column="title"/>
<collection property="subtypes" column="tid" javaType="java.util.List" ofType="Subtype" select="cn.mldn.vo.mapping.SubtypeNS.findAllByType"/>
</resultMap>
這個代碼直接使用了兩次查詢,如果要進行全部分類列表的話就會產生嚴重的“1 + N”次查詢問題。這個時候在輸出Subtype數據的時候是無法顯示出type數據的(subtype裏面需要的type內容只有tid數據),如果你確定要顯示這樣的tid數據,那麼就需要去修改Subtype.xml文件中的關聯配置;
<resultMap type="Subtype" id="SubtypeMap">
<id property="stid" column="stid"/>
<result property="title" column="title"/>
<association property="type" javaType="Type" column="tid" resultMap="cn.mldn.vo.mapping.TypeNS.TypeMap"/>
</resultMap>
如果希望在查詢Subtype的時候把對應的Type也查詢出來;
<resultMap type="Subtype" id="SubtypeMap">
<id property="stid" column="stid"/>
<result property="title" column="title"/>
<association property="type" javaType="Type" column="tid" select="cn.mldn.vo.mapping.TypeNS.findById"/>
</resultMap>