有一个主表对象,从表作为它的明细属性.
怎样在查主表对象时,把它的明细也带出来?

解决方案 »

  1.   

    需要在XML文件里配置的.等下给你发代码
      

  2.   

    用datarelation 可以解决
    lz可以找找相关实例
      

  3.   

    <?xml version="1.0" encoding="utf-8" ?>
    <sqlMap namespace="Service.BO" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <alias>
    <typeAlias alias="Role" type="Service.BO.Role, Service" />
    </alias>
    <resultMaps>
    <resultMap id="RoleResult" class="Role">
    <result property="Id" column="Id" />
    <result property="Name" column="Name" />
    <result property="Description" column="Description" />
    <result property ="Popedom" select="SelectPopedomByRoleId" column="Id"/>
    <result property ="Users" lazyLoad="true" select="SelectUsersByRoleId" column="Id"/>
    </resultMap>
    </resultMaps>
    <statements>
    <select id="SelectRole" parameterClass="string" resultMap="RoleResult">
    select Id, Name, Description
    from Role
    <dynamic prepend="WHERE">
    <isParameterPresent>
    Id = #value#
    </isParameterPresent>
    </dynamic>
    </select>
    <select id="SelectRoleByRoleName" parameterClass="string" resultMap="RoleResult">
    select Id, Name, Description
    from Role
    <dynamic prepend="WHERE">
    <isParameterPresent>
    Name = #value#
    </isParameterPresent>
    </dynamic>
    </select>
    <select id="SelectPopedomByRoleId" parameterClass="string" resultClass="Service.BO.Popedom">
    select p.Code, p.PopedomName, p.Description, p.ParentCode,p.Flag
    from Popedom p,RolePopedom rp
    where rp.PopedomCode = p.Code and
    rp.RoleId = #Id#
    </select>
    <select id="SelectUsersByRoleId" parameterClass="string" resultClass="Service.BO.UserInfo">
    select ui.Id, Name, Password, ShortName, Sex, BirthDate, Duty, OfficialPhone, Fax, MobilePhone, Email, Sequence, IsDirector, Re, Flag, DepartmentCode, InfoCheckedNumber
    from UserInfo ui, RoleUser ru
    where ru.UserId = ui.Id and
    ru.RoleId = #Id#
    order by ui.DepartmentCode,ui.Sequence
    </select>
    <select id="SelectAllRole" resultMap="RoleResult">
    select Id, Name, Description
    from Role
    </select>
    <insert id="InsertRole" parameterClass="Role">
    insert into Role (Id, Name, Description)
    values
    (#Id#, #Name#, #Description#)
    </insert>
    <update id="UpdateRole" parameterClass="Role">
    update Role set
    Name = #Name#,
    Description = #Description#
    where Id = #Id#
    </update>
    <delete id="DeleteRole" parameterClass="string">
    delete from Role
    where Id = #value#
    </delete>
    </statements>
    </sqlMap>Role表
      

  4.   


    <?xml version="1.0" encoding="utf-8" ?>
    <sqlMap namespace="Service.BO" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <alias>
    <typeAlias alias="UserInfo" type="Service.BO.UserInfo, Service" />
    </alias>
    <resultMaps>
    <resultMap id="UserInfoResult" class="UserInfo">
    <result property="Id" column="Id" ></result>
    <result property="Name" column="Name"  ></result>
    <result property="Password" column="Password"  ></result>
    <result property="LogOnName" column="LogOnName"  ></result>
    <result property="Sex" column="Sex"  ></result>
    <result property="BirthDate" column="BirthDate"  ></result>
    <result property="Duty" column="Duty"  ></result>
    <result property="OfficialPhone" column="OfficialPhone"  ></result>
    <result property="Fax" column="Fax"  ></result>
    <result property="MobilePhone" column="MobilePhone" ></result>
    <result property="Email" column="Email"  ></result>
    <result property="Sequence" column="Sequence"  ></result>
    <result property="IsDirector" column="IsDirector"  ></result>
    <result property="Re" column="Re"  ></result>
    <result property="Flag" column="Flag" ></result>
    <result property="DepartmentCode" column="DepartmentCode" />
    <result property ="Popedom" lazyLoad="true" select="SelectPopedomById" column="Id" ></result>
    </resultMap>
    </resultMaps>
    <statements>
    <select id="SelectUserInfo" parameterClass="string" resultMap="UserInfoResult">
    select Id, Name, Password, LogOnName, Sex, BirthDate, Duty, OfficialPhone, Fax, MobilePhone, Email, Sequence, IsDirector, Re, Flag, DepartmentCode<!--, InfoCheckedNumber-->
    from UserInfo
    <dynamic prepend="WHERE">
    <isParameterPresent>
    Id = #value#
    </isParameterPresent>
    </dynamic>
    </select>
    <select id="SelectUserInfoByLoginName" parameterClass="string" resultMap="UserInfoResult">
    select Id, Name, Password, LogOnName, Sex, BirthDate, Duty, OfficialPhone, Fax, MobilePhone, Email, Sequence, IsDirector, Re, Flag, DepartmentCode<!--, InfoCheckedNumber-->
    from UserInfo
    <dynamic prepend="WHERE">
    <isParameterPresent>
    LogOnName = #value#
    </isParameterPresent>
    </dynamic>
    </select>
    <select id="SelectAllUserInfo" resultMap="UserInfoResult">
    select Id, Name, Password, LogOnName, Sex, BirthDate, Duty, OfficialPhone, Fax, MobilePhone, Email, Sequence, IsDirector, Re, Flag, DepartmentCode<!--, InfoCheckedNumber-->
    from UserInfo
    order by DepartmentCode,Sequence
    </select>
    <select id="SelectUserByDepCode" parameterClass="string" resultMap="UserInfoResult">
    select Id, Name, Password, LogOnName, Sex, BirthDate, Duty, OfficialPhone, Fax, MobilePhone, Email, Sequence, IsDirector, Re, Flag, DepartmentCode<!--, InfoCheckedNumber-->
    from UserInfo
    <dynamic prepend="WHERE">
    <isParameterPresent>
    DepartmentCode = #value#
    </isParameterPresent>
    </dynamic>
    order by Sequence
    </select>
    <select id="SelectChildUserByCode" parameterClass="string" resultMap="UserInfoResult">
    select Id, Name, Password, LogOnName, Sex, BirthDate, Duty, OfficialPhone, Fax, MobilePhone, Email, Sequence, IsDirector, Re, Flag, DepartmentCode<!--, InfoCheckedNumber-->
    from UserInfo
    <dynamic prepend="WHERE">
    <isParameterPresent>
    DepartmentCode like #value#
    </isParameterPresent>
    </dynamic>
    order by Sequence
    </select>
    <select id="SelectPopedomById" parameterClass="string" resultClass="Service.BO.Popedom">
    select distinct Popedom.PopedomName,Popedom.Description,Popedom.Code
    from Popedom, RolePopedom, RoleUser, UserInfo
    where UserInfo.ID = RoleUser.UserID and RoleUser.RoleID = RolePopedom.RoleID and RolePopedom.PopedomCode = Popedom.Code and UserInfo.ID=#Id#
    </select>
    <insert id="InsertUserInfo" parameterClass="UserInfo">
    insert into UserInfo (Id, Name, [Password], LogOnName, Sex, BirthDate, Duty, OfficialPhone, Fax, MobilePhone, Email, Sequence, IsDirector, Re, Flag, DepartmentCode)
    values(#Id#, #Name#, #Password#, #LogOnName#, #Sex#, #BirthDate#, #Duty#, #OfficialPhone#, #Fax#, #MobilePhone#, #Email#, #Sequence#, #IsDirector#, #Re#, #Flag#, #DepartmentCode#)
    </insert>
    <update id="UpdateUserInfo" parameterClass="UserInfo">
    update UserInfo set
    Name = #Name#,
    [Password] = #Password#,
    LogOnName = #LogOnName#,
    Sex = #Sex#,
    BirthDate = #BirthDate#,
    Duty = #Duty#,
    OfficialPhone = #OfficialPhone#,
    Fax = #Fax#,
    MobilePhone = #MobilePhone#,
    Email = #Email#,
    Sequence = #Sequence#,
    IsDirector = #IsDirector#,
    Re = #Re#,
    Flag = #Flag#,
    DepartmentCode = #DepartmentCode#
    where Id = #Id#
    </update>
    <delete id="DeleteUserInfo" parameterClass="string">
    delete from UserInfo
    where Id = #value#
    </delete>
    </statements>
    </sqlMap>
    UserInfo表这里面有这两者的关系.