欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

mybatis教程之resultmap_動力節(jié)點(diǎn)Java學(xué)院整理

 更新時間:2017年09月01日 11:46:09   作者:limingnihao  
這篇文章主要介紹了mybatis教程之resultmap,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

SQL 映射XML 文件是所有sql語句放置的地方。需要定義一個workspace,一般定義為對應(yīng)的接口類的路徑。寫好SQL語句映射文件后,需要在MyBAtis配置文件mappers標(biāo)簽中引用,例如:

<mappers> 
  <mapper resource="com/bjpowernode/manager/data/mappers/UserMapper.xml" /> 
  <mapper resource="com/bjpowernode/manager/data/mappers/StudentMapper.xml" /> 
  <mapper resource="com/bjpowernode/manager/data/mappers/ClassMapper.xml" /> 
  <mapper resource="com/bjpowernode/manager/data/mappers/TeacherMapper.xml" /> 
</mappers> 

當(dāng)Java接口與XML文件在一個相對路徑下時,可以不在myBatis配置文件的mappers中聲明。 

SQL 映射XML 文件一些初級的元素:

1. cache – 配置給定模式的緩存
2. cache-ref – 從別的模式中引用一個緩存
3. resultMap – 這是最復(fù)雜而卻強(qiáng)大的一個元素了,它描述如何從結(jié)果集中加載對象
4. sql – 一個可以被其他語句復(fù)用的SQL 塊
5. insert – 映射INSERT 語句
6. update – 映射UPDATE 語句
7. delete – 映射DELEETE 語句
8. select  -  映射SELECT語句 

2.1 resultMap

resultMap 是MyBatis 中最重要最強(qiáng)大的元素了。你可以讓你比使用JDBC 調(diào)用結(jié)果集省掉90%的代碼,也可以讓你做許多JDBC 不支持的事?,F(xiàn)實(shí)上,要寫一個等同類似于交互的映射這樣的復(fù)雜語句,可能要上千行的代碼。ResultMaps 的目的,就是這樣簡單的語句而不需要多余的結(jié)果映射,更多復(fù)雜的語句,除了只要一些絕對必須的語句描述關(guān)系以外,再也不需要其它的。

resultMap屬性:type為java實(shí)體類;id為此resultMap的標(biāo)識。

resultMap可以設(shè)置的映射:

1. constructor – 用來將結(jié)果反射給一個實(shí)例化好的類的構(gòu)造器

a) idArg – ID 參數(shù);將結(jié)果集標(biāo)記為ID,以方便全局調(diào)用
b) arg –反射到構(gòu)造器的通常結(jié)果

2. id – ID 結(jié)果,將結(jié)果集標(biāo)記為ID,以方便全局調(diào)用

3. result – 反射到JavaBean 屬性的普通結(jié)果

4. association – 復(fù)雜類型的結(jié)合;多個結(jié)果合成的類型

a) nested result mappings – 幾resultMap 自身嵌套關(guān)聯(lián),也可以引用到一個其它上

5. collection –復(fù)雜類型集合a collection of complex types

6. nested result mappings – resultMap 的集合,也可以引用到一個其它上

7. discriminator – 使用一個結(jié)果值以決定使用哪個resultMap

a) case – 基本一些值的結(jié)果映射的case 情形

i. nested result mappings –一個case 情形本身就是一個結(jié)果映射,因此也可以包括一些相同的元素,也可以引用一個外部resultMap。

id、result

id、result是最簡單的映射,id為主鍵映射;result其他基本數(shù)據(jù)庫表字段到實(shí)體類屬性的映射。

最簡單的例子:

<resultMap type="bjpowernodestudentmanagerdatamodelStudentEntity" 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="orgapacheibatistypeBlobTypeHandler" /> 
</resultMap> 

id、result語句屬性配置細(xì)節(jié):

屬性 描述  

屬性
描述
 
property
需要映射到JavaBean 的屬性名稱。
 
column
數(shù)據(jù)表的列名或者標(biāo)簽別名。
 
