先看如下链接博客,再来观看本篇文章,两者对比,可能会有不一样的感受。
原文链接:https://blog.youkuaiyun.com/2301_79965602/article/details/136605149
实现:根据套餐id查询套餐详情接口
在套餐详情页面需要展示当前套餐的信息(包括图片、套餐名称、套餐介绍、适用性别、适用年龄)、此套餐包含的检查组信息、检查组包含的检查项信息等。
此时就涉及到了多张表,我们就可以利用mybatis的多表联查来解决!
1、首先,根据套餐id查询套餐
<?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.itheima.dao.UserSetmealDao">
<resultMap id="baseResultMap" type="com.itheima.pojo.Setmeal">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="code" property="code"/>
<result column="helpCode" property="helpCode"/>
<result column="sex" property="sex"/>
<result column="age" property="age"/>
<result column="price" property="price"/>
<result column="remark" property="remark"/>
<result column="attention" property="attention"/>
<result column="img" property="img"/>
</resultMap>
<resultMap id="findByIdResultMap" type="com.itheima.pojo.Setmeal" extends="baseResultMap">
<!--套餐和检查组多对多映射-->
<collection
property="checkGroups"
ofType="com.itheima.pojo.CheckGroup"
select="com.itheima.dao.UserCheckGroupDao.findCheckGroupById"
column="id">
</collection>
</resultMap>
<!--根据套餐id查询套餐详情-->
<select id="findById" parameterType="int" resultMap="findByIdResultMap">
SELECT * FROM t_setmeal WHERE id = #{id}
</select>
</mapper>
2、然后再根据套餐id查询关联的检查组信息(注意关联的名称不要弄错)
<?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.itheima.dao.UserCheckGroupDao">
<resultMap id="baseResultMap" type="com.itheima.pojo.CheckGroup">
<id column="id" property="id"/>
<result column="code" property="code"/>
<result column="name" property="name"/>
<result column="helpCode" property="helpCode"/>
<result column="sex" property="sex"/>
<result column="remark" property="remark"/>
<result column="attention" property="attention"/>
</resultMap>
<resultMap id="findByIdResultMap" type="com.itheima.pojo.CheckGroup" extends="baseResultMap">
<!--检查项和检查组多对多映射-->
<collection property="checkItems"
ofType="com.itheima.pojo.CheckItem"
column="id"
select="com.itheima.dao.UserCheckItemDao.findCheckItemById">
</collection>
</resultMap>
<!--根据套餐id查询关联的检查组详情-->
<select id="findCheckGroupById" parameterType="int" resultMap="findByIdResultMap">
SELECT * FROM t_checkgroup WHERE id IN (SELECT checkgroup_id FROM t_setmeal_checkgroup WHERE setmeal_id = #{id})
</select>
</mapper>
3、最后,根据检查组id查询关联的检查项详情
<?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.itheima.dao.UserCheckItemDao">
<!--根据检查组id查询关联的检查项详情-->
<select id="findCheckItemById" parameterType="int" resultType="com.itheima.pojo.CheckItem">
SELECT * FROM t_checkitem WHERE id IN (SELECT checkitem_id FROM t_checkgroup_checkitem WHERE checkgroup_id = #{id})
</select>
</mapper>
就这三步,就搞定啦!