(外键)mybatis的sqlmapper配置文件中处理外键提供两种解决方案

现在有两张表professional专业表和classes班级表,专业表中的proid作为班级表的外键存在。
professional:

public class Professional {
    private int proid;
    private String proname;
    private String prodesc;
    private Date createdate;

classes:

public class Classes {
    private int clsid;
    private String clsname;
    private String clsdesc;
    private Professional pro;
    private Date createdate;
    private Date enddate;

第一种解决方案:

 <resultMap type="Classes" id="ClsResult">
    <id column="clsid"  property="clsid"/>
    <result column="clsname"  property="clsname"/>
    <result column="clsdesc"  property="clsdesc"/>
    <result column="createdate" property="createdate"/>
    <result column="enddate" property="enddate"/>
   <association column="proid" property="pro" javaType="Professional" select="com.oa.dao.professionalDao.selProById"></association>
  </resultMap>
  <select id="selClsById" parameterType="int" resultMap="ClsResult">
       SELECT * FROM classes WHERE clsid = #{id}
  </select>

第二种解决方案(表连接查询):
如果采用表连接的方式进行查询,那么resultmap中每一个列名column和属性property都必须要指定

<resultMap type="Classes" id="ClsResult">
    <id column="clsid"  property="clsid"/>
    <result column="clsname"  property="clsname"/>
    <result column="clsdesc"  property="clsdesc"/>
    <result column="createdate" property="createdate"/>
    <result column="enddate" property="enddate"/>

  <association property="pro" javaType="Professional">
    <result column="proid" property="proid"/>
    <result column="proname" property="proname"/>
    <result column="prodesc" property="prodesc"/>
    <result column="pcreatedate" property="createdate"/>
  </association>
  </resultMap>

  <select id="selClsById" parameterType="int" resultMap="ClsResult">
    select classes.*,professional.proname,professional.prodesc,professional.createdate as pcreatedate from classes INNER JOIN professional on classes.proid = professional.proid where classes.clsid = 1 
  </select>
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值