<select id="findAdverts" resultMap="advertMap"> SELECT a.*, p.id AS position_id, p.name AS position_name, m.id AS media_id, 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 (id = :id OR :id IS NULL) </select>和<resultMap id="advertMap" type="com.ruifan.taihaolian.biz.model.dto.AdvertDTO"> <id property="id" column="id"/> <result property="name" column="name"/> <association property="adPosition" javaType="com.ruifan.taihaolian.biz.model.AdPosition"> <id property="id" column="position_id"/> <result property="name" column="position_name"/> </association> <association property="mediaFile" javaType="com.ruifan.taihaolian.biz.model.MediaFile"> <id property="id" column="id"/> <result property="fileName" column="name"/> <result property="fileType" column="type"/> <result property="fileUrl" column="url"/> </association> </resultMap>对吗?
时间: 2023-12-05 15:48:38 浏览: 38
这是一个 MyBatis 的 XML 映射文件,其中包含了一个名为 findAdverts 的 SQL 查询语句和一个名为 advertMap 的结果映射。SQL 查询语句使用了 LEFT JOIN 连接 advert、ad_position 和 media_file 三张表,通过 WHERE 子句对查询进行筛选。结果映射定义了 AdvertDTO、AdPosition 和 MediaFile 三个类之间的关系,其中 AdvertDTO 包含了 id、name、adPosition 和 mediaFile 四个属性,分别对应 advert 表的 id 和 name 字段,ad_position 表的 id 和 name 字段,以及 media_file 表的 id、name、type 和 url 字段。
相关问题
<select id="findAdverts" resultMap="advertMap"> SELECT a.*, p.id AS position_id, p.name AS position_name, m.id AS media_id, 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 (id = :id OR :id IS NULL) </select>是否正确
这个 SQL 语句是正确的,它查询了 `advert` 表、`ad_position` 表和 `media_file` 表,使用了 `LEFT JOIN` 进行了关联查询。条件语句 `(id = :id OR :id IS NULL)` 判断了是否传入了 ID 参数,如果传入了,则只查询 ID 对应的数据,如果没有传入,则查询全部数据。
需要注意的是,`resultMap` 属性指定了查询结果的映射方式,需要在相应的 resultMap 中定义 `advertMap`。同时,作为一个符合规范的 SQL 语句,它还需要在 `<mapper>` 标签中定义 `namespace` 属性,指定该 SQL 语句所属的命名空间,如下所示:
```xml
<mapper namespace="com.example.mapper.AdvertMapper">
<resultMap id="advertMap" type="Advert">
<!-- 定义查询结果与实体类的映射关系 -->
</resultMap>
<select id="findAdverts" resultMap="advertMap">
SELECT a.*, p.id AS position_id, p.name AS position_name, m.id AS media_id, 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 (id = :id OR :id IS NULL)
</select>
</mapper>
```
<select id="getProductByStatusWhenCalculatePage" resultMap="productResultMap"> SELECT p.id, p.user_id, p.goods_name, GROUP_CONCAT(i.url SEPARATOR ',') AS images FROM pms_product AS p LEFT JOIN pms_product_images AS i ON p.id = i.product_id WHERE p.status = #{status} GROUP BY p.id </select> <resultMap id="productResultMap" type="Product"> <id property="id" column="id"/> <result property="userId" column="user_id"/> <result property="goodsName" column="goods_name"/> <collection property="images" ofType="java.lang.String"> <result column="images" /> </collection> </resultMap> resulttype用Map实现,不要使用vo类或实体类
如果要用 Map 类型来表示查询结果,可以将 resultMap 中的 type 属性设置为 java.util.Map,然后在 resultMap 中使用 map 标签来映射字段。
改写后的代码如下:
```
<select id="getProductByStatusWhenCalculatePage" resultMap="productResultMap">
SELECT p.id, p.user_id, p.goods_name, GROUP_CONCAT(i.url SEPARATOR ',') AS images
FROM pms_product AS p
LEFT JOIN pms_product_images AS i ON p.id = i.product_id
WHERE p.status = #{status}
GROUP BY p.id
</select>
<resultMap id="productResultMap" type="java.util.Map">
<result property="id" column="id" />
<result property="userId" column="user_id" />
<result property="goodsName" column="goods_name" />
<result property="images" column="images" javaType="java.lang.String" />
</resultMap>
```
在 resultMap 中,使用 result 标签将每个字段映射到 Map 中的相应 key 上。注意,images 字段需要显式地指定 javaType 为 java.lang.String,否则可能会出现类型转换错误。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![deb](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)