Mybatis使用Collection屬性的示例代碼
前言
這篇文章實現(xiàn)一下不使用left join
等連接關鍵字來實現(xiàn)Mybatis的聯(lián)表查詢功能。包括json
類型數(shù)據(jù)映射到Java實體類
中。
庫表
父表db1_json
子表db1_json_attach
,子表parent_id
對應副本的id
。
實體類
新建Db1JsonDTO
數(shù)據(jù)傳遞對象實體。
@Data public class Db1JsonDTO { //父表id private Long id; //父表信息 private JSONObject info; //子表數(shù)據(jù) private List<Db1JsonAttach> attaches; }
查看子表實體屬性
@TableName(value ="db1_json_attach") @Data public class Db1JsonAttach implements Serializable { private Long parentId; @TableId(type = IdType.ID_WORKER) private Long id; @TableField(typeHandler = JacksonTypeHandler.class) private JSONObject info; private static final long serialVersionUID = 1L; }
Mapper.xml
父xml
處理,sql沒什么好看的,看一下<resultMap>
中各個屬性吧。
<resultMap id="Db1JsonDTOResultMap" type="com.it.dto.Db1JsonDTO"> <result column="info" property="info" typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/> <collection property="attaches" column="{parentId = id}" select="com.it.mapper.Db1JsonAttachMapper.getAttachList"/> </resultMap> <select id="selectDb1JsonList" resultMap="Db1JsonDTOResultMap"> select id,info from db1_json </select>
id
:自定義id,在SQL查詢后使用resultMap
屬性并指定id返回。type
:返回字段對應的實體類,也可以是DTO,實體需要有Getter、Setter
方法。<result column
:數(shù)據(jù)庫中的字段名稱。<result property
:與數(shù)據(jù)庫字段對應的屬性名。<result typeHandler
:對特殊類型進行處理,例如當前為json類型,指定將返回的結果進行轉化后映射到實體屬性中。<collection property
:父實體中的list集合數(shù)據(jù)
,即為子實體類的集合。<collection column
:id字段賦值,將父id字段賦值給子id。<collection select
:指定子xml
SQL查詢的方法id。
子xml
處理
<resultMap id="BaseResultMap" type="com.it.entity.Db1JsonAttach"> <id property="parentId" column="parent_id" jdbcType="BIGINT"/> <result property="id" column="id" jdbcType="BIGINT"/> <result property="info" column="info" typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/> </resultMap> <select id="getAttachList" resultMap="BaseResultMap"> select * from db1_json_attach where parent_id = #{parentId} </select>
注:Mapper中不需要定義查詢方法,只在XML中定義即可。
測試
看一下測試結果
[ { "id": 1681557955655049218, "info": { "address": "洛杉磯", "name": "科比", "hobby": "直升機" }, "attaches": [ { "parentId": 1681557955655049218, "id": 1681557956250640385, "info": { "address": "洛杉磯", "name": "科比", "hobby": "直升機" } } ] }, { "id": 1681558109766361089, "info": { "address": "美國", "name": "蔡徐坤", "hobby": "唱跳rap籃球" }, "attaches": [ { "parentId": 1681558109766361089, "id": 1681558109766361090, "info": { "address": "美國", "name": "蔡徐坤", "hobby": "唱跳rap籃球" } } ] }, { "id": 1681558181665120257, "info": { "address": "理塘", "name": "丁真", "hobby": "測碼" }, "attaches": [ { "parentId": 1681558181665120257, "id": 1681558181732229122, "info": { "address": "理塘", "name": "丁真", "hobby": "測碼" } } ] } ]
總結
這個方式的聯(lián)表查詢用的不算太多,但是在一些特殊情況可以使用這種方式來完成查詢子表的某一下數(shù)據(jù)集合。
到此這篇關于Mybatis使用Collection屬性的示例代碼的文章就介紹到這了,更多相關Mybatis Collection屬性內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- mybatis collection關聯(lián)查詢多個參數(shù)方式
- MyBatis使用嵌套查詢collection和association的實現(xiàn)
- mybatis?resultMap之collection聚集兩種實現(xiàn)方式
- mybatis中association和collection的使用與區(qū)別
- MyBatis的collection和association的使用解讀
- Mybatis中一對多(collection)和一對一(association)的組合查詢使用
- Mybatis的collection三層嵌套查詢方式(驗證通過)
- mybatis?collection和association的區(qū)別解析
- MyBatis中<collection>標簽的多種用法
相關文章
java中InputStream轉為MultipartFile的解決方案
這篇文章主要介紹了java中InputStream轉為MultipartFile的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-03-03