[MybatisReLearning]03.24Mybatis(特殊SQL的执行+多对一一对多)

MyBatis

特殊SQL的执行

  1. 模糊查询
    image

  2. 批量删除
    image

  3. 动态设置表名
    image

添加功能获取自增的主键

  • 使用的场景为:
    • t_class(class_id,class_name)
    • t-student(student_id,student_name,class_id)
      1. 添加班级信息
      2. 获取新添加的班级的id
      3. 为班级分配学生,即将某学生的班级id改为新添加的班级id
  • 在mapper.xml中设置两个属性
    • useGeneratedKeys:使用自增的主键
    • keyProperty:因为增删改查有统一的返回值是收影响的行数,因此只能将获取的自增的主键放在创术的参数user对象的某个属性中

image

自定义映射resultMap处理字段和属性的映射关系

  • resultMap:设置自定义映射

    • 属性:
      • id:表示自定义映射的唯一标识,不能重复
      • type:查询的数据要映射的实体类的类型
    • 子标签
      • id:设置主键的映射关系
      • 设置普通字段的映射关系
      • 子标签属性
        • property:设置映射关系中实体类中的属性名
        • column:设置映射关系中表中的字段名
  • 若字段名和实体类中的属性名不一致时,可以通过resultMap来设置自定义映射,既使字段名和属性名一致的属性也要设置映射,全部都需要写出来
    image

  • 或者去mybatis-config.xml中配置(可以在查询表中数据的时候将_类型的字段名字转换为驼峰)
    具体设置:<setting name="mapUnderscoreToCamelCase" value="true"/>

多对一映射处理

查询员工信息一级员工所对应的部门信息

@Data
public class Emp {

    private Integer eid;

    private String empName;

    private Integer age;

    private String sex;

    private String email;

    private Dept dept;
}
  1. 级联方式处理映射关系
    image

  2. 使用association处理关系映射

  • association:处理多对一的映射关系
  • property:需要处理多对一的映射关系的属性名
  • javaType:该属性的类型
    image
  1. 分步查询
  1. 第一步查询员工信息
    image
  • select:设置分步查询的sql唯一标识(namespace.SQL或mapper忌口的全类名.方法)
  • column:设置分步查询的条件
Mappre接口
     /**
     * 通过分步查询员工以及所对应的部门信息
     * 分布查询第一步,查询出员工信息
     */
    Emp getEmpAndDeptByStepOne(@Param("eid") Integer eid);
对应的xml文件
 <!--association中column是分布查询的条件-->
    <resultMap id="empAndDeptByStepResultMap" type="Emp">
        <id property="eid" column="eid"></id>
        <result property="empName" column="emp_name"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
        <result property="email" column="email"></result>
        <association property="dept" select="com.sli.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo" column="did"></association>
    </resultMap>

    <!--3.最常用的通过分步查询两张表-->
    <!-- Emp getEmpAndDeptByStepOne(@Param("eid") Integer eid);-->
    <select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
        select * from t_user where eid = #{eid};
    </select>
  1. 第二步查询部门信息(根据部门did)
    image
Mapper接口
    /**
     * 通过分步查询员工以及所对应的部门信息
     * 分步查询第二步:通过did查询员工所对应的部门
     */
    Dept getEmpAndDeptByStepTwo(@Param("did")Integer did);

mapper接口对应的xml文件

<!--Dept getEmpAndDeptByStepTwo(@Param("did")Integer did);-->
    <select id="getEmpAndDeptByStepTwo" resultType="Dept">
        select * from t_dept where did = #{did};
    </select>

一对多处理

需要现在多的那一方建立list集合用于封装查询出来的所有数据

@Data
public class Dept {

    private Integer did;

    private String  deptName;

    private List<Emp> emps;
}
  1. Collection

image

  • collection:用来处理一堆多的映射关系
  • ofType:表示该属性对应的集合中存储的数据类型(也就是list中存储的对象的类型)
  1. 分布查询

第一步,查询部门信息

 /**
     * 通过分布查询查询部门以及部门中所有的员工信息
     * 分布查询第一步
     */
    Dept getDeptAndEmpStepOne(@Param("did") Integer did);
<!--2.分布查询实现1对多
    Dept getDeptAndEmpStepOne(@Param("did") Integer did);-->
    <resultMap id="deptAndEmpByStepResultMap" type="Dept">
        <id property="did" column="did"></id>
        <result property="deptName" column="dept_name"></result>
        <collection property="emps" select="com.sli.mybatis.mapper.EmpMapper.getDeptAndDeptByStepTwo" column="did"></collection>
    </resultMap>
    <select id="getDeptAndEmpStepOne" resultMap="deptAndEmpByStepResultMap">
        select * from t_dept where did=#{did}
    </select>

第二步,查询这个部门对应的员工信息

/**
     * 分步查询第二步
     * 通过分布查询查询部门一级部门中所哟的员工的信息
     */
    List<Emp> getDeptAndDeptByStepTwo(@Param("did")Integer did);
 <!--List<Emp> getDeptAndDeptByStepTwo(@Param("did")Integer did);-->
    <select id="getDeptAndDeptByStepTwo" resultType="Emp">
         select * from t_user where did = #{did}
    </select>
posted @   1_f  阅读(10)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 快收藏!一个技巧从此不再搞混缓存穿透和缓存击穿
· Blazor Hybrid适配到HarmonyOS系统
· 支付宝 IoT 设备入门宝典(下)设备经营篇
· 万字调研——AI生成内容检测
· 解决跨域问题的这6种方案,真香!
点击右上角即可分享
微信分享提示