1. 程式人生 > >Mybatis學習第17節 -- 嵌套查詢(多對一和一對一)

Mybatis學習第17節 -- 嵌套查詢(多對一和一對一)

tsql mes 店鋪 ltm map div 審核 app 文件

Shop實體類
package io.github.coinsjack.pojo;

import java.io.Serializable;
import java.sql.Date;

public class Shop implements Serializable{
    Area area;
}
Area實體類
public class Area implements Serializable{

Integer id;
String name;
Integer priority;

Date createTime;
Date lastEditTime;
ShopMapper映射文件
<resultMap id="simpleResultMap" type="Shop">
<id column="shop_id" property="id" ></id>
<result column="owner_id" property="ownerId" ></result>
<result column="shop_category_id" property="categoryId" ></result>
<result column="shop_name" property="name"></result>
<result column="shop_desc" property="desc"></result>
<result column="shop_addr" property="addr"></result>
<result column="phone" property="phone"></result>
<result column="shop_img" property="image"></result>
<result column="priority" property="priority"></result>
<result column="create_time" property="createTime"></result>
<result column="last_edit_time" property="lastEditTime"></result>
<result column="enable_status" property="enableStatus"></result>
<association property="area" column="area_id" javaType="Area"
select="io.github.coinsjack.dao.AreaMapper.getAreaById"/>
</resultMap>

<select id="getShopById" parameterType="int" resultMap="simpleResultMap" >
select * from tb_shop where `shop_id` = #{id}
</select>

這裏需要註意, association必須放置在result後面

AreaMapper映射文件
<?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="io.github.coinsjack.dao.AreaMapper">

<cache/>

<resultMap id="areaResultMap" type="Area">
<result column="area_id" property="id"/>
<result column="area_name" property="name"/>
<result column="create_time" property="createTime"/>
<result column="last_edit_time" property="lastEditTime"/>
</resultMap>

<select id="getAreaById" resultMap="areaResultMap">
select * from tb_area
WHERE `area_id` = #{id};
</select>
</mapper>
AreaMapper接口
public interface AreaMapper {

Area getAreaById(Integer id);
}
測試用例
@Test
public void testGetShopById() {
SqlSession session = MyBatisUtil.getSqlSession();
ShopMapper mapper = session.getMapper(ShopMapper.class);
System.out.println(mapper.getShopById(1));
session.close();
}
運行結果
2018-12-28 20:33:07,171 [main] DEBUG [io.github.coinsjack.dao.ShopMapper] - Cache Hit Ratio [io.github.coinsjack.dao.ShopMapper]: 0.0
2018-12-28 20:33:07,565 [main] DEBUG [io.github.coinsjack.dao.ShopMapper.getShopById] - ==> Preparing: select * from tb_shop where `shop_id` = ?
2018-12-28 20:33:07,655 [main] DEBUG [io.github.coinsjack.dao.ShopMapper.getShopById] - ==> Parameters: 1(Integer)
2018-12-28 20:33:07,728 [main] DEBUG [io.github.coinsjack.dao.AreaMapper.getAreaById] - ====> Preparing: select * from tb_area WHERE `area_id` = ?;
2018-12-28 20:33:07,729 [main] DEBUG [io.github.coinsjack.dao.AreaMapper.getAreaById] - ====> Parameters: 3(Integer)
2018-12-28 20:33:07,736 [main] DEBUG [io.github.coinsjack.dao.AreaMapper.getAreaById] - <==== Total: 1
2018-12-28 20:33:07,737 [main] DEBUG [io.github.coinsjack.dao.ShopMapper.getShopById] - <== Total: 1
Shop{id=1, ownerId=1, area=Area{id=3, name=‘長治學院‘, priority=2, createTime=null, lastEditTime=null}, categoryId=14, name=‘new 正式店鋪名稱‘, desc=‘測試描述‘, addr=‘正式地址‘, phone=‘13810524086‘, image=‘/upload/item/shop/1/2017091621545314507.jpg‘, priority=10, createTime=2017-08-03, lastEditTime=2017-09-16, enableStatus=0, advice=‘審核中‘}
總結 觀察Preparing可以明顯從結果中看出,進行了嵌套查詢.

Mybatis學習第17節 -- 嵌套查詢(多對一和一對一)