0
点赞
收藏
分享

微信扫一扫

MyBatis的查询(自定义查询、分步查询)

1、自定义查询

1.1 对象(association)

<!-- 
    <resultMap>:自定义映射,处理复杂的表关系
    
    <id column="eid" property="eid"/>
    <id>:设置主键的映射关系,column设置字段名,property设置属性名
    
    <result column="ename" property="ename"/>
    <result>:设置非主键的映射关系,column设置字段名,property设置属性名
-->
<resultMap type="Emp" id="empMap">
    <id column="eid" property="eid"/>
    <result column="ename" property="ename"/>
    <result column="age" property="age"/>
    <result column="sex" property="sex"/>
    <!-- javaType:必须写明类型 -->
    <association property="dept" javaType="Dept">
        <id column="did" property="did"/>
        <result column="dname" property="dname"/>
    </association>
</resultMap>


<!-- List<Emp> getAllEmp(); -->
<select id="getAllEmp" resultMap="empMap">
    select e.eid,e.ename,e.age,e.sex,e.did,d.dname from emp e left join dept d on e.did = d.did
</select>

1.2 集合(collection)

<resultMap type="Dept" id="deptMap">
    <id column="did" property="did"/>
    <result column="dname" property="dname"/>
    <!--
         <collection>:处理一对多和多对多的关系
         ofType:指集合中的类型,不需要指定javaType
     -->
    <collection property="emps" ofType="Emp">
        <id column="eid" property="eid"/>
        <result column="ename" property="ename"/>
        <result column="age" property="age"/>
        <result column="sex" property="sex"/>
    </collection>
</resultMap>


<!-- Dept getDeptEmpsByDid(String did); -->
<select id="getDeptEmpsByDid" resultMap="deptMap">
    select d.did,d.dname,e.eid,e.ename,e.age,e.sex from dept d left join emp e on 			d.did = e.did where d.did = #{did}
</select>


2、分步查询

2.1 对象(association)

<!-- 
    <resultMap>:自定义映射,处理复杂的表关系
-->
<resultMap type="Emp" id="empMapStep">
    <id column="eid" property="eid"/>
    <result column="ename" property="ename"/>
    <result column="age" property="age"/>
    <result column="sex" property="sex"/>
    <!-- 
        select: 分步查询的SQL的id,即接口的全限定名.方法名或namespace.SQL的id
        column: 分步查询的条件。(注意:此条件必须是从数据库查询过得)
        fetchType: 设置是否延时加载, lazy  eager
	-->
    <association property="dept" select="com.example.mapper.DeptMapper.getDeptByDid" column="did"/>
</resultMap>


<!-- Emp getEmpStep(String eid); -->
<select id="getEmpStep" resultMap="empMapStep">
    select eid,ename,age,sex,did from emp where eid = #{eid}
</select>

2.2 集合(collection)

<!--
    fetchType="eager",表示不延时加载,默认值为lazy
-->
<resultMap type="Dept" id="deptMapStep">
    <id column="did" property="did"/>
    <result column="dname" property="dname"/>
    <collection property="emps" select="com.example.mapper.EmpDeptMapper.getEmpListByDid" column="{did=did}" fetchType="eager"></collection>
</resultMap>


<!-- Dept getOnlyDeptByDid(String did); -->
<select id="getOnlyDeptByDid" resultMap="deptMapStep">
    select did, dname from dept where did = #{did}
</select>

<!-- List<Emp> getEmpListByDid(String did); -->
<select id="getEmpListByDid" resultType="com.example.entity.Emp">
    ...省略(根据 getOnlyDeptByDid 中 sql 查询出来的 did 查询集合)
</select>


MyBatis的查询(自定义查询、分步查询)_分步查询

举报

相关推荐

0 条评论