首页 > 其他分享 > mybatis 表与表 关联查询 (二)

mybatis 表与表 关联查询 (二)

时间:2023-02-06 16:23:17浏览次数:32  
标签:职工 did 表与表 查询 emp mybatis select eid

Mapper
@Mapper
public interface EmpMapper {

    /**
     * 查询所有的职工信息
     */
    List<Emp> getAllEmp();

    /**
     * 查询职工及职工所对应的部门信息
     */
    Emp getEmpAndDept(@Param("eid") Integer eid);

    /**
     * 分步查询查询职工及职工所对应的部门信息
     * 分步查询第一步:查询职工信息
     */
    Emp getEmpAndDeptByOne(@Param("eid") Integer eid);

    /**
     * 分步查询查询部门及部门中所有的职工信息
     * 分步查询第二步:根据did查询职工信息
     */
    List<Emp> getDeptAndEmpByTwo(@Param("did") Integer did);


}

 

Mybatis

复制代码
   <!--
        resultMap:设置自定义映射关系
        id:唯一标识,不能重复
        type:设置映射关系中的实体类类型
        子标签:
        id:设置主键的映射关系
        result:设置普通字段的映射关系
        属性:
        property:设置映射关系中的属性名,必须是type属性所设置的实体类类型中的属性名
        column:设置映射关系中的字段名,必须是sql语句查询出的字段名
    -->
    <resultMap id="empResultMap" type="com.example.bootdemo.pojo.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>
    </resultMap>

    <select id="getAllEmp" resultMap="empResultMap">
        select * from m_emp
    </select>

    <!--处理多对一映射关系方式一:级联属性赋值-->
    <resultMap id="empAndDeptResultMapOne" type="com.example.bootdemo.pojo.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>
        <result property="dept.did" column="did"></result>
        <result property="dept.deptName" column="dept_name"></result>
    </resultMap>
    <!--处理多对一映射关系方式二:association-->
    <resultMap id="empAndDeptResultMapTwo" type="com.example.bootdemo.pojo.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:需要处理多对的映射关系的属性名
            javaType:该属性的类型
        -->
        <association property="dept" javaType="com.example.bootdemo.pojo.Dept">
            <id property="did" column="did"></id>
            <result property="deptName" column="dept_name"></result>
        </association>
    </resultMap>
    <select id="getEmpAndDept" resultMap="empAndDeptResultMapTwo">
        select * from m_emp left join m_dept on m_emp.did = m_dept .did where m_emp.eid = #{eid}
    </select>

    <resultMap id="empAndDeptByStepResultMap" type="com.example.bootdemo.pojo.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>
        <!--
            select:设置分步查询的sql的唯一标识(namespace.SQLId或mapper接口的全类名.方法名)
            column:设置分布查询的条件
            fetchType:当开启了全局的延迟加载之后,可通过此属性手动控制延迟加载的效果
            fetchType="lazy|eager":lazy表示延迟加载,eager表示立即加载
        -->
        <association property="com.example.bootdemo.pojo.Dept"
                     select="com.example.bootdemo.mapper.DeptMapper.getEmpAndDeptByStepTwo"
                     column="did"
                     fetchType="eager"></association>
    </resultMap>
    <select id="getEmpAndDeptByOne" resultMap="empAndDeptByStepResultMap">
        select * from m_emp where eid = #{eid}
    </select>

    <select id="getAllEmpOld" resultType="com.example.bootdemo.pojo.Emp">

        select * from m_emp
    </select>

    <select id="getDeptAndEmpByTwo" resultType="com.example.bootdemo.pojo.Emp">
        select * from m_emp where did = #{did}
    </select>
复制代码

 

标签:职工,did,表与表,查询,emp,mybatis,select,eid
From: https://www.cnblogs.com/RedOrange/p/17091653.html

相关文章

  • 01linux查询io资源使用情况。
    1.执行top   2.执行第一个参数是间隔多少s,第二个参数是执行多少次vmstat 110  也可以执行vmstat1 每秒输出一次......
  • //根据商品名称模糊查询时可以输入空格
    导包:importcom.baomidou.mybatisplus.core.toolkit.ObjectUtils;if(ObjectUtils.isNotNull(vo.getName())){booleanisTrue=true;Stringcondition=vo.get......
  • mysql max_allowed_packet查询和修改
    ​mysql根据配置文件会限制server接受的数据包大小。有时候大的插入和更新会被max_allowed_packet参数限制掉,导致失败。查看目前配置  showVARIABLESlike......
  • Mybatis源码解读
    1.mybatis4种加载配置文件的方式:   (1)相对应类资源路径的引用,属性关键字resource:     <mappers>        <mapperresource="mapper/PostM......
  • Mybatis记录
    MyBatis01:第一个程序MyBatis简介简介|MyBatis-Plus(baomidou.com)环境说明:jdk8+MySQL5.7.19maven-3.6.1IDEA学习前需要掌握:JDBCMySQLJ......
  • 查询
    查询多条记录语法格式:db.collection.find(query,projection,options)query为查询条件projection为展示行规则options为其他条件使用db.getCollection("books").fi......
  • Spring整合MyBatis及Junit4.11报错:No tests found matching [{ExactMatcher:fDisplayN
    发生缘由复习Spring整合MyBatis及Junit运行环境VSCode版本:1.72.0(usersetup)jdk版本:jdk-8电脑系统:win10spring-context:5.2.10.RELEASEjunit:4.11spring-test:5.......
  • 使用kibana来进行ElasticSearch的信息查询检索
    大家经常会听到使用ELK搭建日志管理平台、完成日志聚合检索的功能,那么这个平台到底是个什么概念,怎么搭建,怎么使用呢?ELK包括ElasticSearch(数据存储、快速查询)、logstash(日志......
  • 小程序云开发联表数据查询以及云函数中的应用
    点击观看视频↓↓↓小程序云开发联表数据查询以及在云函数中的应用|多表查询|连表lookup|聚合函数文章目录​​1、联表查询​​​​(1)lookup联接两个表格​​​​(2)使......
  • MyBatis的使用八(动态SQL)
    本主要讲述mybatis处理动态sql语句一.问题引入前端展示的数据表格中,查询条件可能不止一个,如何将用户输入的多个查询条件,拼接到sql语句中呢?DynamicMapper接口声......