实体对象里定义的集合图片
private List<CompanyImagesModel>
companyImages;
一、
1、定义 resultMap :
<!-- 图片 -->
<resultMap id="
BaseResultMap_images" type="com.healthengine.model.CompanyModel" extends="BaseResultMap" >
<!-- 添加一对多的关联对象 -->
<collection property="
companyImages" column="companyImagesId" ofType="com.healthengine.model.CompanyImagesModel">
<id column="company_images_id" property="companyImagesId" jdbcType="BIGINT" />
<result column="company_id" property="companyId" jdbcType="BIGINT" />
<result column="type" property="type" jdbcType="INTEGER" />
<result column="image_path" property="imagePath" jdbcType="VARCHAR" />
</collection>
</resultMap>
2.sql查询
<!-- 查看公司信息关联图片 -->
<select id="selectModelAndImagesByPrimaryKey" resultMap="
BaseResultMap_images" parameterType="java.lang.Long" >
select
<include refid="Base_Column_List_images" />,
img.company_images_id, img.company_id, img.image_path, img.type,
SUBSTRING(city.merger_name,4) as city_name,city.parent_id as city_parentId
from tb_company c
left join tb_company_images img on c.company_id = img.company_id and img.flag=1
LEFT JOIN tb_city city on c.city_id = city.city_id
where c.flag=1 and c.company_id = #{companyId,jdbcType=BIGINT}
</select>
这种方法不适合分页查询,适合单一查询
二、
<!-- 关联图片表 -->
private List<CompanyImagesModel> picture;
1、定义 resultMap :
<resultMap id="
BaseResultMap_leftJoin_images" type="com.healthengine.model.CompanyModel" extends="BaseResultMap" >
<collection property="picture" column="{
companyId=company_id,
picture_view_path_pre=picture_view_path_pre}" select="com.healthengine.mapper.CompanyImagesModelMapper.
selectByInformationId" >
</collection>
</resultMap>
2.sql查询:
<!-- 根据会员Id查看公司详情 -->
<select id="selectByMemberId" resultMap="
BaseResultMap_leftJoin_images" parameterType="java.util.Map" >
select
<include refid="Base_Column_List_images" />, city.name as city_name,
#{picture_view_path_pre} as picture_view_path_pre
FROM tb_company c
LEFT JOIN tb_city city on c.city_id = city.city_id
where c.flag=1 and c.member_id = #{memberId,jdbcType=BIGINT}
</select>
3.圖片表sql
<select id="
selectByInformationId" resultMap="BaseResultMap" parameterType="java.util.Map" >
select
<include refid="Base_Column_List_images" />, CONCAT(#{
picture_view_path_pre},img.image_path) as imagePath,
img.image_path as localHead_Path
from tb_company_images img
where img.flag=1 and img.company_id = #{
companyId,jdbcType=BIGINT}
</select>
companyId 和
picture_view_path_pre需要的参数需要从第一个Mapper.xml里传过来。
分享到:
相关推荐
MyBatis加载关联关系对象主要通过两种方式 嵌套查询: 1、嵌套查询是通过另一条SQL映射语句来返回预期的复杂类型 2、嵌套查询是查询语句中插入一个子SQL语句 3、嵌套查询会执行多条SQL语句 4、嵌套查询编写...
9.1 关联关系概述 在关系型数据库中,多表之间存在着三种关联关系,分别为一对一、一对多和多对多,如下图所示: 一对一 一对多 多对多 在任意一方引入对方主键作为外键; 在“多”的一方,添加“一”的一方的主键...
编写程序,对MyBatis的关联映射进行应用,要求对一对一、一对多、多对多三种关系,分别应用嵌套查询和嵌套结果两种方式,来实现操作。
mybatis实战教程mybatis in action之四实现关联数据的查询 mybatis实战教程mybatis in action之五与spring3集成附源码 mybatis实战教程mybatis in action之六与Spring MVC 的集成 mybatis实战教程mybatis in action...
Mybatis 连接池与事务 Mybatis 的 动态 SQL 语句 Mybatis 关联关系 要实现多对多的关联,需要满足如下两个条件: 1. 先决条件一:多对多需要一种中间表建立连接...2. 先决条件二:多对多关系是由两个一对多关系组成的,
支持一对一、一对多、多对一注解方式的关联查询。 支持在实体模型类方法上配置query注解。可直接调用实体方法进行相关查询。 框架全部采用懒加载机制。 支持单对象查询、分页查询、集合查询、级联查询。 默认...
采用两个版本第一个版本:springboot+mybatis+dubbo+rocketmq+mysql+redis等。第二个版 MySQL 是一款广受欢迎的开源关系型数据库管理系统(RDBMS),由瑞典MySQL AB公司开发,现隶属于美国甲骨文公司(Oracle)。自...
6、多表关联查询 7、延长加载策略 8、xml和注解开发 Spring基础 第一阶段 开源框架源码剖析 1、使用springIOC实现程序解耦合 2、注解方式实现 3、Spring对Junit的支持 4、理解spring的 ioc和aop 5、JdbcTemplate...
无论数据库内原始数据是以何种样式的表结构存储,无论最终用户要求数据以何种表格样式显示,FineReport报表软件独创的多数据源关联查询、公式动态扩展计算等强大的功能,在无需改变表结构,无需改变用户的要求的基础...