Ibatis 关联查询问题

Ibatis 关联查询问题,ibatis,mybatis,Ibatis,Mybatis,我正在使用mybatis,我遇到了一个关联查询问题,请先看看表结构 DROP TABLE IF EXISTS `comp_items_spec`; CREATE TABLE `comp_items_spec` ( `id` int(11) NOT NULL AUTO_INCREMENT, `comp_id` int(11) NOT NULL, `content_type_id` int(11) NOT NULL, `in_list_flag` char(1) DEFAULT N

我正在使用mybatis,我遇到了一个关联查询问题,请先看看表结构

DROP TABLE IF EXISTS `comp_items_spec`;

CREATE TABLE `comp_items_spec` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `comp_id` int(11) NOT NULL,
  `content_type_id` int(11) NOT NULL,
  `in_list_flag` char(1) DEFAULT NULL,
  `label` varchar(200) DEFAULT NULL,
  `static_flag` char(1) DEFAULT NULL,
  `ext1_name` varchar(200) DEFAULT NULL,
  `ext1_value` text,
  `ext2_name` varchar(200) DEFAULT NULL,
  `ext2_value` text,
  `ext3_name` varchar(200) DEFAULT NULL,
  `ext3_value` text,
  `ext4_name` varchar(200) DEFAULT NULL,
  `ext4_value` text,
  `ext5_name` varchar(200) DEFAULT NULL,
  `ext5_value` text,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=47 DEFAULT CHARSET=latin1;

/*Data for the table `comp_items_spec` */

insert  into `comp_items_spec`(`id`,`comp_id`,`content_type_id`,`in_list_flag`,`label`,`static_flag`,`ext1_name`,`ext1_value`,`ext2_name`,`ext2_value`,`ext3_name`,`ext3_value`,`ext4_name`,`ext4_value`,`ext5_name`,`ext5_value`) values (43,22,1,'\0','description','Y','description','description1......',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL),(44,22,3,'\0','static image','Y',NULL,'http://img3.cache.netease.com/cnews/2012/12/6/20121206092637ba11c.jpg',NULL,'501',NULL,'425',NULL,NULL,NULL,NULL);

/*Table structure for table `components_spec` */

DROP TABLE IF EXISTS `components_spec`;

CREATE TABLE `components_spec` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(200) DEFAULT NULL,
  `show_latest_num` int(11) DEFAULT NULL,
  `more_link_url` varchar(200) DEFAULT NULL,
  `more_link_flag` char(1) DEFAULT NULL,
  `open_in_new_flag` char(1) DEFAULT NULL,
  `create_date` datetime DEFAULT NULL,
  `update_date` datetime DEFAULT NULL,
  `creator_sso` varchar(20) DEFAULT NULL,
  `updator_sso` varchar(20) DEFAULT NULL,
  KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=latin1;

/*Data for the table `components_spec` */

insert  into `components_spec`(`id`,`name`,`show_latest_num`,`more_link_url`,`more_link_flag`,`open_in_new_flag`,`create_date`,`update_date`,`creator_sso`,`updator_sso`) values (22,'Banner',5,'more.blog.ge.com','Y','Y','2012-12-08 21:30:58','2012-12-08 21:30:58','502156886','502156886');
components_spec和comp_items_spec之间的关系是1:N,comp_items_spec有两种数据,动态项和静态项,它们区别于列static_flag,当static_flag='N'时是动态项,我还定义了bean ComponentSpec和CompItemSpec来映射实体。请参阅下面的源代码:

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ComponentSpec {
    private int id;
    private String name;
    private int showLatestNum;
    private String moreLinkUrl;
    private char moreLinkFlag;
    private char openInNewFlag;
    private Date createDate;
    private Date updateDate;
    private String creatorSSO;
    private String updatorSSO;
    private List<CompItemSpec> staticItemSpecList =new ArrayList<CompItemSpec>();
    private List<CompItemSpec> dynamicItemSpecList =new ArrayList<CompItemSpec>();

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public char getMoreLinkFlag() {
        return moreLinkFlag;
    }

    public void setMoreLinkFlag(char moreLinkFlag) {
        this.moreLinkFlag = moreLinkFlag;
    }

    public String getMoreLinkUrl() {
        return moreLinkUrl;
    }

    public void setMoreLinkUrl(String moreLinkUrl) {
        this.moreLinkUrl = moreLinkUrl;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getShowLatestNum() {
        return showLatestNum;
    }

    public void setShowLatestNum(int showLatestNum) {
        this.showLatestNum = showLatestNum;
    }

    public Date getUpdateDate() {
        return updateDate;
    }

    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }

    public char getOpenInNewFlag() {
        return openInNewFlag;
    }

    public void setOpenInNewFlag(char openInNewFlag) {
        this.openInNewFlag = openInNewFlag;
    }

    public List<CompItemSpec> getStaticItemSpecList() {
        return staticItemSpecList;
    }

    public void addStaticItemSpec(CompItemSpec compStaticItemSpec){
        getStaticItemSpecList().add(compStaticItemSpec);
        compStaticItemSpec.setComponentSpec(this);
    }

    public List<CompItemSpec> getDynamicItemSpecList() {
        return dynamicItemSpecList;
    }

    public void addDynamicItemSpec(CompItemSpec dynamicItemSpec){
        getDynamicItemSpecList().add(dynamicItemSpec);
        dynamicItemSpec.setComponentSpec(this);
    }

    public String getCreatorSSO() {
        return creatorSSO;
    }

    public void setCreatorSSO(String creatorSSO) {
        this.creatorSSO = creatorSSO;
    }

    public String getUpdatorSSO() {
        return updatorSSO;
    }

    public void setUpdatorSSO(String updatorSSO) {
        this.updatorSSO = updatorSSO;
    }


}

public class CompItemSpec {
    private int id;
    private int compId;
    private int contentTypeId;
    private char inListFlag;
    private char staticFlag;
    private String label;
    private String ext1Name;
    private String ext1Value;
    private String ext2Name;
    private String ext2Value;
    private String ext3Name;
    private String ext3Value;
    private String ext4Name;
    private String ext4Value;
    private String ext5Name;
    private String ext5Value;
    private ComponentSpec componentSpec;

    public int getCompId() {
        return compId;
    }

    public void setCompId(int compId) {
        this.compId = compId;
    }

    public String getExt1Name() {
        return ext1Name;
    }

    public void setExt1Name(String ext1Name) {
        this.ext1Name = ext1Name;
    }

    public String getExt1Value() {
        return ext1Value;
    }

    public void setExt1Value(String ext1Value) {
        this.ext1Value = ext1Value;
    }

    public String getExt2Name() {
        return ext2Name;
    }

    public void setExt2Name(String ext2Name) {
        this.ext2Name = ext2Name;
    }

    public String getExt2Value() {
        return ext2Value;
    }

    public void setExt2Value(String ext2Value) {
        this.ext2Value = ext2Value;
    }

    public String getExt3Name() {
        return ext3Name;
    }

    public void setExt3Name(String ext3Name) {
        this.ext3Name = ext3Name;
    }

    public String getExt3Value() {
        return ext3Value;
    }

    public void setExt3Value(String ext3Value) {
        this.ext3Value = ext3Value;
    }

    public String getExt4Name() {
        return ext4Name;
    }

    public void setExt4Name(String ext4Name) {
        this.ext4Name = ext4Name;
    }

    public String getExt4Value() {
        return ext4Value;
    }

    public void setExt4Value(String ext4Value) {
        this.ext4Value = ext4Value;
    }

    public String getExt5Name() {
        return ext5Name;
    }

    public void setExt5Name(String ext5Name) {
        this.ext5Name = ext5Name;
    }

    public String getExt5Value() {
        return ext5Value;
    }

    public void setExt5Value(String ext5Value) {
        this.ext5Value = ext5Value;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getContentTypeId() {
        return contentTypeId;
    }

    public void setContentTypeId(int contentTypeId) {
        this.contentTypeId = contentTypeId;
    }

    public char getInListFlag() {
        return inListFlag;
    }

    public void setInListFlag(char inListFlag) {
        this.inListFlag = inListFlag;
    }

    public char getStaticFlag() {
        return staticFlag;
    }

    public void setStaticFlag(char staticFlag) {
        this.staticFlag = staticFlag;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public ComponentSpec getComponentSpec() {
        return componentSpec;
    }

    public void setComponentSpec(ComponentSpec componentSpec) {
        this.componentSpec = componentSpec;
    }
}
import java.util.ArrayList;
导入java.util.Date;
导入java.util.List;
公共类组件规范{
私有int-id;
私有字符串名称;
私有int showLatestNum;
私有字符串moreLinkUrl;
私有字符moreLinkFlag;
私有字符openInNewFlag;
私人日期;
私人日期更新日期;
私有字符串创建者SO;
私有字符串更新器SO;
private List staticItemSpecList=new ArrayList();
私有列表dynamicItemSpecList=new ArrayList();
公共日期getCreateDate(){
返回创建日期;
}
公共无效setCreateDate(日期createDate){
this.createDate=createDate;
}
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公共字符getMoreLinkFlag(){
返回moreLinkFlag;
}
公共无效setMoreLinkFlag(char moreLinkFlag){
this.moreLinkFlag=moreLinkFlag;
}
公共字符串getMoreLinkUrl(){
返回moreLinkUrl;
}
public void setMoreLinkUrl(字符串moreLinkUrl){
this.moreLinkUrl=moreLinkUrl;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
public int getShowLatestNum(){
返回showLatestNum;
}
公共void setShowLatestNum(int showLatestNum){
this.showLatestNum=showLatestNum;
}
公共日期getUpdateDate(){
返回更新日期;
}
公共无效设置日期日期(日期更新日期){
this.updateDate=updateDate;
}
公共字符getOpenInNewFlag(){
返回openInNewFlag;
}
公共void setOpenInNewFlag(char openInNewFlag){
this.openInNewFlag=openInNewFlag;
}
公共列表getStaticItemSpecList(){
返回staticItemSpecList;
}
公共无效addStaticItemSpec(CompItemSpec compStaticItemSpec){
getStaticItemSpecList().add(compStaticItemSpec);
compStaticItemSpec.setComponentSpec(本);
}
公共列表getDynamicItemSpecList(){
返回dynamictemspeclist;
}
public void adddynamictemspec(CompItemSpec dynamictemspec){
getDynamicItemSpecList().add(dynamicItemSpec);
dynamicItemSpec.setComponentSpec(此);
}
公共字符串getCreatorSSO(){
归还创造者SO;
}
public void setCreatorSSO(字符串creatorSSO){
this.creatorSSO=creatorSSO;
}
公共字符串getUpdaterSSO(){
返回更新器SO;
}
public void setUpdatorSSO(字符串更新器){
this.updateorsso=updateorsso;
}
}
公共类CompItemSpec{
私有int-id;
私人公司;
私有int-contentTypeId;
私有字符标识;
私有字符标记;
私有字符串标签;
私有字符串ext1Name;
私有字符串ext1Value;
私有字符串ext2Name;
私有字符串ext2Value;
私有字符串ext3Name;
私有字符串ext3Value;
私有字符串ext4Name;
私有字符串ext4Value;
私有字符串ext5Name;
私有字符串ext5Value;
私有组件规范组件规范;
public int getCompId(){
返回compId;
}
公共无效setCompId(int compId){
this.compId=compId;
}
公共字符串getExt1Name(){
返回ext1Name;
}
public void setExt1Name(字符串ext1Name){
this.ext1Name=ext1Name;
}
公共字符串getExt1Value(){
返回ext1Value;
}
公共void setExt1Value(字符串ext1Value){
this.ext1Value=ext1Value;
}
公共字符串getExt2Name(){
返回ext2Name;
}
public void setExt2Name(字符串ext2Name){
this.ext2Name=ext2Name;
}
公共字符串getExt2Value(){
返回ext2Value;
}
公共void setExt2Value(字符串ext2Value){
this.ext2Value=ext2Value;
}
公共字符串getExt3Name(){
返回ext3Name;
}
public void setExt3Name(字符串ext3Name){
this.ext3Name=ext3Name;
}
公共字符串getExt3Value(){
返回ext3Value;
}
公共void setExt3Value(字符串ext3Value){
this.ext3Value=ext3Value;
}
公共字符串getExt4Name(){
返回ext4Name;
}
public void setExt4Name(字符串ext4Name){
this.ext4Name=ext4Name;
}
公共字符串getExt4Value(){
返回ext4Value;
}
公共void setExt4Value(字符串ext4Value){
this.ext4Value=ext4Value;
}
公共字符串getExt5Name(){
返回ext5Name;
}
public void setExt5Name(字符串ext5Name){
this.ext5Name=ext5Name;
}
公共字符串getExt5Value(){
返回ext5Value;
}
公共void setExt5Value(字符串ext5Value){
this.ext5Value=ext5Value;
}
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
public int getContentTypeId(){
返回contentTypeId;
}
public void setContentTypeId(int contentTypeId){
this.contentTypeId=contentTypeId;
}
公共字符getInListFlag(){
返回inListFlag;
}
public void setInListFlag(char inListFlag){
this.inListFlag=inListFlag;
}
公共字符getStaticFlag(){
返回静态标志;
}
public void setStaticFlag(字符staticFlag){
this.staticFlag=staticFlag;
}
公共字符串getLabel(){
退货标签;
}
公共空集合标签(字符串标签)
    <mapper namespace="com.ge.dao.ComponentSpecMapper">
<resultMap id="componentMap" type="componentSpec">
    <id column="id" property="id"/>
    <result column="temp_id" property="tempId"/>
    <result column="show_latest_num" property="showLatestNum"/>
    <result column="more_link_url" property="moreLinkUrl"/>
    <result column="more_link_flag" property="moreLinkFlag"/>
    <result column="open_in_new_flag" property="openInNewFlag"/>
    <result column="update_date" property="updateDate"/>
    <result column="create_date" property="createDate"/>
    <result column="updator_sso" property="updatorSSO"/>
    <result column="creator_sso" property="creatorSSO"/>
    <collection property="staticItemSpecList" ofType="itemSpec">
        <id column="static_item_id" property="id"/>
        <result column="id" property="compId"/>
        <result column="static_content_type_id" property="contentTypeId"/>
        <result column="static_label" property="label"/>
        <result column="static_ext1_value" property="ext1Value"/>
        <result column="static_ext2_value" property="ext2Value"/>
        <result column="static_ext3_value" property="ext3Value"/>
        <result column="static_ext4_value" property="ext4Value"/>
        <result column="static_ext5_value" property="ext5Value"/>
    </collection>
    <collection property="dynamicItemSpecList" ofType="itemSpec">
        <id column="dynamic_item_id" property="id"/>
        <result column="id" property="compId"/>
        <result column="dynamic_content_type_id" property="contentTypeId"/>
        <result column="dynamic_in_list_flag" property="inListFlag"/>
        <result column="dynamic_label" property="label"/>
        <result column="dynamic_ext1_value" property="ext1Value"/>
        <result column="dynamic_ext2_value" property="ext2Value"/>
        <result column="dynamic_ext3_value" property="ext3Value"/>
        <result column="dynamic_ext4_value" property="ext4Value"/>
        <result column="dynamic_ext5_value" property="ext5Value"/>
    </collection>

</resultMap>

<sql id="compSpecMain">
      SELECT
          comp.id,
          comp.name,
          show_latest_num,
          more_link_url,
          more_link_flag,
          open_in_new_flag,
          create_date,
          update_date,
          creator_sso,
          updator_sso,
          si.id static_item_id,
          si.content_type_id static_content_type_id,
          si.in_list_flag static_in_list_flag,
          si.label static_label,
          si.ext1_value static_ext1_value,
          si.ext2_value static_ext2_value,
          si.ext3_value static_ext3_value,
          si.ext4_value static_ext4_value,
          si.ext5_value static_ext5_value,
          di.id dynamic_item_id,
          di.content_type_id dynamic_content_type_id,
          di.in_list_flag dynamic_in_list_flag,
          di.label dynamic_label,
          di.ext1_value dynamic_ext1_value,
          di.ext2_value dynamic_ext2_value,
          di.ext3_value dynamic_ext3_value,
          di.ext4_value dynamic_ext4_value,
          di.ext5_value dynamic_ext5_value
        FROM components_spec comp
            LEFT JOIN comp_items_spec si ON si.comp_id=comp.id AND si.static_flag='Y'
            LEFT JOIN comp_items_spec di ON di.comp_id=comp.id AND di.static_flag='N'
</sql>

<select id="selectComponentSpecByID" parameterType="int" resultMap="componentMap">
        <include refid="compSpecMain"/>
        WHERE
            comp.id=#{id}
</select>

 <select id="selectComponentSpecByName" parameterType="string" resultMap="componentMap">
        <include refid="compSpecMain"/>
        WHERE
            comp.name like #{name}

</select>
 <resultMap id="componentMap" type="componentSpec">
        <id column="id" property="id"/>
        <result column="temp_id" property="tempId"/>
        <result column="show_latest_num" property="showLatestNum"/>
        <result column="more_link_url" property="moreLinkUrl"/>
        <result column="more_link_flag" property="moreLinkFlag"/>
        <result column="open_in_new_flag" property="openInNewFlag"/>
        <result column="update_date" property="updateDate"/>
        <result column="create_date" property="createDate"/>
        <result column="updator_sso" property="updatorSSO"/>
        <result column="creator_sso" property="creatorSSO"/>
        <collection property="staticItemSpecList" notNullColumn="static_item_id" ofType="itemSpec">
            <id column="static_item_id" property="id"/>
            <result column="id" property="compId"/>
            <result column="static_content_type_id" property="contentTypeId"/>
            <result column="static_label" property="label"/>
            <result column="static_ext1_value" property="ext1Value"/>
            <result column="static_ext2_value" property="ext2Value"/>
            <result column="static_ext3_value" property="ext3Value"/>
            <result column="static_ext4_value" property="ext4Value"/>
            <result column="static_ext5_value" property="ext5Value"/>
        </collection>
        <collection property="dynamicItemSpecList" notNullColumn="dynamic_item_id" ofType="itemSpec">
            <id column="dynamic_item_id" property="id"/>
            <result column="id" property="compId"/>
            <result column="dynamic_content_type_id" property="contentTypeId"/>
            <result column="dynamic_in_list_flag" property="inListFlag"/>
            <result column="dynamic_label" property="label"/>
            <result column="dynamic_ext1_value" property="ext1Value"/>
            <result column="dynamic_ext2_value" property="ext2Value"/>
            <result column="dynamic_ext3_value" property="ext3Value"/>
            <result column="dynamic_ext4_value" property="ext4Value"/>
            <result column="dynamic_ext5_value" property="ext5Value"/>
        </collection>

    </resultMap>