mybatis-<resultMap><association></association></resultMap>-多对一
1.对应的Entity
1 private Long shopId; 2 private PersonInfo owner; 3 private ShopCategory shopCategory; 4 private Area area; 5 private String shopName; 6 private String shopDesc; 7 private String shopAddr; 8 private String phone; 9 private String shopImg; 10 private Integer priority; 11 private Date createTime; 12 private Date lastEditTime; 13 //-1不可用 ,0 审核组 1可用 14 private Integer enableStatus; 15 //超级管理员给店家的提醒 16 private String advice;
2.对应Entity的Mapper
column是数据库中的字段,property是Entity的定义名
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="com.gzhli.o2o.dao.ShopDao"> 6 <resultMap type="com.gzhli.o2o.entity.Shop" id="shopMap"> 7 <id column="shop_id" property="shopId"/> 8 <result column="shop_name" property="shopName"/> 9 <result column="shop_desc" property="shopDesc"/> 10 <result column="shop_addr" property="shopAddr"/> 11 <result column="phone" property="phone"/> 12 <result column="shop_img" property="shopImg"/> 13 <result column="priority" property="priority"/> 14 <result column="create_time" property="createTime"/> 15 <result column="last_edit_time" property="lastEditTime"/> 16 <result column="enable_status" property="enableStatus"/> 17 <result column="advice" property="advice"/> 18 <association property="area" column="area_id" javaType="com.gzhli.o2o.entity.Area"> 19 <id column="area_id" property="areaId"/> 20 <result column="area_name" property="areaName"/> 21 </association> 22 <association property="shopCategory" column="shop_category_id" javaType="com.gzhli.o2o.entity.ShopCategory"> 23 <id column="shop_category_id" property="shopCategoryId"/> 24 <result column="shop_category_name" property="shopCategoryName"/> 25 </association> 26 <association property="owner" column="userId" javaType="com.gzhli.o2o.entity.PersonInfo"> 27 <id column="user_id" property="userId"/> 28 <result column="name" property="name"/> 29 </association> 30 </resultMap> 31 63 <select id="queryByShopId" parameterType="Long" resultMap="shopMap"> 64 SELECT 65 s.shop_id , 66 a.area_id, 67 a.area_name , 68 sc.shop_category_id , 69 sc.shop_category_name, 70 s.SHOP_NAME , 71 s.SHoP_DESC , 72 s.SHOP_ADDr , 73 s.phone , 74 s.shop_imG , 75 s.prioRITY , 76 s.cREate_time , 77 s.LASt_edit_time , 78 s.enabLE_STAtUS , 79 s.ADVICe 80 FROM 81 TB_SHOP S, 82 tb_shop_category SC, 83 tb_area A 84 WHERE 85 S.AREA_ID = A.area_id 86 AND S.shop_category_id = SC.shop_category_id 87 AND shop_id = #{shopId} 88 </select> 89 </mapper>