Mybatis之一对多查询标签

使用Mybatis-generator生成实体类与dao层相关信息后, 需要用到一对多查询时, 修改SQL映射文件

1. 数据库结构


1.1 奖项(award)表

award表

1.2奖项照片表(award_photo)

奖项照片表


2. 实体类描述

2.1 Award

package com.turing.team_website.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.*;

import java.util.Date;
import java.util.List;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Award {

    private Integer awardId;

    private String awardName;
    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date awardTime;
	/**
	 *  照片路径集合 
	 */
    private List<String> photoLocList;

}

2.2 AwardMapper

package com.turing.team_website.dao;

import com.turing.team_website.entity.Award;
import com.turing.team_website.entity.AwardExample;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface AwardMapper {
    long countByExample(AwardExample example);

    int deleteByExample(AwardExample example);

    int insert(Award record);

    int insertSelective(Award record);
    
    List<Award> selectByExample(AwardExample example);

    int updateByExampleSelective(@Param("record") Award record, @Param("example") AwardExample example);

    int updateByExample(@Param("record") Award record, @Param("example") AwardExample example);
}

3.SQL映射文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.turing.team_website.dao.AwardMapper">
  <resultMap id="BaseResultMap" type="com.turing.team_website.entity.Award">
    <result column="award_id" jdbcType="INTEGER" property="awardId" />
    <result column="award_name" jdbcType="VARCHAR" property="awardName" />
    <result column="award_time" jdbcType="DATE" property="awardTime" />
  </resultMap>
  <!--带奖项照片的查询映射列-->
  <resultMap id="WithPhotoBaseResultMap" type="com.turing.team_website.entity.Award">
    <result column="award_id" jdbcType="INTEGER" property="awardId" />
    <result column="award_name" jdbcType="VARCHAR" property="awardName" />
    <result column="award_time" jdbcType="DATE" property="awardTime" />
    <!--一对多查询-->
    <!--将award_photo_loc封装进List集合中, 泛型为String-->
    <collection property="photoLocList" ofType="java.lang.String" javaType="java.util.List">
      <result column="award_photo_loc" />
    </collection>
  </resultMap>
  <sql id="Example_Where_Clause">
    ...
  </sql>
  <sql id="Base_Column_List">
    award_id, award_name, award_time
  </sql>
  <!--带奖项图片路径的查询, 必须要有别名, 否则关联查询时会报award_id描述不清晰错误-->
  <sql id="With_PhotoLoc_Base_Column_List">
    a.award_id, a.award_name, a.award_time, ap.award_photo_loc
  </sql>
  <select id="selectByExample" parameterType="com.turing.team_website.entity.AwardExample" resultMap="WithPhotoBaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <!--关联查询-->
    <include refid="With_PhotoLoc_Base_Column_List" />
    from award a inner join award_photo ap where a.award_id = ap.award_id
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  ...其它省略
</mapper>
关键字 collection ofType javaType
解释 一对多查询时使用的标签 多的一方的数据类型 多的一方的封装集合类型(数组,集合…)
发布了50 篇原创文章 · 获赞 46 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_41949328/article/details/95041442