@Data @AllArgsConstructor @NoArgsConstructor @Builder @Table(name = "advert") public class Advert { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @ApiModelProperty(value = "广告唯一id") private Integer id; @ApiModelProperty(value = "广告名称") @NotBlank(message = "广告名称不可为空", groups = {AddGroup.class, UpdateGroup.class}) private String name; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "position_id") private AdPosition position; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "media_id") private MediaFile media; } @Data @AllArgsConstructor @NoArgsConstructor @Builder @Table(name = "ad_position") public class AdPosition { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @ApiModelProperty(value = "位置名称") @NotBlank(message = "位置名称不可为空", groups = {AddGroup.class}) private String name; @OneToMany(mappedBy = "AdPosition", cascade = CascadeType.ALL) private List<Advert> adverts = new ArrayList<>(); } @Data @AllArgsConstructor @NoArgsConstructor @Builder @Table(name = "media_file") public class MediaFile { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @ApiModelProperty(value = "媒体文件名称") private String fileName; @ApiModelProperty(value = "媒体文件类型") private Integer fileType; @ApiModelProperty(value = "媒体文件URL地址") private String fileUrl; @OneToMany(mappedBy = "MediaFile", cascade = CascadeType.ALL) private List<Advert> adverts = new ArrayList<>(); } public interface MyBaseMapper<T> extends Mapper<T>, MySqlMapper<T>,MyInsertListMapper<T> {} public interface AdvertMapper extends MyBaseMapper<Advert> {},如何在查询advert表时,将关联的其余两张表的数据一并查询出来?请将mapper.xml文件全部展示出来
时间: 2024-03-03 19:47:03 浏览: 61
advert.rar
AdvertMapper.xml文件如下:
```
<?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.example.mapper.AdvertMapper">
<resultMap id="advertMap" type="com.example.entity.Advert">
<id property="id" column="id" />
<result property="name" column="name" />
<association property="position" column="position_id" javaType="com.example.entity.AdPosition">
<id property="id" column="id" />
<result property="name" column="name" />
</association>
<association property="media" column="media_id" javaType="com.example.entity.MediaFile">
<id property="id" column="id" />
<result property="fileName" column="file_name" />
<result property="fileType" column="file_type" />
<result property="fileUrl" column="file_url" />
</association>
</resultMap>
<select id="selectAdvert" resultMap="advertMap">
SELECT a.id, a.name, a.position_id, a.media_id, p.name as position_name, m.file_name, m.file_type, m.file_url
FROM advert a
LEFT JOIN ad_position p ON a.position_id = p.id
LEFT JOIN media_file m ON a.media_id = m.id
WHERE a.id = #{id}
</select>
</mapper>
```
在上面的XML文件中,我们定义了一个名为advertMap的结果映射,它将advert表、ad_position表和media_file表的数据全部查询出来。我们还定义了一个名为selectAdvert的查询语句,它使用advertMap作为结果映射,并在SELECT语句中使用LEFT JOIN将三个表连接起来。最后,我们将这个查询语句的ID设置为selectAdvert,以便在Java代码中调用它。
阅读全文