MyBatis resultMap元素詳解
- MyBatis resultMap元素詳解
- resultMap元素屬性
- 1. id:映射規則集的唯一標識,可以被select元素的resultMap屬性應用
- 2.type:映射的結果型別,這里指定封裝成userList實體
- resultMap元素包含以下子元素
- 1. id:指定和資料表主鍵欄位對應的標識屬性,設定此項可以提升MyBatis框架的性能,特別是應用快取和嵌套結果映射的時候,
- 2.result:指定結果集欄位和物體類屬性的映射關系,
- 3.association:映射到JavaBean某個“復雜型別”屬性,比如JavaBean類
- 4.collection:映射到JavaBean某個“復雜型別”屬性,比如集合
- resultMap如何將結果映射到結果集
- 如何使用association元素嵌套結果映射(一般使用一對一查詢)
- 如何使用collection元素嵌套結果映射(一般使用一對多查詢)
- resultMap的自動映射的三種行為
MyBatis resultMap元素詳解
resultMap元素屬性
1. id:映射規則集的唯一標識,可以被select元素的resultMap屬性應用
2.type:映射的結果型別,這里指定封裝成userList實體
resultMap元素包含以下子元素
1. id:指定和資料表主鍵欄位對應的標識屬性,設定此項可以提升MyBatis框架的性能,特別是應用快取和嵌套結果映射的時候,
2.result:指定結果集欄位和物體類屬性的映射關系,
3.association:映射到JavaBean某個“復雜型別”屬性,比如JavaBean類
4.collection:映射到JavaBean某個“復雜型別”屬性,比如集合
resultMap如何將結果映射到結果集
<!--id是唯一的,對應resultMap的唯一標識名稱 type是里面回傳的型別-->
<resultMap id="userMap" type="User">
<!--id標簽表示表的主鍵-->
<!--column表示資料庫的列 property表示物體類的屬性-->
<id column="id" property="id"></id>
<!--如果不是主鍵列,就用result表示-->
<result column="roleName" property="userRoleName"></result>
</resultMap>
如何使用association元素嵌套結果映射(一般使用一對一查詢)
/**
* 對用戶和角色進行聯表查詢
* @return
*/
List<User> findAllUserAndRole();
<resultMap id="UserAndRole" type="User">
<id property="id" column="id"></id>
<result property="username" column="userName"></result>
<!--association表示一個物體類-->
<!--association對應User類的屬性值,javaType表示屬性值的型別-->
<association property="role" javaType="Role">
<id property="id" column="roleId"></id>
<result property="roleName" column="roleName"></result>
</association>
</resultMap>
<select id="findAllUserAndRole" resultMap="UserAndRole">
select user.*,role.id as roleId,role.roleName from smbms_user user inner join smbms_role role on user.userRole = role.id
</select>
如何使用collection元素嵌套結果映射(一般使用一對多查詢)
/**
* 根據用戶ID查詢所有的地址資訊
* @return
*/
List<User> findAddressById(Long id);
<resultMap id="UserAndAddressList" type="User">
<id property="id" column="id"></id>
<result property="username" column="userName"></result>
<!--association表示一個物體類-->
<!--association對應User類的屬性值,javaType表示屬性值的型別-->
<collection property="addressList" ofType="com.changan.entity.Address">
<id property="id" column="addressId"></id>
<result property="addressDesc" column="addressDesc"></result>
</collection>
</resultMap>
<select id="findAddressById" resultMap="UserAndAddressList">
select user.*,address.id as addressId,address.addressDesc from smbms_user user inner join smbms_address address on
user.id = address.userId where user.id = #{id}
</select>
resultMap的自動映射的三種行為
- NONE 禁用自動映射,僅為手工映射的屬性賦值,
<settings>
<!-- 啟動 自動映射 -->
<setting name="autoMappingBehavior" value="NONE"/>
</settings>
- PARTIAL 這是默認行為,對于沒有嵌套映射的resultMap使用自動映射;而對于有嵌套映射的resultMap 不使用自動映射,僅為手工映射的屬性賦值
<settings>
<!-- 啟動 自動映射 -->
<setting name="autoMappingBehavior" value="PARTIAL"/>
</settings>
- FULL 全部使用自動映射,即使有嵌套映射的resultMap也會使用自動映射
<settings>
<!-- 啟動 自動映射 -->
<setting name="autoMappingBehavior" value="FULL"/>
</settings>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/275059.html
標籤:其他
