mybitis分步查询延迟加载和resultMap几种常用方法

本文详细介绍了MyBatis中的resultMap的两种常用查询方法,包括如何映射单个对象和联合对象。通过示例展示了如何进行分步查询,包括单条查询和集合查询,并讲解了如何配置懒加载和按需加载。同时,还演示了如何在XML映射文件中定义resultMap来处理一对多的关系,以及如何通过select属性进行分步查询。
摘要由CSDN通过智能技术生成

举例说明 有两张表    学生和成绩  students 和 scores

忽略Java对象  z注意  students对象中有个属性是scores对象

 

resultMap 常用的查询方法一:

查询标签需要注意 返回类型是resultMap   这里Mymap是我们定义的resultMap的id

<select id="" resultMap="Mymap1"></select>

定义resultMap

        column:指定哪一列
        property:指定对应的javaBean属性

<resultMap type="students" id="Mymap1">
        <id column="id" property="id"/>
        <result column="User_Name" property="userName"/>
        <result column="gender" property="gender"/>

        <result column="age" property="age"/>
        <result column="scores_id" property="scores.id"/>
        <result column="chinese" property="scores.chinese"/>

        <result column="mathematics" property="scores.mathematics"/>

        <result column="english" property="scores.english"/>
    </resultMap>

resultMap 常用的查询方法二:

<select id="" resultMap="Mymap2"></select>

<resultMap type="students" id="Mymap2">
        <id column="id" property="id"/>
        <result column="User_Name" property="userName"/>
        <result column="gender" property="gender"/>

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

<!--  association可以指定联合的javaBean对象
        property="":指定哪个属性是联合的对象
        javaType:指定这个属性对象的类型[不能省略]
        -->     
 <association property="scores" javaType="全路径scores">
            <id column="scores_id" property="id"/>
            

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

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

        <result column="english" property="english"/>
        </association>     
    </resultMap>

 

分步查询  单条查询

分步查询首先设置mybits的两个属性

开启懒加载模式

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

和按需加载

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

select属性填写xml的namespace和那个select标签的id

column查询传入的值

<association property="xxx" 
             select="xxx.ccc"
             column="id">
         </association>

分步查询  集合查询

<resultMap type="com.luohp.mybatis.bean.Deptno" id="result_list" extends="result_map">

<!--

    collection:嵌套结果集方式,返回一个集合

    ofType:指定集合元素的类型

-->

    <collection property="empList" ofType="Employee">

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

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

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

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

    </collection>

</resultMap>

<select id="findDeptAndEmpList" resultMap="result_list">

    select

    d.id,d.dept_name ,e.id eid,e.last_name lastName,e.gender gender,e.email

  from

    tbl_deptno d left join tbl_employee e on d.id=e.dept_id

  where

    d.id=#{id}

</select>

 

 

 

<resultMap type="com.luohp.mybatis.bean.Deptno" id="result_list_setp" extends="result_map">

<!--

    collection:嵌套结果集方式,返回一个集合

    select:分步查询

    column:将id列的值作为参数传给EmployeeMapper接口的findByDeptId方法,然后将返回的员工集合,赋值给empList

-->

    <collection property="empList"

        column="id" select="com.luohp.mybatis.dao.EmployeeMapper.findByDeptId">

    </collection>

</resultMap>

 

<select id="findDeptAndEmpList2" resultMap="result_list_setp">

select d.id id,d.dept_name from tbl_deptno d where d.id=#{id}

</select>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值