mybatis实现两张表的连接查询(一对多)

有时候我们需要从一对多查询(既查询到地区的编号之后再查询到属于这个地区的所有歌手)


这个是我配置的xml文件SingerClassifyMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">


<mapper namespace="com.myspotlight.entity.SingerClassifyMapper">
    
<!-- 这里面有几个问题要注意:第一点就是各种type的是否是你所需要的类型 不能将SingerClassify和Singer捣乱
第二个因为这是从一端到多端查询,因此要用collection column="s_type"是外键   -->
   <resultMap type="com.myspotlight.entity.SingerClassify" id="cidTosingerResultMap">
            <id property="cId" column="c_id" />
<result property="cName" column="c_name" />

<collection property="singerAll" ofType="com.myspotlight.entity.Singer" column="s_type">
   <id property="sId" column="s_id" />
<result property="sName" column="s_name" />
<result property="sSex" column="s_sex" />
<result property="sBirthday" column="s_birthday" />
<result property="sHistory" column="s_history" />
<result property="sAchievement" column="s_achievement" />
<result property="sMessage" column="s_message" />
<result property="sRemark" column="s_remark" />
</collection>
</resultMap>
<!-- 这个是从歌手类型查到属于这个类型的所有歌手信息 (一对多) -->
<select id="selectCidToSinger" resultMap="cidTosingerResultMap">
SELECT
s.s_id,
s.s_name,
s.s_sex,
s.s_birthday,
s.s_history,
s.s_achievement,
s.s_message,
s.s_remark,
c.c_id,
c.c_name
FROM
myspotlight_singer s
JOIN myspotlight_classify c ON s.s_type = c.c_id WHERE c_id=#{cId}
</select>


</mapper>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值