MyBatis一对多嵌套查询的完整实例
前言
嵌套查询的实现原理为两次查询,比如产品表为主表,图片表为从表通过product_id字段与产品表id字段关联实现一对多,嵌套查询首先查询主表的数据然后将主表id字段赋值给从表实体类中product_id字段(productId)然后通过dao接口路径映射找到对应的MyBatisXMl文件SQL语句ID如:com.liao.dao.DImgMapper.selectDImgByProductId进行子查询也就是第二次查询。然后返回数据
数据库建表语句和测试数据如下:
数据库版本为MySQL8.0
产品表
DROPTABLEIFEXISTS`d_product`; CREATETABLE`d_product`( `id`int(11)NOTNULLAUTO_INCREMENTCOMMENT'产品ID', `product_name`varchar(255)CHARACTERSETutf8COLLATEutf8_binNULLDEFAULTNULLCOMMENT'产品名称', `product_introduction`textCHARACTERSETutf8COLLATEutf8_binNULLCOMMENT'产品介绍', `product_category`int(11)NULLDEFAULTNULLCOMMENT'产品ID', `product_status`int(1)NULLDEFAULTNULLCOMMENT'产品状态', `create_time`datetime(0)NULLDEFAULTNULLCOMMENT'创建时间', PRIMARYKEY(`id`)USINGBTREE )ENGINE=InnoDBAUTO_INCREMENT=18CHARACTERSET=utf8COLLATE=utf8_binCOMMENT='产品表'ROW_FORMAT=Dynamic; INSERTINTO`d_product`VALUES(1,'测试产品名称修改','测试产品介绍修改',NULL,1,'2020-02-0212:40:06'); INSERTINTO`d_product`VALUES(2,'产品名称','产品介绍',NULL,1,'2020-03-0218:15:07'); INSERTINTO`d_product`VALUES(3,'bbb','bbb',NULL,1,'2020-03-0122:18:40');
图片表
DROPTABLEIFEXISTS`d_img`; CREATETABLE`d_img`( `id`int(11)NOTNULLAUTO_INCREMENTCOMMENT'id', `product_id`int(11)NULLDEFAULTNULLCOMMENT'产品图片ID', `img`varchar(500)CHARACTERSETutf8COLLATEutf8_binNULLDEFAULTNULLCOMMENT'图片', `create_time`datetime(0)NULLDEFAULTNULLCOMMENT'创建时间', PRIMARYKEY(`id`)USINGBTREE, INDEX`product_id`(`product_id`)USINGBTREE, CONSTRAINT`d_img_ibfk_1`FOREIGNKEY(`product_id`)REFERENCES`d_product`(`id`)ONDELETERESTRICTONUPDATERESTRICT )ENGINE=InnoDBAUTO_INCREMENT=86CHARACTERSET=utf8COLLATE=utf8_binCOMMENT='产品图片'ROW_FORMAT=Dynamic; INSERTINTO`d_img`VALUES(40,1,'1491803633034_683819.jpg','2020-03-0317:21:20'); INSERTINTO`d_img`VALUES(40,1,'1491803633034_683819.jpg','2020-03-0317:21:20'); INSERTINTO`d_img`VALUES(41,1,'1568950881751_702421.jpg','2020-03-0317:21:20');
Java实体类:
//将这个注解写在类上之后,就会忽略类中不存在的字段,否则可能会报错 @JsonIgnoreProperties(value={"handler"}) /** * *TODO:产品类 *@authorLiAo *@date2020/5/2017:04 */ publicclassDProduct{ privateIntegerid; privateStringproductName; privateIntegerproductCategory; privateIntegerproductStatus; privateDatecreateTime; privateStringproductIntroduction; privateListdImgs;//用于存放图片集合 //..getsettoString } /** * *TODO:产品图片类 *@authorLiAo *@date2020/5/2017:05 */ @JsonIgnoreProperties(value={"handler"}) publicclassDImg{ privateIntegerid; privateIntegerproductId; privateStringimg; privateDatecreateTime; //..getsettoString }
实体类创建好后要编写Dao接口和MapperXML了
持久层接口DAO:
/** * *TODO:产品Dao接口 *@authorLiAo *@date2020/5/2017:08 */ publicinterfaceDProductMapper{ /** *产品图片一对多嵌套 *@paramrecord查询条件 *@return返回参数 */ ListproductSelect(DProductrecord); }
产品MyBatisxml:
select d.id, d.product_name, d.product_category, d.product_status, d.create_time, d.product_introduction fromd_productd where1=1 andd.id=#{id} andd.product_namelikeconcat(#{productName},'%') andd.product_status=#{productStatus} andd.create_timelikeconcat(#{createTime},'%') andd.product_introductionlikeconcat(#{productIntroduction},'%')
图片MyBatisxml:
selecti.idasdid, i.product_id, i.img, i.create_time fromd_imgi wherei.product_id=#{productId}
测试查询结果
查询结果为一个产品对象里有若干个产品图片对象。
{ "id":18, "productName":"产品添加图片上传测试", "productCategory":null, "productStatus":1, "createTime":"2020-04-14T13:40:40.000+0000", "productIntroduction":"产品添加图片上传测试", "dImgs":[ { "id":92, "productId":18, "img":"01.jpg", "createTime":"2020-04-26T02:33:04.000+0000" }, { "id":93, "productId":18, "img":"1554103835292_610234.jpg", "createTime":"2020-04-26T02:33:04.000+0000" }, { "id":94, "productId":18, "img":"1555484699771_582172.jpg", "createTime":"2020-04-26T02:33:04.000+0000" }, { "id":95, "productId":18, "img":"1554103835292_610234.jpg", "createTime":"2020-04-26T02:33:04.000+0000" } ] },
总结
到此这篇关于MyBatis一对多嵌套查询的文章就介绍到这了,更多相关MyBatis一对多嵌套查询内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!