mybatis 多表关联mapper文件写法

本文详细介绍了如何在MyBatis中实现SystemParam表与Suit表的多对一关联查询,包括自连接查询的实现方式,以及如何通过resultMap进行结果映射。文章深入解析了Mapper方法、PO类设计、resultMap配置和SQL语句的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

两张表SystemParam(系统参数表) Suit (主题)
SystemParam 与 Suit 是多对一
Suit 的higerSuit字段是Suit 的父及主题id 是多对一,需要自连接查询,因为重名所以父表sql字段加别名

mapper方法

Systemparam selectJoinSuit(String strparamcode);

Po类

public class Systemparam {
    //ManyToOne "主题"
    private Suit suitobj;
    private String strparamcode;
    private String strenable;
    private String strparamname;
    //suit表主键
    private String suit;
    private String strparamvalue;
}

 

public class Suit {
    //ManyToOne
    private Suit suit;
    //主键
    private String strsuitcode;
    private String strsuitname;
    //父级id
    private String higersuit;
}

 

resultMap的写法

<resultMap id="BaseResultMap" type="net.transino.model.Systemparam" >
  <id column="strParamCode" property="strparamcode" jdbcType="VARCHAR" />
  <result column="strEnable" property="strenable" jdbcType="VARCHAR" />
  <result column="strParamName" property="strparamname" jdbcType="VARCHAR" />
  <result column="suit" property="suit" jdbcType="VARCHAR" />
</resultMap>
resultMap 使用extends 继承上级map
<resultMap id="ResultMapWithBLOBs" type="net.transino.model.Systemparam" extends="BaseResultMap" >
  <result column="strParamValue" property="strparamvalue" jdbcType="LONGVARCHAR" />
</resultMap>
<resultMap id="JoinsuitMap" type="net.transino.model.Systemparam" extends="ResultMapWithBLOBs" >
  <association property="suitobj" javaType="Suit">
    <id column="strSuitCode" property="strsuitcode" jdbcType="VARCHAR" />
    <result column="strSuitName" property="strsuitname" jdbcType="VARCHAR" />
    <result column="higerSuit" property="higersuit" jdbcType="VARCHAR" />
    <association property="suit" javaType="Suit">
      <id column="pstrSuitCode" property="strsuitcode" jdbcType="VARCHAR" />
      <result column="pstrSuitName" property="strsuitname" jdbcType="VARCHAR" />
      <result column="phigerSuit" property="higersuit" jdbcType="VARCHAR" />
    </association>
  </association>
</resultMap>

select写法
<select id="selectJoinSuit" resultMap="JoinsuitMap" parameterType="java.lang.String">
  select
  systempara0_.*,
  suit1_.*,
  suit2_.strSuitCode pstrSuitCode,
  suit2_.strSuitName pstrSuitName,
  suit2_.higerSuit phigerSuit
  from SystemParam systempara0_
  LEFT OUTER JOIN
  Suit suit1_
  ON systempara0_.suit=suit1_.strSuitCode
  LEFT OUTER JOIN
  Suit suit2_
  ON suit1_.higerSuit=suit2_.strSuitCode
  WHERE
  systempara0_.strParamCode=#{strparamcode,jdbcType=VARCHAR}
</select>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值