In this section, we will talk about resultMap. Because a lot of example code has been provided in the previous sections, this section will no longer be introduced. If you need it, you can enter my github, clone or download import your tool try run (methods)
Welcome to Star, or Follow
First, let's share a trick: domain alias @Alias("name")
But this is not recommended, because specifying the classpath of the entity class is already very simplified.
Well, let's start today's content!
Map
-
Map<Object, Object> you just need to write
resultType="map"
-
Map<Object, POJO> needs to specify the key of Map on the interface
@MapKey
resultMap
resultMap package format
<resultMap>
<id /> 主键 底层有优化
column:列名
property:对象的属性
<result>
</>
Related query
- custom package
Example code:
<resultMap id="cp" type="City">
<id column="cId" property="id"/>
<result column="cName" property="name" />
<result column="pId" property="province.id" />
<result column="pName" property="province.name" />
</resultMap>
- association
1. The association customizes the encapsulation rule attribute of a single object, attribute attribute object type
<association property="" javaType=""></>
Example code:
CityMapper.xml
<!-- City findById3 (Integer id); -->
<resultMap id="cp" type="City">
<id column="cId" property="id"/>
<result column="cName" property="name" />
<association property="province" javaType="Province">
<id column="pId" property="id" />
<result column="pName" property="name" />
</association>
</resultMap>
<select id="findById3" resultMap="cp">
SELECT
c.id as cId,
c.name as cName,
p.id as pId,
p.name as pName
FROM
city c, province p
WHERE
c.province_id = p.id
AND
c.id = #{id}
</select>
2、association 分布查询
<association property="" select="......Mpper.get..." column=""></>
Example code:
CityMapper.xml
<!--
association 分步查询
City findById4 (Integer id);
-->
<resultMap id="cp2" type="City">
<id column="id" property="id"/>
<result column="name" property="name" />
<association property="province"
select="com.fengwenyi.mybatis.demo2.dao.ProvinceDao.findById2"
column="id"
><!--fetchType="lazy" // 懒加载-->
<id column="id" property="id" />
<result column="name" property="name" />
</association>
</resultMap>
<select id="findById4" resultMap="cp2">
SELECT
*
FROM
city
WHERE
id = #{id}
</select>
- collection specifies the type of elements in the collection <collection property="" ofType=""></>
Distributed queries use lazy loading lazy: lazy loading / eager: immediate <collection property="" select="" column="" fetchType="lazy"></>
Lazy Loading lazyLoadingEnabled=true
4. How to transmit when there are multiple columns?
Encapsulate Map
column="{key1=column1, key2=column2,....}"
5. discriminator
MyBatis can use discriminator to judge the value of a column, and then change the encapsulation behavior according to the value of a column
<discriminator javaType="" column="">
<case value="" resultType="">
</>
</>
Example code:
<!--
测试鉴别器 discriminator
City findById5 (Integer id);
-->
<resultMap id="cp5" type="City">
<id column="id" property="id"/>
<result column="name" property="name" />
<discriminator javaType="String" column="name">
<case value="巴中" resultType="City">
<id column="id" property="id"/>
<result column="id" property="name" />
</case>
</discriminator>
</resultMap>
<select id="findById5" resultMap="cp5">
SELECT
id, name
FROM
city
WHERE
id = #{id}
</select>