MyBatis映射文件resultMap元素中使用多个association的方法
现在有一张订单表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语句。
到此这篇关于MyBatis映射文件resultMap元素中使用多个association的方法的文章就介绍到这了,更多相关MyBatis 多个association内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
spring boot idea maven依赖找不到问题处理方法
这篇文章主要介绍了spring boot idea 偶尔maven依赖找不到问题,这里总结了几种处理方法,方便尝试排查,对spring boot idea maven依赖找不到问题感兴趣的朋友跟随小编一起看看吧2023-08-08Spring中@Repository注解的作用和用法以及和@Mapper的区别详析
这篇文章主要给大家介绍了关于Spring中@Repository注解的作用和用法以及和@Mapper的区别的相关资料,注解的作用是标识一个类为数据访问对象,并由Spring框架进行实例化和管理,需要的朋友可以参考下2023-09-09IDEA 重新导入依赖maven 命令 reimport的方法
这篇文章主要介绍了IDEA 重新导入依赖maven 命令 reimport的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-04-04基于idea 的 Java中的get/set方法之优雅的写法
这篇文章主要介绍了基于idea 的 Java中的get/set方法之优雅的写法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2019-01-01J2EE Servlet基础在浏览器上运行HelloServlet的方法
这篇文章主要介绍了J2EE Servlet基础在浏览器上运行HelloServlet的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-10-10
最新评论