閑聊:這種問題基本上都比較常見,以前遇到這個問題的時候總是故意繞彎來避免,后來經過我一天的百度瀏覽,終於懂了,搜索百度,我們會看到很多關於這個問題的解決方案,基本上都是說使用resultMap的子查詢方法,經過我親身實驗,確實有用,但是很多博客解決方案是提出來了,但思路不夠具體,只是草草的說了幾句,比如,使用resultMap子查詢來解決,具體的解決方法應該是啥,我之前從來沒用過resultMap的子查詢,搞了半天也沒弄明白,后來終於看到一個有用的博客了,現在我就來更具體的給大家分享這個解決方案
1.resultMap
首先給大家更具體的介紹一下resultMap里面的包含的元素以及詞義:
<!--column不做限制,可以為任意表的字段,而property須為type 定義的pojo屬性--> <resultMap id="唯一的標識" type="映射的pojo對象"> <id column="表的主鍵字段,或者可以為查詢語句中的別名字段" jdbcType="字段類型" property="映射pojo對象的主鍵屬性" /> <result column="表的一個字段(可以為任意表的一個字段)" jdbcType="字段類型" property="映射到pojo對象的一個屬性(須為type定義的pojo對象中的一個屬 性)"/> <association property="pojo的一個對象屬性" javaType="pojo關聯的pojo對象"> <id column="關聯pojo對象對應表的主鍵字段" jdbcType="字段類型" property="關聯pojo對象的主席屬性"/> <result column="任意表的字段" jdbcType="字段類型" property="關聯pojo對象的屬性"/> </association> <!-- 集合中的property須為oftype定義的pojo對象的屬性--> <collection property="pojo的集合屬性" ofType="集合中的pojo對象"> <id column="集合中pojo對象對應的表的主鍵字段" jdbcType="字段類型" property="集合中pojo對象的主鍵屬性" /> <result column="可以為任意表的字段" jdbcType="字段類型" property="集合中的pojo對象的屬性" /> </collection> </resultMap> <strong>如果collection標簽是使用嵌套查詢,格式如下:</strong> <collection column="傳遞給嵌套查詢語句的字段參數" property="pojo對象中集合屬性" ofType="集合屬性中的pojo對象" select="嵌套的查詢語句" > </collection>
好了,理解了以上各元素的意思后,開始寫代碼了【注意:這里只寫子查詢哦!】
2.創建兩個存在父子關系的表和類
【這里就不寫在數據庫的創表數據了】
- 主表字段信息:
@Data
public class TbNoticeVO extends BaseVo {
@ApiModelProperty(value = "自增主鍵")
private Long id;
@ApiModelProperty(value = "公告內容")
private String notice;
@ApiModelProperty(value = "創建時間")
private java.time.LocalDateTime createDate;
@ApiModelProperty(value = "修改時間")
private java.time.LocalDateTime modifyDate;
}
- 子表字段信息:
@Data
public class TbNoticeFileVO extends BaseVo {
@ApiModelProperty(value = "自增主鍵")
private Long id;
@ApiModelProperty(value = "公告表id")
private Long noticeId;
@ApiModelProperty(value = "標題")
private String title;
@ApiModelProperty(value = "文件上傳路徑url")
private String fileUrl;
@ApiModelProperty(value = "創建時間")
private java.time.LocalDateTime createDate;
}
因為要關聯兩張表一起查詢,所以在此,我們可以創建一個bo類
@Data
public class TbNoticeBO implements Serializable {
private static final Long serialVersionUID = 1L;
@ApiModelProperty(value = "自增主鍵")
private Long id;
@ApiModelProperty(value = "公告內容")
private String notice;
@ApiModelProperty(value = "創建時間")
@DateTimeFormat(pattern = "yyyy-MM-dd")
private java.time.LocalDate createDate;
private List<TbNoticeFileVO> fileVOList;
}
3.mapper層接口
public interface TbNoticeMapper {
List<TbNoticeBO> getNoticeFile(TbNotice model);
}
4.service層接口
public interface ITbNoticeService {
Page groupbypage(TbNoticeVO model);
}
5.serviceImpl服務實現層
@Service
public class TbNoticeService extends AbstractBaseService implements ITbNoticeService {
@Autowired
private TbNoticeMapper tbNoticeMapper;
@Autowired
private TbNoticeFileMapper tbNoticeFileMapper;
@Override
public Page groupbypage(TbNoticeVO model) {
TbNotice param = copyProperties(model, TbNotice.class);
PageHelper.startPage(model.getPageNum(), model.getPageSize());
List<TbNoticeBO> list = tbNoticeMapper.getNoticeFile(param);
PageInfo pageInfo = new PageInfo(list);
return new Page(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getNavigateLastPage(), pageInfo.getTotal(), list);
}
}
6.mapping映射文件
<!--公告通知表的resultMap映射-->
<resultMap id="getGroupByList" type="com.bestva.service.bestvascm.entity.bo.TbNoticeBO">
<id property="id" column="id"></id>
<result property="notice" column="notice"></result>
<result property="createDate" column="create_date"></result>
<collection property="fileVOList" ofType="com.bestva.service.bestvascm.entity.vo.TbNoticeFileVO"
javaType="java.util.List" column="{noticeId=id}" select="getByNoticeId">
</collection>
</resultMap>
<select id="getNoticeFile" resultMap="getGroupByList"
parameterType="com.bestva.service.bestvascm.entity.dto.TbNotice">
select n.id,n.notice,n.create_date
from tb_notice n
where 1=1
<if test="id != null">
n.id=#{id}
</if>
ORDER BY create_date DESC
</select>
<resultMap id="NoticeFileResultMap" type="com.bestva.service.bestvascm.entity.vo.TbNoticeFileVO">
<id property="id" column="id"></id>
<result property="noticeId" column="notice_id"></result>
<result property="title" column="title"></result>
<result property="fileUrl" column="file_url"></result>
<result property="createDate" column="create_date"></result>
</resultMap>
<select id="getByNoticeId" resultMap="NoticeFileResultMap">
select f.id,f.notice_id,f.title,f.file_url
from tb_notice_file f
where f.notice_id=#{noticeId}
</select>
7.子查詢詳解【圖片版】

8.Cotroller返回json數據
@PostMapping("/tbNotice/groupbypage")
@AutoLog(description = "分頁查詢公告通知信息接口")
@ApiOperation(value = "分頁查詢公告通知信息接口")
public BaseResponse<List<TbNoticeBO>> groupByPage(@RequestBody TbNoticeVO request) {
return new BaseResponse(true, "查詢成功", 0, tbNoticeService.groupbypage(request));
}
使用postMam得出的json數據

--我是一只在深圳打拼的蝸牛,一起加油吧!
未經允許不可轉載哦!
