mybatis实现两张表的连接查询(一对多)
来源:互联网 发布:阿里云专有网络配置 编辑:程序博客网 时间:2024/06/05 06:16
有时候我们需要从一对多查询(既查询到地区的编号之后再查询到属于这个地区的所有歌手)
这个是我配置的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>
这个是我配置的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>
阅读全文
0 0
- mybatis实现两张表的连接查询(一对多)
- mybatis自连接查询子集合(一对多查询)
- MyBatis 3中实现一对多的插入和查询
- Mybatis关联查询一对一和一对多的实现
- Mybatis关联查询一对一和一对多的实现
- Mybatis关联查询一对一和一对多的实现
- Mybatis关联查询一对一和一对多的实现
- Mybatis关联查询一对一和一对多的实现
- Mybatis关联查询一对一和一对多的实现
- mybatis实现两张表的连接查询(多对一)
- Mybatis实现关联表查询(一对多关联)
- mybatis中实现一对一,一对多查询
- 使用Mybatis实现数据库一对多查询
- mybatis查询(一对多查询)
- MyBatis--关联关系查询--一对多--多表连接查询
- MyBatis(三)---实现一对一和一对多的关联表查询
- SSM搭建-Mybatis关联查询一对一和一对多的实现(19)
- 22、SSM框架-Mybatis关联查询一对一和一对多的实现(4)
- swap分区
- .NET轻量级ORM组件Dapper修炼手册
- Python进阶之路—四
- Mybatis逐步深入
- OC -基础(六) 学习中。。。
- mybatis实现两张表的连接查询(一对多)
- 高效 Linux 4 文件相关操作
- java中的泛型方法
- HashMap遍历的两种方式
- Hopscotch(dfs)
- .NET轻量级ORM组件Dapper葵花宝典
- 能用10块大洋去解决的事,千万不要用时间
- PowerDesigner设置
- Bellovin