javaType
一個完整的類名,或者是一個類型別名。如果你匹配的是一個JavaBean,那MyBatis 通常會自行檢測到。然后,如果你是要映射到一個HashMap,那你需要指定javaType 要達(dá)到的目的。
 
jdbcType
數(shù)據(jù)表支持的類型列表。這個屬性只在insert,update 或delete 的時候針對允許空的列有用。JDBC 需要這項,但MyBatis 不需要。如果你是直接針對JDBC 編碼,且有允許空的列,而你要指定這項。
 
typeHandler
使用這個屬性可以覆寫類型處理器。這項值可以是一個完整的類名,也可以是一個類型別名。
 

支持的JDBC類型

為了將來的引用,MyBatis 支持下列JDBC 類型,通過JdbcType 枚舉:

BIT,F(xiàn)LOAT,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

constructor

我們使用id、result時候,需要定義java實(shí)體類的屬性映射到數(shù)據(jù)庫表的字段上。這個時候是使用JavaBean實(shí)現(xiàn)的。當(dāng)然我們也可以使用實(shí)體類的構(gòu)造方法來實(shí)現(xiàn)值的映射,這個時候是通過構(gòu)造方法參數(shù)的書寫的順序來進(jìn)行賦值的。

使用construcotr功能有限(例如使用collection級聯(lián)查詢)。

上面使用id、result實(shí)現(xiàn)的功能就可以改為:

<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> 

當(dāng)然,我們需要定義StudentEntity實(shí)體類的構(gòu)造方法: 

public StudentEntity(String studentID, String studentName, String studentSex, Date studentBirthday){ 
  this.studentID = studentID; 
  this.studentName = studentName; 
  this.studentSex = studentSex; 
  this.studentBirthday = studentBirthday; 
} 

association聯(lián)合

聯(lián)合元素用來處理“一對一”的關(guān)系。需要指定映射的Java實(shí)體類的屬性,屬性的javaType(通常MyBatis 自己會識別)。對應(yīng)的數(shù)據(jù)庫表的列名稱。如果想覆寫的話返回結(jié)果的值,需要指定typeHandler。

不同情況需要告訴MyBatis 如何加載一個聯(lián)合。MyBatis 可以用兩種方式加載:

1. select: 執(zhí)行一個其它映射的SQL 語句返回一個Java實(shí)體類型。較靈活;

2. resultsMap: 使用一個嵌套的結(jié)果映射來處理通過join查詢結(jié)果集,映射成Java實(shí)體類型。

例如,一個班級對應(yīng)一個班主任。

首先定義好班級中的班主任屬性:

private TeacherEntity teacherEntity; 

使用select實(shí)現(xiàn)聯(lián)合

例:班級實(shí)體類中有班主任的屬性,通過聯(lián)合在得到一個班級實(shí)體時,同時映射出班主任實(shí)體。
這樣可以直接復(fù)用在TeacherMapper.xml文件中定義好的查詢teacher根據(jù)其ID的select語句。而且不需要修改寫好的SQL語句,只需要直接修改resultMap即可。

 ClassMapper.xml文件部分內(nèi)容:

<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 CTCLASS_ID = #{classID}; 
</select> 

 TeacherMapper.xml文件部分內(nèi)容:

<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實(shí)現(xiàn)聯(lián)合

與上面同樣的功能,查詢班級,同時查詢器班主任。需在association中添加resultMap(在teacher的xml文件中定義好的),新寫sql(查詢班級表left join教師表),不需要teacher的select。

 修改ClassMapper.xml文件部分內(nèi)容:

<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文件部分內(nèi)容中。

collection聚集

聚集元素用來處理“一對多”的關(guān)系。需要指定映射的Java實(shí)體類的屬性,屬性的javaType(一般為ArrayList);列表中對象的類型ofType(Java實(shí)體類);對應(yīng)的數(shù)據(jù)庫表的列名稱;

不同情況需要告訴MyBatis 如何加載一個聚集。MyBatis 可以用兩種方式加載:

1. select: 執(zhí)行一個其它映射的SQL 語句返回一個Java實(shí)體類型。較靈活;

2. resultsMap: 使用一個嵌套的結(jié)果映射來處理通過join查詢結(jié)果集,映射成Java實(shí)體類型。

