mybatis进阶:一对一查询

一对一查询

一对一查询。自己的主键对应别的表外键。连表查询

1、联合查询

<!--一对一 联合查询-->
  <select id="getIntegralInfoById" resultMap="ClassesResultMap">
    select ua.id uid,ua.*,pi.* from pms_integration pi,ums_admin ua
    where pi.member_id = ua.id
    and pi.id = #{id}
  </select>

  <resultMap type="com.ljs.integralmall.model.PmsIntegration" id="ClassesResultMap">
    <!-- 实体类的字段名和数据表的字段名映射 -->
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="member_id" jdbcType="BIGINT" property="memberId" />
    <result column="integration" jdbcType="BIGINT" property="integration" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="create_by" jdbcType="VARCHAR" property="createBy" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="update_by" jdbcType="VARCHAR" property="updateBy" />
    <result column="review_status" jdbcType="VARCHAR" property="reviewStatus" />
    <association property="umsAdmin"
                 javaType="com.ljs.integralmall.model.UmsAdmin">
      <result column="uid" jdbcType="BIGINT" property="id"/>
      <result column="username" jdbcType="VARCHAR" property="username" />
      <result column="password" jdbcType="VARCHAR" property="password" />
      <result column="icon" jdbcType="VARCHAR" property="icon" />
      <result column="email" jdbcType="VARCHAR" property="email" />
      <result column="nick_name" jdbcType="VARCHAR" property="nickName" />
      <result column="note" jdbcType="VARCHAR" property="note" />
      <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
      <result column="login_time" jdbcType="TIMESTAMP" property="loginTime" />
      <result column="status" jdbcType="INTEGER" property="status" />
    </association>
  </resultMap>

####因为两表的id数据库字段一致。不好区分。则这里umsAdmin表id指定为uid
查询sql语句则以别名方式查询即可

2、嵌套查询

将别的表数据查询出来。放回现表进行连表查询

<!--一对一 嵌套查询-->
  <select id="getIntegralInfoById2" resultMap="getIntegrationMap">
    select * from pms_integration pi
    left join ums_admin ua on pi.member_id = ua.id
    where pi.id = #{id}
  </select>

  <resultMap id="getIntegrationMap" type="com.ljs.integralmall.model.PmsIntegration">
    <association property="umsAdmin" javaType="com.ljs.integralmall.model.UmsAdmin"
    select="com.ljs.integralmall.mapper.UmsAdminMapper.selectByPrimaryKey"
                 column="{id=member_id}"/>
  </resultMap>

UserMapper.xml

  <select id="selectByPrimaryKey"  resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from ums_admin
    where id = #{id,jdbcType=BIGINT}
  </select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值