首页 > 其他分享 >Mybatis的三种方式秒杀多对一的映射关系

Mybatis的三种方式秒杀多对一的映射关系

时间:2022-08-23 20:46:09浏览次数:63  
标签:查询 映射 stId class 秒杀 Mybatis st id

处理多对一的映射关系

1、使用级联 处理多对一的映射关系

  <resultMap id="StResultMap" type="St">
      <id column="st_id" property="stId"></id>
      <result column="st_name" property="stName"></result>
      <result column="age" property="age"></result>
      <result column="gender" property="gender"></result>
      <!--确认好级联属性在数据库中的字段名字(column),以及在Java实体类中对应的字段的名字(property)-->
      <result column="class_id" property="class.classId"></result>
      <result column="class_name" property="class.className"></result>
  </resultMap>
  <select id="getStAndClassByStId" resultMap="StResultMap">
      SELECT
          t_st.st_id,
          t_st.st_name,
          t_st.age,
          t_st.gender,
          t_class.class_id,
          t_class.class_name
      FROM
          t_st
              LEFT JOIN t_class ON t_st.class_id = t_class.class_id
      where t_st.st_id = #{stId}
  </select>

2、使用association标签 处理多对一的映射关系

 <resultMap id="StResultMapA" type="St">
        <id column="st_id" property="stId"></id>
        <result column="st_name" property="stName"></result>
        <result column="age" property="age"></result>
        <result column="gender" property="gender"></result>
        <!--专门处理多对一的映射关系association,(处理实体类类型的属性)
        1、property:设置需要处理映射关系的属性的属性名 :class
        2、javaType:设置要处理的属性的类型 : Class
        -->
        <association property="class" javaType="Class">
            <id column="class_id" property="classId"></id>
            <result column="class_name" property="className"></result>
        </association>
    </resultMap>
<select id="getStAndClassByStId" resultMap="StResultMapA">
        SELECT
            t_st.st_id,
            t_st.st_name,
            t_st.age,
            t_st.gender,
            t_class.class_id,
            t_class.class_name
        FROM
            t_st
                LEFT JOIN t_class ON t_st.class_id = t_class.class_id
        where t_st.st_id = #{stId}
    </select>

3、使用分步查询 处理多对一的映射关系

① 学生查询的接口类
public interface StMapper {

    //查询员工以及所对应的部门信息,通过分步查询
    St getStAndClassByStepOne(@Param("stId")Integer stId);

}

② 查询的xml文件,采用分步查询
<resultMap id="getStAndClassByStepOne" type="St">
        <id column="st_id" property="stId"></id>
        <result column="st_name" property="stName"></result>
        <result column="age" property="age"></result>
        <result column="gender" property="gender"></result>
        <!--
        property:设置需要处理映射关系的属性的属性名
        select:设置分步查询的sql的唯一标识
        column:将查询出来的某个字段作置分步查询的sql的条件  
        -->
        <association property="class"
                     select="com.xmut.mybatis.mapper.ClassMapper.getStAndClassByStepTwo"
                     column="class_id"></association>
    </resultMap>
    <select id="getStAndClassByStepOne" resultMap="getStAndClassByStepOne">
        select * from t_st where st_id = #{stId};
    </select>
③班级查询接口
public interface ClassMapper {

    //分步查询第二步
    Class getStAndClassByStepTwo(@Param("stId")Integer stId);

}
④班级查询的xml文件
<select id="getStAndClassByStepTwo" resultType="Class">
        select * from t_class where class_id = #{classId}
</select>

OK!就这么多,睡大觉!!!

标签:查询,映射,stId,class,秒杀,Mybatis,st,id
From: https://www.cnblogs.com/Lake-Chen/p/16615665.html

相关文章

  • 自定义Mybatis拦截器实现自动添加创建人修改人等公共字段
    摘要本文通过自定义Mybatis拦截器拦截Executor接口实现在插入和修改操作时自动添加创建人修改人等公共字段,话不多说,直接上代码定义Mybatis拦截器packagecom.syb.spring......
  • MybatisPlus属性自动填充
    阿里巴巴开发规范,对于每一张表都因该有id(主键),createTime(创建时间),updateTime(修改时间)这三个字段主键ID我们可以使用自增,或者雪花算法创建时间修改时间我们可以使用数......
  • springboot Failed to bind properties under 'mybatis-plus.configuration' to com.b
    启动springboot时出现错误Failedtobindpropertiesunder'mybatis-plus.configuration'tocom.baomidou.mybatisplus.core.MybatisConfiguration是因为配置文件.yml......
  • Java精进-20分钟学会mybatis使用
    文字分享希望现在的你无论有明确具体的目标还是没有,都能重视自己的需求和目标,并且常常回顾,或许可以找一个你习惯的方式写出来,挂在哪里,电脑或日记本都好。当你疲惫或迷茫的......
  • Mybatis
    什么是MyBatis?MyBatis是一款优秀的持久层框架,它支持自定义SQL、存储过程以及高级映射。MyBatis免除了几乎所有的JDBC代码以及设置参数和获取结果集的工作。MyBatis......
  • mybatis 配置文件mybatis.xml的加载过程
    mybatis配置文件的整体加载过程mybatis几乎所有的用户相关的操作都是再SqlSession上进行的,儿sqlSession是由SqlSessionFactory调用openSession方法创建的.正常情况下......
  • springboot整合mybatis-plus实现增删改查功能
    一、创建数据库字段名称中文类型长度主键自增默认值备注Id Int Y   emp_name员工姓名varchar ......
  • Mybatis下@Select注解下使用like模糊查询
    Mybatis下@Select注解使用模糊查询要使用concat方法拼接%百分号和关键词,案例如下:packagecom.xzit.mapper;importcom.xzit.entity.Emp;importorg.apache.ibatis.ann......
  • MybatisPlus系列---【时间查询】
    1.问题描述项目中经常遇到这样的问题,有个查询条件是日期,或者日期范围,但是数据库一般存的是日期时间,想要查询,肯定要做格式化后再比较。不使用MybatisPlus的时候,一般都......
  • ES之动态映射
    1.定义与关系型数据库不同的是其不需要先定义表结构,而可以根据写入文档的内容,来推断字段和数据类型,创建索引结构,这就是dynamicmapping,动态映射的由来。有时这是想要的......