mybatis 一对多和一对一写法注意事项

  <resultMap id="ChartResultMap" type="com.qif.dsa.ucenter.planinfo.entity.ChartDate">
        <id column="time" jdbcType="VARCHAR" property="time"/>
        <collection property="planChartList"   javaType="java.util.List"
                    column="time" ofType="com.qif.dsa.ucenter.planinfo.entity.PlanChart" >
            <id column="time" jdbcType="VARCHAR" property="time" />

下面的i-State 是数据库里的字段, iState 则是展示到页面的字段
<result column="i_State" jdbcType="VARCHAR" property="iState" /> <result column="stateCount" jdbcType="INTEGER" property="stateCount" /> </collection> </resultMap>


以上写法用于一对多select..... leftjoin .....

 

 

 

  <resultMap id="BigTypeResultMap" type="com.qif.dsa.ucenter.bigtype.entity.BigType">
        <id column="BigTypeId" jdbcType="INTEGER" property="bigTypeId" />
        <result column="vc_Name" jdbcType="VARCHAR" property="vcName" />

      这里的BigTypeId 作为外键 
    querySmallTypeByBigId方法是另外一个dao层里面通过外键查到的数据 一次返回
<collection property="smallTypeList" column="BigTypeId" select="com.qif.dsa.ucenter.smallType.dao.SmallTypeDao.querySmallTypeByBigId"/> </resultMap>




以上写法用于一对多 select.....(包含外键)
原理:    一条不关联的sql 里面包含外键 
         当返回值为带有 select  的collection时,自动从别的表调用查询语句 返回整体结果                   

 

 

<resultMap id="HazardResultMap" type="com.qif.dsa.ucenter.hazardinfo.entity.HazardInfo">
        <id column="HazardID" jdbcType="INTEGER" property="hazardId" />
        <result column="RcuID" jdbcType="INTEGER" property="rcuId" />
        <result column="vc_HazardContent" jdbcType="VARCHAR" property="vcHazardContent" />
        <result column="vc_FillPeople" jdbcType="VARCHAR" property="vcFillPeople" />
        <association property="rcuInfo" javaType="com.qif.dsa.ucenter.rcuinfo.entity.RcuInfo" columnPrefix="rcu_">
            <id property="rcuId" jdbcType="INTEGER" column="RcuID"/>
            <result property="orgId" jdbcType="INTEGER" column="OrgID"/>
            <result property="vcName" jdbcType="VARCHAR"  column="vcName"/>
        </association>
    </resultMap>


以上写法用于一对一  select..... innerjoin   .....  
 

 

posted @ 2019-07-11 16:43  MagicAsa  阅读(693)  评论(0编辑  收藏  举报