您的位置:首页 > 其它

mybatis-resultMap使用与详解

2013-09-13 16:39 609 查看
1,当数据库的字段名与属性名称不一致时,在mybatis中如何处理?

第一种方式: 采用投影对字段重命名
<select id="load" parameterType="int" resultMap="addressMap">
select * user_id as userId  from t_address where id=#{id}
</select>


第二种方式: 使用resultMap

<resultMap type="Address" id="addressMap">
<!-- id是用来对主键映射的 -->
<!-- result是用来对一般属性映射的 -->
<!-- 只需要对不一致的字段进行映射,其它的没有必要,  id一致是,可以不映射 -->
<id column="id" property="id"/>
<result column="postcode" property="postcode"/>
</resultMap>
<select id="load" parameterType="int" resultMap="addressMap">
select *  from t_address where id=#{id}
</select>


2,resultMap中除了前面的id result两个属性之外,还有很多有趣的属性,

2.1 association取关联对象 (外键关联)

第一种方式 会发N+1条sql,不可取

<resultMap type="Address" id="addressMap">
<!-- association 用来作关联   property属性:要关联的对象名称  column属性:用哪一列进行关联
javaType属性: 要关联的对象类型  select属性:执行的sqlId
-->
<!-- association最大的问题就是取关联对象时,会发N条sql 因此以下取关联对象的方式不会使用的 -->
<association property="user" column="user_id" javaType="User"
select="com.yangwei.shop.entity.User.load" />
</resultMap>

<select id="load" parameterType="int" resultMap="addressMap">
select *  from t_address where id=#{id}
</select>


第二种方式  只会发1条sql ,请使用这种
  注意sql的写法
<resultMap type="Address" id="addressMap" autoMapping="true">
<!-- 设置了autoMapping之后,自己对象的属性会完成自动映射 -->
<id column="a_id" property="id" />
     <-- 取关联对象-->

<association property="user" javaType="User">
<!-- 这里面的字段都是user表中的,全部必须手动映射,没有映射的将是null -->
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="passwd" property="passwd"/>
<result column="nickname" property="nickname"/>
<result column="type" property="type"/>
</association>
</resultMap>
<select id="load" parameterType="int" resultMap="addressMap">
select *,t1.id as a_id  from t_address t1 right join t_user t2 on(t1.user_id=t2.id) where t1.id=#{id}
</select>


第三种方式: 与第二种差不多,只是将结果resultMap单独出来
<resultMap type="Address" id="addressMap" autoMapping="true">
<!-- 设置了autoMapping之后,自己对象的属性会完成自动映射 -->
<id column="a_id" property="id" />
<association property="user" javaType="User" resultMap="userMap" />
</resultMap>

<resultMap type="User" id="userMap">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="passwd" property="passwd"/>
<result column="nickname" property="nickname"/>
<result column="type" property="type"/>
</resultMap>

<select id="load" parameterType="int" resultMap="addressMap">
select *,t1.id as a_id  from t_address t1 right join t_user t2 on(t1.user_id=t2.id) where t1.id=#{id}
</select>


2.2 collection 获取对象中关联的集合数据

<resultMap type="User" id="userMap" autoMapping="true">
<!--User类中有 List<Address> address属性;-->
<!--column写不写都无所谓-->
<!--ofType必须设置 List里面的类型-->
      
    <id column="user_id" property="id"></id> <!--下面的查询会有两个id,需要指明用哪一个映射-->

<collection property="address" column="user_id" ofType="Address">
<id column="a_id" property="id" />
<result column="name" property="name"/>
<result column="postcode" property="postcode"/>
<result column="detail" property="detail"/>
<result column="phone" property="phone"/>
</collection>
</resultMap>
<select id="load" parameterType="int" resultMap="userMap">
select *,t2.id as a_id from t_user t1 left join t_address t2 on(t1.id=t2.user_id) where t1.id=#{id}
</select>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: