博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Mybatis--多表联查
阅读量:4159 次
发布时间:2019-05-26

本文共 2766 字,大约阅读时间需要 9 分钟。

    1. 实体类中包含对象的普通写法:

Employee和Department都是实体类

<resultMap type="Employee" id="result1">

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

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

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

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

<!-- 1.联合查询(使用对象进行关联) -->

<!-- <result column="dept_id" property="dept.id"/>

 <result column="dname"   property="dept.dname"/> -->

<!-- 2.关联查询,可以关联一个独立的对象 -->

<association property="dept"

javaType="com.hwua.entity.Department">

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

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

</association>

</resultMap>

<select id="findById" parameterType="Long" resultMap="result1">

select e.id,e.last_name,e.email,e.gender,e.dept_id,d.dname

from t_emps e,t_dept d where e.dept_id=d.id and e.id=#{id}

</select>

    1. 实体类中包含对象的分步写法

<!-- 分步查询 -->

<resultMap type="Employee" id="result2">

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

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

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

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

<!-- 3.使用association实现分段查询(分步),将一条sql拆分

property 是pojo中定义的以类为属性的名称:private Department dept;

select可以调用一个接口的方法

column 表示入参,这个接口需要传入的参数 -->

<association property="dept"

select="com.hwua.dao.DeptMapper.findById" 

column="dept_id">

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

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

</association>

</resultMap>

<select id="findByIdStep" parameterType="Long"

resultMap="result2">

select e.id,e.last_name,e.email,e.gender,e.dept_id from

t_emps e where e.id=#{id}

</select>

    1. 实体类中包含集合的普通写法

<resultMap type="com.hwua.entity.Department" id="result1">

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

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

<!-- 员工信息 collection:封装集合对象

ofType:专门封装集合对象时,用来指定对象类型,不能用javaType -->

<collection property="emps"

ofType="com.hwua.entity.Employee">

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

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

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

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

</collection>

</resultMap>

<select id="findFullById" parameterType="Long"

resultMap="result1">

select d.id,dname,e.id eid,e.last_name,e.email,e.gender

from t_dept d LEFT JOIN t_emps e

on e.dept_id=d.id

where d.id=#{1}

</select>

    1. 实体类中包含集合的分步写法

<!-- 分步查询 -->

<resultMap type="com.hwua.entity.Department" id="result2">

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

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

<collection property="emps"

select="com.hwua.dao.EmpMapper.findByDeptId" column="id">

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

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

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

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

</collection>

</resultMap>

<select id="findFullByIdStep" parameterType="Long"

resultMap="result2">

select d.id,dname from t_dept d where d.id=#{1}

</select>

转载地址:http://gejxi.baihongyu.com/

你可能感兴趣的文章
java SE面向对象思维导图
查看>>
数据结构常见排序
查看>>
人工智能GIS技术升级,将应用到更多领域
查看>>
厕所地图,玩出新花样!
查看>>
时空大数据技术驱动城市交通智能化,重庆这样做
查看>>
springboot中配置supermap objects java组件
查看>>
supermap idesktop产品postgis数据源支持地方坐标系
查看>>
iPortal配置HTTPS加密协议
查看>>
浅谈SuperMap iDesktopX桌面扩展开发
查看>>
WebGL之飞行管理
查看>>
postgis数据源一库多平台共用
查看>>
SuperMap产品中栅格数据更新还能这样用
查看>>
iMobile中加载大数据量的矢量数据性能优化方法有哪些
查看>>
SueperMap iMobile for android中在线数据对接大全
查看>>
SuperMap iClient3D for WebGL之BIM模型爆炸
查看>>
SuperMap iClient3D for WebGL之点聚合
查看>>
精模缓存更新方案
查看>>
SuperMap iObjects C++之SQL查询和空间查询
查看>>
SuperMap iObjects C++之记录集管理
查看>>
SuperMap iObjects C++之数据集管理
查看>>