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 可以明显从结果中看出, 进行了嵌套查询.
来源: http://www.bubuko.com/infodetail-2990199.html