Mybatis框架一對多級聯查詢的場景很多,很多人往往對具體的Xml映射文件配置的細節不太注意,今天筆者就給大家做個詳細的講解。常用的一對多有2種配置方式。
-
表名字和實體名字對應,作爲講解,爲了易懂,這裏只設置了幾個必要字段。實體結構如下圖:
- 商品實體Product(對應product表)
-
public class Product { private String productId; private String productName; private Double salePrice; private List<ProductPic> pics; public String getProductId() { return productId; } public void setProductId(String productId) { this.productId = productId == null ? null : productId.trim(); } public String getProductName() { return productName; } public void setProductName(String productName) { this.productName = productName == null ? null : productName.trim(); } public Double getSalePrice() { return salePrice; } public void setSalePrice(Double salePrice) { this.salePrice = salePrice; } public List<ProductPic> getPics() { return pics; } public void setPics(List<ProductPic> pics) { this.pics = pics; }
圖片實體ProductPic(對應productPic表)
-
public class ProductPic { private String id; /** * 商品id */ private String productId; /** * 圖片文件id */ private String attachmentId;
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.shsc.base.data.common.mapper.dao.ProductMapper"> <resultMap id="BaseResultMap" type="com.shsc.base.data.common.mapper.entity.CustomerProduct"> <result column="product_id" jdbcType="VARCHAR" property="productId" /> <result column="product_name" jdbcType="VARCHAR" property="productName" /> <result column="sale_price" jdbcType="FLOAT" property="salePrice" /> <collection property="pics" ofType="com.shsc.base.data.common.mapper.entity.ProductPic" column="product_id" select="getPics"> </collection> </resultMap> <select id="getPics" parameterType="java.lang.String" resultType="com.shsc.base.data.common.mapper.entity.ProductPic"> SELECT id,product_id,attachment_id FROM product_pic WHERE product_id = #{productId} order by id <!-- 注意這裏的參數productId是指的屬性名而不是數據庫字段名--> </select> <select id="queryProduct" parameterType="Map" resultMap="BaseResultMap"> select p.id as product_id, p.product_name, p.sale_price from product p </select> </mapper>
這種級聯查詢是將主表(product)的查詢結果聯合其他表(product_pic)的查詢結果,封裝成一個對象。主表查詢結果中的某一列數據,作爲其他表查詢的條件。這多個查詢是可以跨越多個映射文件的,即可以跨越多個namespace的。使用時,添加上其所在的namespace全路徑名即可。關聯屬性<collection />的數據來源於另一個查詢getPics,該查詢getPics的動態參數product_id=#{productId}的值則來自於主查詢queryProduct的查詢結果字段product_id對應的屬性名。這種方式符合面向對象的理念,但是涉及嵌套動態級聯查詢,效率較慢。
-
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.shsc.base.data.common.mapper.dao.ProductMapper"> <resultMap id="BaseResultMap" type="com.shsc.base.data.common.mapper.entity.CustomerProduct"> <result column="product_id" jdbcType="VARCHAR" property="productId" /> <result column="product_name" jdbcType="VARCHAR" property="productName" /> <result column="sale_price" jdbcType="FLOAT" property="salePrice" /> <collection property="pics" ofType="com.shsc.base.data.common.mapper.entity.ProductPic"> <id column="id" property="id"/> <result column="product_id" property="productId"/> <result column="attachment_id" property="attachmentId"/> </collection> </resultMap> <select id="queryProduct" resultMap="BaseResultMap"> select p.id as product_id, p.product_name, p.sale_price, pic.id, pic.attachment_id from product p,product_pic pic where p.id = pic.product_id </select> </mapper>
這種方式是將多張表先進行連接,連爲一張表後進行查詢。其查詢本質是一張表。也只有一個select
<collection />是集合的意思,即有多個對象,property:指定關聯屬性,即Product類中的集合屬性,ofType:集合屬性的泛型類型。這是連接查詢效率高一些。
-
以上2種方式查詢出來的數據結構最終如下:
-
[{ "productId": "8dc1e841-c462-4f3b-a5ac-d915475ff4c3", "productName": "白醋", "salePrice": 19, "pics": [ { "id": null, "productId": null, "attachmentId": "12759047a0cf4782b87fdcad6acfa4a4", "type": 0 }, { "id": null, "productId": null, "attachmentId": "7fdad169dba747b0940bc13fca709671", "type": 0 }, { "id": null, "productId": null, "attachmentId": "302e8d178dbf42febe7d66adab3cccbf", "type": 0 } ]}, { "productId": "8dc1e841-c462-4f3b-a5ac-d915475ff4c2", "productName": "陳醋", "salePrice": 17, "pics": [ { "id": null, "productId": null, "attachmentId": "12759047a0cf4782b87fdcad6acfa4a1", "type": 0 }, { "id": null, "productId": null, "attachmentId": "7fdad169dba747b0940bc13fca709656", "type": 0 } ] }]
歡迎各位開發者朋友一起交流。筆者電話(微信):18629374628