sql語句一對多查詢,【mybatis】一對一關聯查詢

 2023-10-21 阅读 26 评论 0

摘要:mybatis: 一對一關聯查詢注意代碼部分實體類(extity)mybatismapperservicecontroller 注意 在.xml文件編寫中有報錯,The content of element type "resultMap" must match ...。參考了這篇博客的解答https://blog.csdn.net/chenxing1990/article/details/7964

mybatis: 一對一關聯查詢

  • 注意
  • 代碼部分
    • 實體類(extity)
    • mybatis
    • mapper
    • service
    • controller

注意

.xml文件編寫中有報錯,The content of element type "resultMap" must match ...。參考了這篇博客的解答https://blog.csdn.net/chenxing1990/article/details/79649958

sql語句一對多查詢,<resultMap>標簽中要按照

<id>
<result>
<association>
<collection>

的順序來排列

代碼部分

實體類(extity)

實體類Product_orderextends擴展類Product_orderExtend

public class Product_order extends Product_orderExtend {private Long orderId;private Integer sellerId;private Long goodId;private Integer purchaserId;private Integer addressId;private BigDecimal price;private Integer shippingMethod;private Integer pickupMethod;private Boolean usePoint;private Integer orderState;private Integer gainPoint;private Date createDate;
}

在擴展類Product_orderExtend

public class Product_orderExtend {private Good good;private User seller;private User purchaser;private Address address;
}

mybatis

sql兩個表關聯查詢,mybatis/mapper/extend下的xml文件中寫擴展mapper

<?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.group5.springboot.crud.mapper.Product_orderMapper"><resultMap type="com.group5.springboot.crud.common.domain.Product_order" id="Product_orderAssociationResultMap"><id property="orderId" column="order_id"/><result property="price" column="price"/><result property="shippingMethod" column="shipping_method"/><result property="pickupMethod" column="pickup_method"/><result property="usePoint" column="use_point"/><result property="orderState" column="order_state"/><result property="gainPoint" column="gain_point"/><result property="createDate" column="create_date"/><association property="good" javaType="com.group5.springboot.crud.common.domain.Good"><id column="good_id" jdbcType="BIGINT" property="goodId" /><result column="image_path" jdbcType="VARCHAR" property="imagePath" /><result column="start_time" jdbcType="TIMESTAMP" property="startTime" /><result column="end_time" jdbcType="TIMESTAMP" property="endTime" /><result column="quantity" jdbcType="INTEGER" property="quantity" /><result column="origin_place" jdbcType="VARCHAR" property="originPlace" /><result column="type" jdbcType="INTEGER" property="type" /><result column="city" jdbcType="VARCHAR" property="city" /><result column="brand" jdbcType="VARCHAR" property="brand" /><result column="content" jdbcType="VARCHAR" property="content" /><result column="good_name" jdbcType="VARCHAR" property="goodName" /><result column="price" jdbcType="DECIMAL" property="price" /><result column="status" jdbcType="INTEGER" property="status" /><result column="current_price" jdbcType="DECIMAL" property="currentPrice" /></association><association property="seller" javaType="com.group5.springboot.crud.common.domain.User"><id column="id" jdbcType="INTEGER" property="id" /><result column="username" jdbcType="VARCHAR" property="username" /><result column="phone_num" jdbcType="VARCHAR" property="phoneNum" /><result column="email" jdbcType="VARCHAR" property="email" /><result column="password" jdbcType="VARCHAR" property="password" /><result column="rewardpoint" jdbcType="INTEGER" property="rewardpoint" /><result column="profile" jdbcType="VARCHAR" property="profile" /></association><association property="purchaser" javaType="com.group5.springboot.crud.common.domain.User"><id column="id" jdbcType="INTEGER" property="id" /><result column="username" jdbcType="VARCHAR" property="username" /><result column="phone_num" jdbcType="VARCHAR" property="phoneNum" /><result column="email" jdbcType="VARCHAR" property="email" /><result column="password" jdbcType="VARCHAR" property="password" /><result column="rewardpoint" jdbcType="INTEGER" property="rewardpoint" /><result column="profile" jdbcType="VARCHAR" property="profile" /></association><association property="address" javaType="com.group5.springboot.crud.common.domain.Address"><id column="address_id" jdbcType="INTEGER" property="addressId" /><result column="receiver" jdbcType="VARCHAR" property="receiver" /><result column="contact_number" jdbcType="VARCHAR" property="contactNumber" /><result column="user_id" jdbcType="INTEGER" property="userId" /><result column="adresss" jdbcType="VARCHAR" property="adresss" /><result column="isdefault" jdbcType="INTEGER" property="isdefault" /></association></resultMap><select id="selectAssociationPurchaser" parameterType="int" resultMap="Product_orderAssociationResultMap">select * from product_order p, user u1, user u2, good g, address awhere p.purchaser_id=#{purchaserId} and a.address_id=p.address_id and u1.id = p.seller_id and u2.id = p.purchaser_id and g.good_id=p.good_id</select><select id="selectAssociationSeller" parameterType="int" resultMap="Product_orderAssociationResultMap">select * from product_order p, user u1, user u2, good g, address awhere p.seller_id=#{sellerId} and a.address_id=p.address_id and u1.id = p.seller_id and u2.id = p.purchaser_id and g.good_id=p.good_id</select>
</mapper>

mapper

同理在mapper/extend中添加接口。

public interface Browsing_historyExtendMapper {List<Good> selectGoodsByUid(int user_id);List<Browsing_history> selectAssociationGood(int userId);
}

service

略。

controller

略。

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/156265.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息