mybatis-resultMap使用与详解
2013-09-13 16:39
609 查看
1,当数据库的字段名与属性名称不一致时,在mybatis中如何处理?
2,resultMap中除了前面的id result两个属性之外,还有很多有趣的属性,
2.1 association取关联对象 (外键关联)
2.2 collection 获取对象中关联的集合数据
第一种方式: 采用投影对字段重命名 <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>
相关文章推荐
- Mybatis的ResultMap的使用
- Mybatis映射返回resultMap详解
- MyBatis框架核心之(五)注解使用resultMap及多表查询
- mybatis进阶(3)--一对一查询(使用resultMap)
- mybatis--resultMap的insert使用
- Struts2和MyBatis的整合和ResultMap的使用
- Mybatis初窥:使用resultMap自定义高级映射规则
- Mybatis的ResultMap的使用
- mybatis使用resultMap实现多对多查询 (需求:商品信息和订单明细有多对多的关系)
- 我的试验__mybatis中resultType使用javaBean和Map的区别
- 知了堂学习笔记--mybatis学习_resultMap的使用
- Mybatis笔记四:Mybatis中的resultType和resultMap查询操作实例详解
- Mybatis学习记录(四)----resultMap的使用
- JavaWeb学习笔记-mybatis-19-一对一使用resultMap
- MyBatis 3.3.1 在resultMap中使用association但是没有指定id时的结果集bug
- mybatis中resultType与resultMap使用经验
- Mybatis---查询使用resultType和resultMap的区别
- Mybatis使用ResultType实现一对一关系详解(二)
- 详解MyBatis的getMapper()接口、resultMap标签、Alias别名、 尽量提取sql列、动态操作