MyBatis框架之关联查询

概述:关联查询主要在<resultMap>元素中,用<association>配置一对一、用<collection> 配置一对多

一、一对一查询

      1.使用扩展类实现一对一查询

<select id="queryById" parameter="int" resultType="User">
    select  * from  user where userid=#{userid}
</select>

      2.使用resulutMap实现一对一查询(association)

              举例说明:将用户信息和用户权限放到不同的实体类中,每一个用户对应着相应的权限,如果要根据用户id查询他对应的属性,则需要在用户信息加入一个权限的属性,再通过SQL映          射文件中的resultMap属性,将查询的结果映射到用户类中的所有属性,包括权限信息     

<select id="queryById" parameter="int" resultMap="User_privilige_map">
    select  * 
from user
inner join privilige on user.privilige=privilige.privilige
where userid
=#{userid} </select> <resultMap type="org.zy.pojo.user" id="User_privilige_map"> <id property="userid" column="userid"/> <result property="userName" column="userName"/> <result property="userAge" column="userAge"/> ... <association property="privilige" javaType="privilige"> <id property="priviligeid" column="priviligeid"/> <result property="priviligeidName"column="priviligeidName"/> ... </association> </resultMap>

二、一对多查询

        本章利用班级和学生之间的一对多举例:

         (1)首先在班级类中添加学生属性     privaie  List<Student> student;

         (2)在学生类中增加表示班级的外键

         (3)查询语句

            

<select id="queryById" parameter="int" resultMap="ClassAndStudent">
    select  * from  Student s where  inner join Class c on s.classid=c.classid  where
    s.id=#{userid}
</select>


<resultMap type="studentClass" id="ClassAndStudent">
      <id  property="classid" column="classid"/>
      <result  property="className" column="className"/>
      ... //省略其他属性
<collection property="students" ofType="student">
<id peoperty="stuNo" column="stuNo"/>
<result property="stuName" column="stuName" />
...//省略其他属性
</collection>
</resultMap>

参数说明:普通的类型通过id,result 映射,List属性的students 通过<collection>映射,并通过ofType指定List元素中的类型,即List类型的属性,需要通过<resultMap>中的<collection>
元素来映射到数据表中的各个列

 

 

 

三、多对一查询

       一个班级和多个学生之间的关系是一对多,反过来看,多个学生和班级之间的关系是多对一,本质是一样的,参照上面的内容即可

四、多对多查询

       多对多的本质上就是两个或多个一对多

五、延迟加载

             说明:在使用一对多查询时,很多时候只需要前面的信息,不需要后面的关联信息,即首次查询的是主要的班级信息,关联的学生信息在需要的时候再加载,

         以减少不必要的数据库开销从而提升程序的效率,这就称为延迟加载

              1.一对多延迟加载使用步骤

                     (1)打开Mybatis配置文件的延迟加载的开关

                        

<configuration>
    <properties resource="db.properties">
    <settings>
          <--将延迟加载设置为ture ,可省略,默认为true-->
          <setting name="LazyLoadingEnabled" value="true"/>
          <--将立即加载设置为false -->
          <setting name="aggressiveLazyLoading" value="false"/>
    </settings>
</configuration>

 

                  (2)SQL映射文件

                       

<select id="queryStudentLazyLoad" parameter="int" resultMap="ClassAndStudent">
    select  * from  class 
</select>

<resultMap type="studentClass" id="ClassAndStudent">
      <id  property="classid" column="classid"/>
      <result  property="className" column="className"/>
      ... //省略其他属性
<collection property="students" ofType="student"
<--通过命名空间+id指定延迟加载执行sql语句-->
select="org.zy.mapper.StudentMapper.queryStudentByClassId" column="classid"/>
</collection>
</resultMap>
StudentMapper.xml配置文件

<mapper namespace="org.zy.mapper.StudentMapper">
//根据班级号码查询学生的信息
<select id="queryStudentByClassId" parmeterType="int" resultType="student">
select * from student where classid=#{classid}
</select>

                  (3)说明:即通过主查询查询班级信息,然后通过一对多元素映射关联student表,并通过select语句属性指定延迟加载的sql语句 

                select * from student where classid=#{classid}
(4)修改Mybatis配置文件
<mappers>
     <mapper resource="org/zy/mapper/studentMapper.xml"/>
                <mapper resource="org/zy/mapper/studentMapper.xml"/>
         <mappers>

 

转载于:https://www.cnblogs.com/pamne/p/11275601.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值