mysql -- collection一對多查詢

數據庫表java

角色組表:數據庫

CREATE TABLE `sso_character_group` ( `group_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '角色組ID', `group_name` varchar(255) DEFAULT NULL COMMENT '角色組名稱', PRIMARY KEY (`group_id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

角色表:app

CREATE TABLE `sso_character` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '角色ID', `name` varchar(255) DEFAULT NULL COMMENT '角色名稱', `group_id` int(11) DEFAULT NULL COMMENT '所屬角色組ID', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

一個角色組包含多個角色spa

 

兩個POJOcode

//CharacterGroup.java
package
cn.com.hiveview.entity.module.portal; import lombok.Data; @Data public class CharacterCondition { private Integer id; private String name; } //Character.java package cn.com.hiveview.entity.module.portal; import lombok.Data; import java.util.List; @Data public class CharacterGroupCondition { private Integer groupId; private String groupName; private List<CharacterCondition> characterList; }

controller--mapper層都是直接調用下一層,此處省略xml

直接上mapper.xmlblog

 1     <resultMap id="characterGroup" type="cn.com.hiveview.entity.module.portal.CharacterGroupCondition">
 2         <id column="group_id" property="groupId"/>
 3         <result column="group_name" property="groupName"/>
 4         <collection property="characterList" column="{groupId=group_id}" select="getCharacterList"/>
 5     </resultMap>
 6 
 7     <select id="getList" resultMap="characterGroup" parameterType="cn.com.hiveview.entity.module.portal.CharacterGroupCondition">
 8  SELECT group_id,group_name FROM sso_character_group  9     </select>
10 
11     <select id="getCharacterList" resultType="cn.com.hiveview.entity.module.portal.CharacterCondition">
12  select id,name from sso_character where group_id=#{groupId} 13     </select>

 

結果展現get

相關文章
相關標籤/搜索