二、SQL語句映射文件(1)resultMap
SQL 映射XML 文件是所有sql語句放置的地方。需要定義一個workspace,一般定義爲對應的接口類的路徑。寫好SQL語句映射文件後,需要在MyBAtis配置文件mappers標籤中引用,例如:
- <mappers>
- <mapper resource="com/liming/manager/data/mappers/UserMapper.xml" />
- <mapper resource="com/liming/manager/data/mappers/StudentMapper.xml" />
- <mapper resource="com/liming/manager/data/mappers/ClassMapper.xml" />
- <mapper resource="com/liming/manager/data/mappers/TeacherMapper.xml" />
- </mappers>
<mappers> <mapper resource="com/liming/manager/data/mappers/UserMapper.xml" /> <mapper resource="com/liming/manager/data/mappers/StudentMapper.xml" /> <mapper resource="com/liming/manager/data/mappers/ClassMapper.xml" /> <mapper resource="com/liming/manager/data/mappers/TeacherMapper.xml" /></mappers>
當Java接口與XML文件在一個相對路徑下時,可以不在myBatis配置文件的mappers中聲明。
SQL 映射XML 文件一些初級的元素:
1. cache – 配置給定模式的緩存
2. cache-ref – 從別的模式中引用一個緩存
3. resultMap – 這是最複雜而卻強大的一個元素了,它描述如何從結果集中加載對象
4. sql – 一個可以被其他語句複用的SQL 塊
5. insert – 映射INSERT 語句
6. update – 映射UPDATE 語句
7. delete – 映射DELEETE 語句
8. select - 映射SELECT語句
2.1 resultMap
resultMap 是MyBatis 中最重要最強大的元素了。你可以讓你比使用JDBC 調用結果集省掉90%的代碼,也可以讓你做許多JDBC 不支持的事。現實上,要寫一個等同類似於交互的映射這樣的複雜語句,可能要上千行的代碼。ResultMaps 的目的,就是這樣簡單的語句而不需要多餘的結果映射,更多複雜的語句,除了只要一些絕對必須的語句描述關係以外,再也不需要其它的。
resultMap屬性:type爲java實體類;id爲此resultMap的標識。
resultMap可以設置的映射:
1. constructor – 用來將結果反射給一個實例化好的類的構造器
a) idArg – ID 參數;將結果集標記爲ID,以方便全局調用
b) arg –反射到構造器的通常結果
2. id – ID 結果,將結果集標記爲ID,以方便全局調用
3. result – 反射到JavaBean 屬性的普通結果
4. association – 複雜類型的結合;多個結果合成的類型
a) nested result mappings – 幾resultMap 自身嵌套關聯,也可以引用到一個其它上
5. collection –複雜類型集合a collection of complex types
6. nested result mappings – resultMap 的集合,也可以引用到一個其它上
7. discriminator – 使用一個結果值以決定使用哪個resultMap
a) case – 基本一些值的結果映射的case 情形
i. nested result mappings –一個case 情形本身就是一個結果映射,因此也可以包括一些相同的元素,也可以引用一個外部resultMap。
2.1.1 id、result
id、result是最簡單的映射,id爲主鍵映射;result其他基本數據庫表字段到實體類屬性的映射。
最簡單的例子:
- <resultMap type="liming.student.manager.data.model.StudentEntity" id="studentResultMap">
- <id property="studentId" column="STUDENT_ID" javaType="String" jdbcType="VARCHAR"/>
- <result property="studentName" column="STUDENT_NAME" javaType="String" jdbcType="VARCHAR"/>
- <result property="studentSex" column="STUDENT_SEX" javaType="int" jdbcType="INTEGER"/>
- <result property="studentBirthday" column="STUDENT_BIRTHDAY" javaType="Date" jdbcType="DATE"/>
- <result property="studentPhoto" column="STUDENT_PHOTO" javaType="byte[]" jdbcType="BLOB" typeHandler="org.apache.ibatis.type.BlobTypeHandler" />
- </resultMap>
<resultMap type="liming.student.manager.data.model.StudentEntity" id="studentResultMap"> <id property="studentId" column="STUDENT_ID" javaType="String" jdbcType="VARCHAR"/> <result property="studentName" column="STUDENT_NAME" javaType="String" jdbcType="VARCHAR"/> <result property="studentSex" column="STUDENT_SEX" javaType="int" jdbcType="INTEGER"/> <result property="studentBirthday" column="STUDENT_BIRTHDAY" javaType="Date" jdbcType="DATE"/> <result property="studentPhoto" column="STUDENT_PHOTO" javaType="byte[]" jdbcType="BLOB" typeHandler="org.apache.ibatis.type.BlobTypeHandler" /></resultMap>
id、result語句屬性配置細節:
屬性 |
描述 |
|
property |
需要映射到JavaBean 的屬性名稱。 |
|
column |
數據表的列名或者標籤別名。 |
|
javaType |
一個完整的類名,或者是一個類型別名。如果你匹配的是一個JavaBean,那MyBatis 通常會自行檢測到。然後,如果你是要映射到一個HashMap,那你需要指定javaType 要達到的目的。 |
|
jdbcType |
數據表支持的類型列表。這個屬性只在insert,update 或delete 的時候針對允許空的列有用。JDBC 需要這項,但MyBatis 不需要。如果你是直接針對JDBC 編碼,且有允許空的列,而你要指定這項。 |
|
typeHandler |
使用這個屬性可以覆寫類型處理器。這項值可以是一個完整的類名,也可以是一個類型別名。 |
|
支持的JDBC類型
爲了將來的引用,MyBatis 支持下列JDBC 類型,通過JdbcType 枚舉:
BIT,FLOAT,CHAR,TIMESTAMP,OTHER,UNDEFINED,TINYINT,REAL,VARCHAR,BINARY,BLOB,NVARCHAR,SMALLINT,DOUBLE,LONGVARCHAR,VARBINARY,CLOB,NCHAR,INTEGER,NUMERIC,DATE,LONGVARBINARY,BOOLEAN,NCLOB,BIGINT,DECIMAL,TIME,NULL,CURSOR
2.1.2 constructor
我們使用id、result時候,需要定義java實體類的屬性映射到數據庫表的字段上。這個時候是使用JavaBean實現的。當然我們也可以使用實體類的構造方法來實現值的映射,這個時候是通過構造方法參數的書寫的順序來進行賦值的。
使用construcotr功能有限(例如使用collection級聯查詢)。
上面使用id、result實現的功能就可以改爲:
- <resultMap type="StudentEntity" id="studentResultMap" >
- <constructor>
- <idArg javaType="String" column="STUDENT_ID"/>
- <arg javaType="String" column="STUDENT_NAME"/>
- <arg javaType="String" column="STUDENT_SEX"/>
- <arg javaType="Date" column="STUDENT_BIRTHDAY"/>
- </constructor>
- </resultMap>
<resultMap type="StudentEntity" id="studentResultMap" > <constructor> <idArg javaType="String" column="STUDENT_ID"/> <arg javaType="String" column="STUDENT_NAME"/> <arg javaType="String" column="STUDENT_SEX"/> <arg javaType="Date" column="STUDENT_BIRTHDAY"/> </constructor></resultMap>
當然,我們需要定義StudentEntity實體類的構造方法:
- public StudentEntity(String studentID, String studentName, String studentSex, Date studentBirthday){
- this.studentID = studentID;
- this.studentName = studentName;
- this.studentSex = studentSex;
- this.studentBirthday = studentBirthday;
- }
public StudentEntity(String studentID, String studentName, String studentSex, Date studentBirthday){ this.studentID = studentID; this.studentName = studentName; this.studentSex = studentSex; this.studentBirthday = studentBirthday;}
2.1.3 association聯合
聯合元素用來處理“一對一”的關係。需要指定映射的Java實體類的屬性,屬性的javaType(通常MyBatis 自己會識別)。對應的數據庫表的列名稱。如果想覆寫的話返回結果的值,需要指定typeHandler。
不同情況需要告訴MyBatis 如何加載一個聯合。MyBatis 可以用兩種方式加載:
1. select: 執行一個其它映射的SQL 語句返回一個Java實體類型。較靈活;
2. resultsMap: 使用一個嵌套的結果映射來處理通過join查詢結果集,映射成Java實體類型。
例如,一個班級對應一個班主任。
首先定義好班級中的班主任屬性:
private TeacherEntity teacherEntity;
2.1.3.1使用select實現聯合
例:班級實體類中有班主任的屬性,通過聯合在得到一個班級實體時,同時映射出班主任實體。
這樣可以直接複用在TeacherMapper.xml文件中定義好的查詢teacher根據其ID的select語句。而且不需要修改寫好的SQL語句,只需要直接修改resultMap即可。
ClassMapper.xml文件部分內容:
- <resultMap type="ClassEntity" id="classResultMap">
- <id property="classID" column="CLASS_ID" />
- <result property="className" column="CLASS_NAME" />
- <result property="classYear" column="CLASS_YEAR" />
- <association property="teacherEntity" column="TEACHER_ID" select="getTeacher"/>
- </resultMap>
- <select id="getClassByID" parameterType="String" resultMap="classResultMap">
- SELECT * FROM CLASS_TBL CT
- WHERE CT.CLASS_ID = #{classID};
- </select>
<resultMap type="ClassEntity" id="classResultMap"> <id property="classID" column="CLASS_ID" /> <result property="className" column="CLASS_NAME" /> <result property="classYear" column="CLASS_YEAR" /> <association property="teacherEntity" column="TEACHER_ID" select="getTeacher"/></resultMap><select id="getClassByID" parameterType="String" resultMap="classResultMap"> SELECT * FROM CLASS_TBL CT WHERE CT.CLASS_ID = #{classID};</select>
TeacherMapper.xml文件部分內容:
- <resultMap type="TeacherEntity" id="teacherResultMap">
- <id property="teacherID" column="TEACHER_ID" />
- <result property="teacherName" column="TEACHER_NAME" />
- <result property="teacherSex" column="TEACHER_SEX" />
- <result property="teacherBirthday" column="TEACHER_BIRTHDAY"/>
- <result property="workDate" column="WORK_DATE"/>
- <result property="professional" column="PROFESSIONAL"/>
- </resultMap>
- <select id="getTeacher" parameterType="String" resultMap="teacherResultMap">
- SELECT *
- FROM TEACHER_TBL TT
- WHERE TT.TEACHER_ID = #{teacherID}
- </select>
<resultMap type="TeacherEntity" id="teacherResultMap"> <id property="teacherID" column="TEACHER_ID" /> <result property="teacherName" column="TEACHER_NAME" /> <result property="teacherSex" column="TEACHER_SEX" /> <result property="teacherBirthday" column="TEACHER_BIRTHDAY"/> <result property="workDate" column="WORK_DATE"/> <result property="professional" column="PROFESSIONAL"/></resultMap><select id="getTeacher" parameterType="String" resultMap="teacherResultMap"> SELECT * FROM TEACHER_TBL TT WHERE TT.TEACHER_ID = #{teacherID}</select>
2.1.3.2使用resultMap實現聯合
與上面同樣的功能,查詢班級,同時查詢器班主任。需在association中添加resultMap(在teacher的xml文件中定義好的),新寫sql(查詢班級表left join教師表),不需要teacher的select。
修改ClassMapper.xml文件部分內容:
- <resultMap type="ClassEntity" id="classResultMap">
- <id property="classID" column="CLASS_ID" />
- <result property="className" column="CLASS_NAME" />
- <result property="classYear" column="CLASS_YEAR" />
- <association property="teacherEntity" column="TEACHER_ID" resultMap="teacherResultMap"/>
- </resultMap>
- <select id="getClassAndTeacher" parameterType="String" resultMap="classResultMap">
- SELECT *
- FROM CLASS_TBL CT LEFT JOIN TEACHER_TBL TT ON CT.TEACHER_ID = TT.TEACHER_ID
- WHERE CT.CLASS_ID = #{classID};
- </select>
<resultMap type="ClassEntity" id="classResultMap"> <id property="classID" column="CLASS_ID" /> <result property="className" column="CLASS_NAME" /> <result property="classYear" column="CLASS_YEAR" /> <association property="teacherEntity" column="TEACHER_ID" resultMap="teacherResultMap"/></resultMap><select id="getClassAndTeacher" parameterType="String" resultMap="classResultMap"> SELECT * FROM CLASS_TBL CT LEFT JOIN TEACHER_TBL TT ON CT.TEACHER_ID = TT.TEACHER_ID WHERE CT.CLASS_ID = #{classID};</select>
其中的teacherResultMap請見上面TeacherMapper.xml文件部分內容中。
2.1.4 collection聚集
聚集元素用來處理“一對多”的關係。需要指定映射的Java實體類的屬性,屬性的javaType(一般爲ArrayList);列表中對象的類型ofType(Java實體類);對應的數據庫表的列名稱;
不同情況需要告訴MyBatis 如何加載一個聚集。MyBatis 可以用兩種方式加載:
1. select: 執行一個其它映射的SQL 語句返回一個Java實體類型。較靈活;
2. resultsMap: 使用一個嵌套的結果映射來處理通過join查詢結果集,映射成Java實體類型。
例如,一個班級有多個學生。
首先定義班級中的學生列表屬性:
private List<StudentEntity> studentList;
2.1.4.1使用select實現聚集
用法和聯合很類似,區別在於,這是一對多,所以一般映射過來的都是列表。所以這裏需要定義javaType爲ArrayList,還需要定義列表中對象的類型ofType,以及必須設置的select的語句名稱(需要注意的是,這裏的查詢student的select語句條件必須是外鍵classID)。
ClassMapper.xml文件部分內容:
- <resultMap type="ClassEntity" id="classResultMap">
- <id property="classID" column="CLASS_ID" />
- <result property="className" column="CLASS_NAME" />
- <result property="classYear" column="CLASS_YEAR" />
- <association property="teacherEntity" column="TEACHER_ID" select="getTeacher"/>
- <collection property="studentList" column="CLASS_ID" javaType="ArrayList" ofType="StudentEntity" select="getStudentByClassID"/>
- </resultMap>
- <select id="getClassByID" parameterType="String" resultMap="classResultMap">
- SELECT * FROM CLASS_TBL CT
- WHERE CT.CLASS_ID = #{classID};
- </select>
<resultMap type="ClassEntity" id="classResultMap"> <id property="classID" column="CLASS_ID" /> <result property="className" column="CLASS_NAME" /> <result property="classYear" column="CLASS_YEAR" /> <association property="teacherEntity" column="TEACHER_ID" select="getTeacher"/> <collection property="studentList" column="CLASS_ID" javaType="ArrayList" ofType="StudentEntity" select="getStudentByClassID"/></resultMap><select id="getClassByID" parameterType="String" resultMap="classResultMap"> SELECT * FROM CLASS_TBL CT WHERE CT.CLASS_ID = #{classID};</select>
StudentMapper.xml文件部分內容:
- <!-- java屬性,數據庫表字段之間的映射定義 -->
- <resultMap type="StudentEntity" id="studentResultMap">
- <id property="studentID" column="STUDENT_ID" />
- <result property="studentName" column="STUDENT_NAME" />
- <result property="studentSex" column="STUDENT_SEX" />
- <result property="studentBirthday" column="STUDENT_BIRTHDAY" />
- </resultMap>
- <!-- 查詢學生list,根據班級id -->
- <select id="getStudentByClassID" parameterType="String" resultMap="studentResultMap">
- <include refid="selectStudentAll" />
- WHERE ST.CLASS_ID = #{classID}
- </select>
<!-- java屬性,數據庫表字段之間的映射定義 --><resultMap type="StudentEntity" id="studentResultMap"> <id property="studentID" column="STUDENT_ID" /> <result property="studentName" column="STUDENT_NAME" /> <result property="studentSex" column="STUDENT_SEX" /> <result property="studentBirthday" column="STUDENT_BIRTHDAY" /></resultMap><!-- 查詢學生list,根據班級id --><select id="getStudentByClassID" parameterType="String" resultMap="studentResultMap"> <include refid="selectStudentAll" /> WHERE ST.CLASS_ID = #{classID}</select>
2.1.4.2使用resultMap實現聚集
使用resultMap,就需要重寫一個sql,left join學生表。
- <resultMap type="ClassEntity" id="classResultMap">
- <id property="classID" column="CLASS_ID" />
- <result property="className" column="CLASS_NAME" />
- <result property="classYear" column="CLASS_YEAR" />
- <association property="teacherEntity" column="TEACHER_ID" resultMap="teacherResultMap"/>
- <collection property="studentList" column="CLASS_ID" javaType="ArrayList" ofType="StudentEntity" resultMap="studentResultMap"/>
- </resultMap>
- <select id="getClassAndTeacherStudent" parameterType="String" resultMap="classResultMap">
- SELECT *
- FROM CLASS_TBL CT
- LEFT JOIN STUDENT_TBL ST
- ON CT.CLASS_ID = ST.CLASS_ID
- LEFT JOIN TEACHER_TBL TT
- ON CT.TEACHER_ID = TT.TEACHER_ID
- WHERE CT.CLASS_ID = #{classID};
- </select>
<resultMap type="ClassEntity" id="classResultMap"> <id property="classID" column="CLASS_ID" /> <result property="className" column="CLASS_NAME" /> <result property="classYear" column="CLASS_YEAR" /> <association property="teacherEntity" column="TEACHER_ID" resultMap="teacherResultMap"/> <collection property="studentList" column="CLASS_ID" javaType="ArrayList" ofType="StudentEntity" resultMap="studentResultMap"/></resultMap><select id="getClassAndTeacherStudent" parameterType="String" resultMap="classResultMap"> SELECT * FROM CLASS_TBL CT LEFT JOIN STUDENT_TBL ST ON CT.CLASS_ID = ST.CLASS_ID LEFT JOIN TEACHER_TBL TT ON CT.TEACHER_ID = TT.TEACHER_ID WHERE CT.CLASS_ID = #{classID};</select>
其中的teacherResultMap請見上面TeacherMapper.xml文件部分內容中。studentResultMap請見上面StudentMapper.xml文件部分內容中。
2.1.5discriminator鑑別器
有時一個單獨的數據庫查詢也許返回很多不同(但是希望有些關聯)數據類型的結果集。鑑別器元素就是被設計來處理這個情況的,還有包括類的繼承層次結構。鑑別器非常容易理解,因爲它的表現很像Java語言中的switch語句。
定義鑑別器指定了column和javaType屬性。列是MyBatis查找比較值的地方。JavaType是需要被用來保證等價測試的合適類型(儘管字符串在很多情形下都會有用)。
下面這個例子爲,當classId爲20000001時,才映射classId屬性。
- <resultMap type="liming.student.manager.data.model.StudentEntity" id="resultMap_studentEntity_discriminator">
- <id property="studentId" column="STUDENT_ID" javaType="String" jdbcType="VARCHAR"/>
- <result property="studentName" column="STUDENT_NAME" javaType="String" jdbcType="VARCHAR"/>
- <result property="studentSex" column="STUDENT_SEX" javaType="int" jdbcType="INTEGER"/>
- <result property="studentBirthday" column="STUDENT_BIRTHDAY" javaType="Date" jdbcType="DATE"/>
- <result property="studentPhoto" column="STUDENT_PHOTO" javaType="byte[]" jdbcType="BLOB" typeHandler="org.apache.ibatis.type.BlobTypeHandler" />
- <result property="placeId" column="PLACE_ID" javaType="String" jdbcType="VARCHAR"/>
- <discriminator column="CLASS_ID" javaType="String" jdbcType="VARCHAR">
- <case value="20000001" resultType="liming.student.manager.data.model.StudentEntity" >
- <result property="classId" column="CLASS_ID" javaType="String" jdbcType="VARCHAR"/>
- </case>
- </discriminator>
- </resultMap>