Mybatis之ResultMap(Mybatis复杂查询实现)

Mybatis能够将查询出来的结果生成对象返回。那么,如果对象中的属性名和数据库中的字段名不一致呢?还能成功映射成对象吗?肯定是不能的。因此ResultMap就是来解决这个问题的。ResultMap 的设计思想是,对于简单的语句根本不需要配置显式的结果映射,而对于复杂一点的语句只需要描述它们的关系就行了。
如下,数据库中密码字段为:psw,可是user对象中密码字段为:password,因此,用RusultMap映射:

<resultMap id="UserMap" type="User">
   <!-- id为主键 -->
   <id column="id" property="id"/>
   <!-- column是数据库表的列名 , property是对应实体类的属性名 -->
   <result column="name" property="name"/>
   <result column="pwd" property="password"/>
</resultMap>

<select id="selectUserById" resultMap="UserMap">
  select id , name , pwd from user where id = #{id}
</select>

多对一查询:
按查询嵌套处理:其实就是类似SQL子查询。
多个学生对应一个老师,association用来对照一个对象,也就是学生中包含老师的对象,然后在association中再嵌套一个select,类似子查询。

   <select id="getStudents" resultMap="StudentTeacher">
    select * from student
   </select>
   <resultMap id="StudentTeacher" type="Student">
       <!--association关联属性 property属性名 javaType属性类型 column在多的一方的表中的列名-->
       <association property="teacher"  column="tid" javaType="Teacher" select="getTeacher"/>
   </resultMap>
   <!--
   这里传递过来的id,只有一个属性的时候,下面可以写任何值
   association中column多参数配置:
       column="{key=value,key=value}"
       其实就是键值对的形式,key是传给下个sql的取值名称,value是片段一中sql查询的字段名。
   -->
   <select id="getTeacher" resultType="teacher">
      select * from teacher where id = #{id}
   </select>

按结果嵌套处理:
类似 SQL中的联表查询,相比上面的查询嵌套,按结果嵌套是SQL语句复杂一些,结构清楚一些,不需要两个查询了。在association中用JavaType写明类型,同时在下面又关联了teacher对象的属性,使得上面的student对象属性同时也可以获取teacher属性,然后用一个联合查询SQL查询出结果。

<select id="getStudents2" resultMap="StudentTeacher2" >
  select s.id sid, s.name sname , t.name tname
  from student s,teacher t
  where s.tid = t.id
</select>

<resultMap id="StudentTeacher2" type="Student">
   <id property="id" column="sid"/>
   <result property="name" column="sname"/>
   <!--关联对象property 关联对象在Student实体类中的属性-->
   <association property="teacher" javaType="Teacher">
       <result property="name" column="tname"/>
   </association>
</resultMap>

一对多查询:
一对多指的是一个老师对应多个学生,当老师的属性中包含学生的集合时,就是一对多。
按查询嵌套处理
一对多就是将上面association对应bean换成collection来对应一个集合,在集合中javaType对应集合类型:ArrayList,集合中的类型对应在ofType中。下面是类似子查询的处理方式,SQL语句简单,结构复杂一些。

<select id="getTeacher2" resultMap="TeacherStudent2">
select * from teacher where id = #{id}
</select>
<resultMap id="TeacherStudent2" type="Teacher">
   <!--column是一对多的外键 , 写的是一的主键的列名-->
   <collection property="students" javaType="ArrayList" ofType="Student" column="id" select="getStudentByTeacherId"/>
</resultMap>
<select id="getStudentByTeacherId" resultType="Student">
  select * from student where tid = #{id}
</select>

按结果嵌套处理:
类似联合查询

 <select id="getTeacher" resultMap="TeacherStudent">
      select s.id sid, s.name sname , t.name tname, t.id tid
      from student s,teacher t
      where s.tid = t.id and t.id=#{id}
   </select>

   <resultMap id="TeacherStudent" type="Teacher">
       <result  property="name" column="tname"/>
       <collection property="students" ofType="Student">
           <result property="id" column="sid" />
           <result property="name" column="sname" />
           <result property="tid" column="tid" />
       </collection>
   </resultMap>

小结
关联-association
集合-collection
所以association是用于一对一和多对一,而collection是用于一对多的关系。

JavaType和ofType都是用来指定对象类型的
JavaType是用来指定pojo中属性的类型,可以是集合List
ofType指定的是映射到list集合属性中pojo的类型。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值