zoukankan      html  css  js  c++  java
  • MyBatis联合查询和使用association 进行分步式查询

    查询Emp的同时,查出emp对应的部门Department

    方法1:联合查询,使用级联属性封装结果集

    <!--

    联合查询,使用级联属性封装结果集

    type:要自定义规则的javaBean类型

      id:唯一标识,方便引用

      column:指的是数据库表中的列名

      property:列名对应的javaBean中的属性名称

    -->

    <resultMap type="cn.bdqn.mybatis.been.Emp" id="myDifemp">

    <id column="id" property="id" />

    <result column="last_name" property="last_name" />

    <result column="gender" property="gender" />

    <result column="did_id" property="dept.id" />

    <result column="dept_name" property="dept.departmentName" />

    </resultMap>

    <!--public Emp getEmpAndDept(Integer id); -->

    <select id="getEmpAndDept" resultMap="myDifEmp2">

    SELECT e.id AS id,e.last_name AS last_name,e.gender AS gender,e.email AS

    email,d.id AS d_id,d.`dept_name` AS dept_name

    FROM emp e,dept d WHERE e.`d_id`=d.id and e.id=#{id}

    </select>

    方法2:association来实现多表查询

    <!--

    association来实现多表查询

    association标签的property属性来指定javaBean中的哪个属性是我们自定义的(:哪个属性是联合的对象),

    javaType是用来指定这个属性的类型(不能省略)

    association标签中继续使用idresult标签来封装相应的封装规则

    -->

    <resultMap type="cn.bdqn.mybatis.been.Emp" id="myDifEmp2">

    <id column="id" property="id" />

    <result column="last_name" property="last_name" />

    <result column="gender" property="gender" />

    <result column="email" property="email"/>

    <association property="dept"  javaType="cn.bdqn.mybatis.been.Department">

    <id column="did_id"  property="id"/>

    <result column="dept_name" property="departmentName"/>

    </association>

    </resultMap>

     

    方法3:association进行分步式多表查询

    01.先按照员工id查询员工信息

    02.根据查出员工信息中的d_id值去部门表查询部门信息

    03.把查出的部门设置到员工中;

    创建DepartmentjavaBean,编写对应的映射文件,

    DepartmentMapper.xml

    <?xml version="1.0" encoding="UTF-8" ?>

    <!DOCTYPE mapper

    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

    <!--namespace与接口进行绑定 -->

    <mapper namespace="cn.bdqn.mybatis.dao.DeptMapper">

    <!--public Department getDepartmentById(Integer id); -->

    <select id= "getDepartmentById" resultType="cn.bdqn.mybatis.been.Department">

    <!--我们数据表中部门名称的字段的名字是dept_name 而我们javaBean中对应的 名字是departmentName,不一致,所以我们要将查询的列一一写出,并且将名称不相同的,起别名 -->

    select id,dept_name as departmentName from dept where id=#{id}

    </select>

    </mapper>

    编写emp对应的映射文件

    <?xml version="1.0" encoding="UTF-8" ?>

    <!DOCTYPE mapper

    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

    <!--namespace:名称空间 -->

    <mapper namespace="cn.bdqn.mybatis.dao.EmpMapperPlus">

    <!-- association来实现多表查询 association标签的property属性来指定javaBean中的哪个属性是我们自定义的(:哪个属性是联合的对象),

    javaType是用来指定这个属性的类型(不能省略) association标签中继续使用idresult标签来封装相应的封装规则 -->

    <resultMap type="cn.bdqn.mybatis.been.Emp" id="myDifEmp2">

    <id column="id" property="id" />

    <result column="last_name" property="last_name" />

    <result column="gender" property="gender" />

    <result column="email" property="email" />

    <association property="dept" javaType="cn.bdqn.mybatis.been.Department">

    <id column="did_id" property="id" />

    <result column="dept_name" property="departmentName" />

    </association>

    </resultMap>

    <!--

    使用association进行分步式查询

     1.先按照员工id查询员工信息

     2.根据查出员工信息中的d_id值去部门表查询部门信息

     3.把查出的部门设置到员工中;

     -->

    <resultMap type="cn.bdqn.mybatis.been.Emp" id="myEmpBy">

    <id column="id" property="id" />

    <result column="last_name" property="last_name" />

    <result column="gender" property="gender" />

    <result column="email" property="email" />

    <!--association定义关联对象的封装规则 select:select的属性值是部门的映射文件的名称空间+对应的方法名称 表明当前属性是调用select指定的方法查出来的结果

    column:指定将哪一列的值传给这个方法 流程:使用select指定的方法(传入column指定的这列参数的值)查出对象,并封装给property指定的属性 -->

    <association property="dept"

    select="cn.bdqn.mybatis.dao.DeptMapper.getDepartmentById" column="d_id">

    </association>

    </resultMap>

    <!-- public Emp getEmpByIdStep(Integer id); -->

    <select id="getEmpByIdStep" resultMap="myEmpBy">

    select * from emp where

    id=#{id}

    </select>

    </mapper>

     

    注意:这两个映射文件写好后,必须都要在全局配置文件mybatis-config.xml中进行注册

    <mappers>

    <mapper resource="EmpMapperPlus.xml" />

    <mapper resource="DepartmentMapper.xml"/>

    </mappers>

    分步查询,可以使用延迟加载(懒加载/按需加载)

    emp==>Dept

    使用联合查询和其他复杂查询时,emp的信息和Dept的信息都会同时查出,这样很浪费资源

    而我们希望部门信息在我们使用的时候再去查询,这样就很节省数据库了

    只需要在分段查询的基础之上加上两个配置:

    只需要在全局配置文件中开启懒加载模式

    <setting name="lazyLoadingEnabled" value="true"/>

    和按需加载

    <setting name="aggressiveLazyLoading" value="false"/>

    便可以实现

  • 相关阅读:
    Ajax实现动态的二级级联菜单
    使用 Struts 2 实现国际化
    使用 Struts2 校验器校验用户注册信息
    Python基础 9.正则
    Python基础 8.迭代器 生成器 包
    Python基础 7.异常
    Python基础 6 .文件操作
    Python基础 5.类
    Python基础 4.函数
    Python基础 3 控制流程
  • 原文地址:https://www.cnblogs.com/zqr99/p/7594042.html
Copyright © 2011-2022 走看看