mybatis的一些知识点
一、collection 标签:用于定义关联的list集合类型的封装规则
例子:
public class CategoryVO { private Integer id; private String name; private String type; private Integer fatherId; // 三级分类vo list private List<SubCategoryVO> subCategoryVOList; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getType() { return type; } public void setType(String type) { this.type = type; } public Integer getFatherId() { return fatherId; } public void setFatherId(Integer fatherId) { this.fatherId = fatherId; } public List<SubCategoryVO> getSubCategoryVOList() { return subCategoryVOList; } public void setSubCategoryVOList(List<SubCategoryVO> subCategoryVOList) { this.subCategoryVOList = subCategoryVOList; } }
public class SubCategoryVO { private Integer subId; private String subName; private String subType; private Integer subFatherId; public Integer getSubId() { return subId; } public void setSubId(Integer subId) { this.subId = subId; } public String getSubName() { return subName; } public void setSubName(String subName) { this.subName = subName; } public String getSubType() { return subType; } public void setSubType(String subType) { this.subType = subType; } public Integer getSubFatherId() { return subFatherId; } public void setSubFatherId(Integer subFatherId) { this.subFatherId = subFatherId; } }
CategoryVO 里有list字段SubCategoryVO,查询的时候,要使用collection在xml里进行配置
<resultMap id="myCategoryVO" type="com.zb.pojo.vo.CategoryVO"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="type" property="type"/> <result column="fatherId" property="fatherId"/> <!-- collection 标签:用于定义关联的list集合类型的封装规则 property:对应三级分类的list属性名 ofType:集合的类型,三级分类的vo --> <collection property="subCategoryVOList" ofType="com.zb.pojo.vo.SubCategoryVO"> <id column="subId" property="subId"/> <result column="subName" property="subName"/> <result column="subType" property="subType"/> <result column="subFatherId" property="subFatherId"/> </collection> </resultMap>
<select id="getSubCatList" resultMap="myCategoryVO" parameterType="int"> SELECT f.id as id, f.`name` as `name`, f.type as type, f.father_id as fatherId, c.id as subId, c.`name` as subName, c.type as subType, c.father_id as subFatherId FROM category f LEFT JOIN category c on f.id = c.father_id WHERE f.father_id = #{rootCatId} </select>
这样就可以查询出包含列表的数据:
{ "status": 200, "msg": "OK", "data": [ { "id": 11, "name": "蛋糕", "type": "2", "fatherId": 1, "subCategoryVOList": [ { "subId": 37, "subName": "蒸蛋糕", "subType": "3", "subFatherId": 11 }, { "subId": 38, "subName": "软面包", "subType": "3", "subFatherId": 11 }, { "subId": 39, "subName": "脱水蛋糕", "subType": "3", "subFatherId": 11 }, { "subId": 40, "subName": "马卡龙", "subType": "3", "subFatherId": 11 }, { "subId": 41, "subName": "甜甜圈", "subType": "3", "subFatherId": 11 }, { "subId": 42, "subName": "三明治", "subType": "3", "subFatherId": 11 }, { "subId": 43, "subName": "铜锣烧", "subType": "3", "subFatherId": 11 } ] }, { "id": 12, "name": "点心", "type": "2", "fatherId": 1, "subCategoryVOList": [ { "subId": 44, "subName": "肉松饼", "subType": "3", "subFatherId": 12 }, { "subId": 45, "subName": "华夫饼", "subType": "3", "subFatherId": 12 }, { "subId": 46, "subName": "沙琪玛", "subType": "3", "subFatherId": 12 }, { "subId": 47, "subName": "鸡蛋卷", "subType": "3", "subFatherId": 12 }, { "subId": 48, "subName": "蛋饼", "subType": "3", "subFatherId": 12 }, { "subId": 49, "subName": "凤梨酥", "subType": "3", "subFatherId": 12 }, { "subId": 50, "subName": "手撕面包", "subType": "3", "subFatherId": 12 } ] } ] }