1.对ResultMap结果映射的理解

012.ResultMap结果映射_字段

 

 

 2.流程操作

2.1 在good.xml中编辑代码(利用Map接收关联查询结果)

<!-- 利用LinkedHashMap保存多表关联结果
MyBatis会将每一条记录包装为LinkedHashMap对象
key是字段名 value是字段对应的值 , 字段类型根据表结构进行自动判断
优点: 易于扩展,易于使用
缺点: 太过灵活,无法进行编译时检查
-->
<select id="selectGoodsMap" resultType="java.util.LinkedHashMap" flushCache="true">
select g.* , c.category_name,'1' as test from t_goods g , t_category c
where g.category_id = c.category_id
</select>

2.1.1测试代码

/**
* 利用Map接收关联查询结果
* @throws Exception
*/
@Test
public void testSelectGoodsMap() throws Exception {
SqlSession session = null;
try{
session = MyBatisUtils.openSession();
List<Map> list = session.selectList("goods.selectGoodsMap");
for(Map map : list){
System.out.println(map);
}
}catch (Exception e){
throw e;
}finally {
MyBatisUtils.closeSession(session);
}
}

 

2.2 创建dto(dto是一个特殊的Java  Bean叫做数据传输对象,对原始的对象进行扩展,用于数据保存和传递)

package com.imooc.mybatis.dto;

import com.imooc.mybatis.entity.Category;
import com.imooc.mybatis.entity.Goods;
//Data Transfer Object--数据传输对象
public class GoodsDTO {
private Goods goods = new Goods();
private Category category = new Category();
private String test;

public Goods getGoods() {
return goods;
}

public void setGoods(Goods goods) {
this.goods = goods;
}

public Category getCategory() {
return category;
}

public void setCategory(Category category) {
this.category = category;
}

public String getTest() {
return test;
}

public void setTest(String test) {
this.test = test;
}
}

2.3 针对dto对象,如何让Mybatis自动为其进行对应赋值呢?需要使用resultMap结果映射

<select id="selectGoodsDTO" resultMap="rmGoods">
select g.* , c.*,'1' as test from t_goods g , t_category c
where g.category_id = c.category_id
</select>

<!--结果映射-->
<resultMap id="rmGoods" type="com.imooc.mybatis.dto.GoodsDTO">
<!--设置主键字段与属性映射-->
<!--指定主键: property是属性名指向了dto中某一个属性的具体名称,column是查询结果中某个字段的名字-->
<id property="goods.goodsId" column="goods_id"/>
<!--设置非主键字段与属性映射-->
<result property="goods.title" column="title"/>
<result property="goods.originalCost" column="original_cost"/>
<result property="goods.currentPrice" column="current_price"/>
<result property="goods.discount" column="discount"/>
<result property="goods.isFreeDelivery" column="is_free_delivery"/>
<result property="goods.categoryId" column="category_id"/>
<result property="category.categoryId" column="category_id"/>
<result property="category.categoryName" column="category_name"/>
<result property="category.parentId" column="parent_id"/>
<result property="category.categoryLevel" column="category_level"/>
<result property="category.categoryOrder" column="category_order"/>
<result property="test" column="test"/></resultMap>

2.4 在某个博客看到的一张图片

012.ResultMap结果映射_字段_02

 

 2.5测试语句

/**
* 利用ResultMap进行结果映射
* @throws Exception
*/
@Test
public void testSelectGoodsDTO() throws Exception {
SqlSession session = null;
try{
session = MyBatisUtils.openSession();
List<GoodsDTO> list = session.selectList("goods.selectGoodsDTO");
for (GoodsDTO g : list) {
System.out.println(g.getGoods().getTitle());
}
}catch (Exception e){
throw e;
}finally {
MyBatisUtils.closeSession(session);
}
}

3.准备工作

3.1 创建Category.java

package com.imooc.mybatis.entity;

public class Category {
private Integer categoryId;
private String categoryName;
private Integer parentId;
private Integer categoryLevel;
private Integer categoryOrder;

public Integer getCategoryId() {
return categoryId;
}

public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}

public String getCategoryName() {
return categoryName;
}

public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}

public Integer getParentId() {
return parentId;
}

public void setParentId(Integer parentId) {
this.parentId = parentId;
}

public Integer getCategoryLevel() {
return categoryLevel;
}

public void setCategoryLevel(Integer categoryLevel) {
this.categoryLevel = categoryLevel;
}

public Integer getCategoryOrder() {
return categoryOrder;
}

public void setCategoryOrder(Integer categoryOrder) {
this.categoryOrder = categoryOrder;
}
}