例如,一個班級有多個學(xué)生。

首先定義班級中的學(xué)生列表屬性:

private List<StudentEntity> studentList; 

使用select實(shí)現(xiàn)聚集

用法和聯(lián)合很類似,區(qū)別在于,這是一對多,所以一般映射過來的都是列表。所以這里需要定義javaType為ArrayList,還需要定義列表中對象的類型ofType,以及必須設(shè)置的select的語句名稱(需要注意的是,這里的查詢student的select語句條件必須是外鍵classID)。

ClassMapper.xml文件部分內(nèi)容:

<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文件部分內(nèi)容:

<!-- java屬性,數(shù)據(jù)庫表字段之間的映射定義 --> 
<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> 
 
<!-- 查詢學(xué)生list,根據(jù)班級id --> 
<select id="getStudentByClassID" parameterType="String" resultMap="studentResultMap"> 
  <include refid="selectStudentAll" /> 
  WHERE STCLASS_ID = #{classID} 
</select> 

使用resultMap實(shí)現(xiàn)聚集

使用resultMap,就需要重寫一個sql,left join學(xué)生表。

<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文件部分內(nèi)容中。studentResultMap請見上面StudentMapper.xml文件部分內(nèi)容中。

discriminator鑒別器

有時一個單獨(dú)的數(shù)據(jù)庫查詢也許返回很多不同(但是希望有些關(guān)聯(lián))數(shù)據(jù)類型的結(jié)果集。鑒別器元素就是被設(shè)計來處理這個情況的,還有包括類的繼承層次結(jié)構(gòu)。鑒別器非常容易理解,因為它的表現(xiàn)很像Java語言中的switch語句。

定義鑒別器指定了column和javaType屬性。列是MyBatis查找比較值的地方。JavaType是需要被用來保證等價測試的合適類型(盡管字符串在很多情形下都會有用)。

下面這個例子為,當(dāng)classId為20000001時,才映射classId屬性。

<resultMap type="bjpowernodestudentmanagerdatamodelStudentEntity" 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="orgapacheibatistypeBlobTypeHandler" /> 
  <result property="placeId"      column="PLACE_ID" javaType="String" jdbcType="VARCHAR"/> 
  <discriminator column="CLASS_ID" javaType="String" jdbcType="VARCHAR"> 
    <case value="20000001" resultType="bjpowernodestudentmanagerdatamodelStudentEntity" > 
      <result property="classId" column="CLASS_ID" javaType="String" jdbcType="VARCHAR"/> 
    </case> 
  </discriminator> 
</resultMap> 

相關(guān)文章

  • java編寫簡單的E-mail發(fā)送端程序

    java編寫簡單的E-mail發(fā)送端程序

    這篇文章主要介紹了使用java語言編寫一個簡單的E-mail發(fā)送端程序,感興趣的小伙伴們可以參考一下
    2016-02-02
  • JAVA 開發(fā)之用靜態(tài)方法返回類名的實(shí)例詳解

    JAVA 開發(fā)之用靜態(tài)方法返回類名的實(shí)例詳解

    這篇文章主要介紹了JAVA 開發(fā)之用靜態(tài)方法返回類名的實(shí)例詳解的相關(guān)資料,這里主要說明使用異常來得到類名,希望能幫助到大家,需要的朋友可以參考下
    2017-08-08
  • SpringCloud中的Ribbon負(fù)載均衡器詳細(xì)解析

    SpringCloud中的Ribbon負(fù)載均衡器詳細(xì)解析

    這篇文章主要介紹了SpringCloud中的Ribbon負(fù)載均衡器詳細(xì)解析,Ribbon 是一個基于 HTTP 和 TCP 的客戶端負(fù)載均衡工具,它基于 Netflix Ribbon 實(shí)現(xiàn),通過封裝可以讓我們輕松地將面向服務(wù)的 REST 模版請求自動轉(zhuǎn)換成客戶端負(fù)載均衡的服務(wù)調(diào)用,需要的朋友可以參考下
    2024-01-01
  • 最新評論