mybatis in关键字查询边学mapper.xml

1.编写接口

 //根据套餐ID查询套餐详情(套餐基本信息、套餐对应的检查组信息、检查组对应的检查项信息)
    public Setmeal findById(int id) {
        return setmealDao.findById(id);
    }

2.mabatis编写

    <!--根据套餐ID查询套餐详情(包含套餐基本信息、检查组信息、检查项信息)-->
    <select id="findById" parameterType="int" resultMap="findByIdResultMap">
        select * from t_setmeal where id = #{id}
    </select>

3.SetmealDao.xml编写findByIdResultMap
3.1实体类

public class Setmeal implements Serializable {
    private Integer id;
    private String name;
    private String code;
    private String helpCode;
    private String sex;//套餐适用性别:0不限 1男 2女
    private String age;//套餐适用年龄
    private Float price;//套餐价格
    private String remark;
    private String attention;
    private String img;//套餐对应图片存储路径
    private List<CheckGroup> checkGroups;//体检套餐对应的检查组,多对多关系
  <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"  //参数
                javaType="java.util.ArrayList" //参数类型
                ofType="com.itheima.pojo.CheckGroup" //对应实体类名称
                select="com.itheima.dao.CheckGroupDao.findCheckGroupById"//对应CheckGroupDao.xml下的findCheckGroupById方法
                column="id"//传递的参数
                >
        </collection>
    </resultMap>

4.CheckGroupDao.xml编写findCheckGroupById
4.1实体类

public class CheckGroup implements Serializable {
    private Integer id;//主键
    private String code;//编码
    private String name;//名称
    private String helpCode;//助记
    private String sex;//适用性别
    private String remark;//介绍
    private String attention;//注意事项
    private List<CheckItem> checkItems;//一个检查组合包含多个检查项

4.2编写findCheckGroupById

    <resultMap id="baseResultMap" type="com.itheima.pojo.CheckGroup">
        <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="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"
                      javaType="java.util.ArrayList" //参数类型
                     column="id"
                     select="com.itheima.dao.CheckItemDao.findCheckItemById"//对应CheckItemDao.xml 下面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 = #{setmeal_id})
    </select>

5编写CheckItemDao.xml

    <!--根据检查组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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值