MyBatis映射文件resultMap元素中使用多个association的方法
作者:Lw中 时间:2023-11-29 06:53:51
现在有一张订单表t_stockorder,其拥有id、code、client_id、merchandise_id、merchandise_number、order_date、operator_id这些字段,其中client_id关联t_client表中code字段,merchandise_id关联t_merchandise表的code字段,operator_id关联t_employee表的code字段。
现在要通过SQL语句将订单表中t_stockorder的数据全部查询出来,SQL语句如下所示:
select so.id, so.code, c.name cname, m.name mname,
so.merchandise_number,
so.order_date, e.name ename
from
inventory.t_stockorder so
inner join
inventory.t_client c on c.code = so.client_id
inner join
inventory.t_merchandise m on m.code = so.merchandise_id
inner join
inventory.t_employee e on e.code = so.operator_id
现在要在mapper映射文件中添加动态Sql语句,一般情况下映射文件中的resultMap元素中只可以有一个association,那如何添加多个association到resultMap中呢?正确代码如下图所示:
<resultMap id="StockorderMap" type="com.lwz.entity.Stockorder">
<id property="id" column="id" />
<result property="code" column="code" />
<result property="merchandiseNumber" column="merchandise_number" />
<result property="orderDate" column="order_date" />
<association property="client" javaType="Client" resultMap="ClientResultMap"></association>
<association property="merchandise" javaType="Merchandise" resultMap="MerchandiseResultMap"></association>
<association property="employee" javaType="Employee" resultMap="EmployeeResultMap"></association>
</resultMap>
<resultMap id="ClientResultMap" type="com.lwz.entity.Client">
<id property="code" column="code" />
<result property="name" column="cname" />
</resultMap>
<resultMap id="MerchandiseResultMap" type="com.lwz.entity.Merchandise">
<id property="code" column="code" />
<result property="name" column="mname" />
</resultMap>
<resultMap id="EmployeeResultMap" type="com.lwz.entity.Employee">
<id property="code" column="code" />
<result property="name" column="ename" />
</resultMap>
<!--通过实体作为筛选条件查询-->
<select id="queryAll" resultMap="StockorderMap">
select so.id, so.code, c.name cname, m.name mname,
so.merchandise_number,
so.order_date, e.name ename
from
inventory.t_stockorder so
inner join
inventory.t_client c on c.code = so.client_id
inner join
inventory.t_merchandise m on m.code = so.merchandise_id
inner join
inventory.t_employee e on e.code = so.operator_id
<where>
<if test="id != null">
and id = #{id}
</if>
<if test="code != null and code != ''">
and so.code = #[code]
</if>
<if test="client != null">
and client_id = #{client.code}
</if>
<if test="merchandise != null">
and merchandise_id = #{merchandise.code}
</if>
<if test="merchandiseNumber != null">
and merchandise_number = #{merchandiseNumber}
</if>
<if test="orderDate != null">
and order_date = #{orderDate}
</if>
<if test="employee != null">
and operator_id = #{employee.code}
</if>
</where>
</select>
resultMap中association的各个属性的含义:
property:映射实体类的字段或属性。
colum:数据库的列名或者列标签别名。
javaTyp:完整java类名或别名。
jdbcType支持的JDBC类型列表列出的JDBC类型。这个属性只在insert,update或delete的时候针对允许空的列有用。
resultMap: 一个可以映射联合嵌套结果集到一个适合的对象视图上的ResultMap。这是一个替代的方式去调用另一个select语句。
来源:https://blog.csdn.net/weixin_43894879/article/details/106770531
标签:MyBatis,association
0
投稿
猜你喜欢
Android模拟器对应的电脑快捷键说明
2022-11-09 17:44:07
解决Java中socket使用getInputStream()阻塞问题
2022-06-16 14:33:14
Java日志框架之logback使用详解
2022-02-18 05:33:47
C#时间格式转换为时间戳的方法步骤
2022-04-13 12:27:19
java自带的MessageDigest实现文本的md5加密算法
2023-10-08 03:35:29
IDEA+JRebel实现全自动热部署的方法步骤
2023-11-29 13:53:30
Android提高之MediaPlayer播放网络视频的实现方法
2021-07-03 06:25:29
java数据结构与算法之noDups去除重复项算法示例
2023-06-19 08:50:34
Android点击事件之多点触摸与手势识别的实现
2022-10-12 05:12:28
使用newInstance()来实例化fragment并传递数据操作
2022-07-11 15:41:57
Java中继承thread类与实现Runnable接口的比较
2022-06-09 12:57:09
Java基础之堆内存溢出的解决
2023-11-10 20:06:30
C#中的协变与逆变小结
2022-10-25 13:30:25
Spring项目中使用Junit单元测试并配置数据源的操作
2022-06-02 05:32:27
Flutter 给列表增加下拉刷新和上滑加载更多功能
2021-10-01 13:30:18
C# 使用PictureBox实现图片按钮控件的示例步骤
2023-04-04 21:33:29
C#利用原图和水印图的重叠简单实现水印的方法
2023-04-21 01:41:24
如何用IDEA调试BUG的几种方法
2022-08-04 17:12:46
基于TCP通信丢包原因总结(推荐)
2022-04-27 04:37:28
Eclipse下Javassist正确使用方法代码解析
2021-07-24 18:43:57