zoukankan      html  css  js  c++  java
  • MyBatis多对一配置

    注意:这里需要把查出来的数据进行命名使用别名,这样我们封装实体类的时候才能使用,因为数据库中生成的别名是随机的

     select e.id,e.name,e.age,d.id did,d.name dname  from employee e
            left join dept d
            on  e.dept_id = d.id

    did,dname就是部门的id和名称

    这里还需要使用resultMap,不是使用resulttype

    多对一也有两种方式

    <?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">
    
    <!-- namespace表示命名空间  保证它是唯一   cn.itsource.mybatis.dao.impl.ProductDaoImpl + id="getUserById"-->
    <mapper namespace="_03_manytoone.EmployeeMapper">
    
    
        <!-- 多对一操作 查询员工的同时 查询部门 一条sql-->
        <select id="query01" resultMap="employeeMp">
            select e.id,e.name,e.age,d.id did,d.name dname  from employee e
            left join dept d
            on  e.dept_id = d.id
        </select>
        <!--id 结果Map的值 type:返回类型 (嵌套结果一条sql)-->
       <resultMap id="employeeMp" type="_03_manytoone.Employee">
            <id property="id" column="id"></id>
            <result property="name" column="name"></result>
            <result property="age" column="age"></result>
            <association property="dept" javaType="_03_manytoone.Dept">
                <id property="id" column="did"></id>
                <result property="name" column="dname"></result>
            </association>
        </resultMap>
    
        <!-- 多对一操作 嵌套查询(在查询出来的值dept_id,在根据dept_id这个值去查询另外东西) 1+N条sql -->
        <select id="query02" resultMap="employeeMp1" >
            select *  from employee e
        </select>
        <resultMap id="employeeMp1" type="_03_manytoone.Employee">
            <id property="id" column="id"></id>
            <result property="name" column="name"></result>
            <result property="age" column="age"></result>
            <association property="dept" column="dept_id" select="getDeptById">
            </association>
        </resultMap>
        <select id="getDeptById" parameterType="long" resultType="_03_manytoone.Dept" >
            select *  from dept where id=#{id}
        </select>
    
    
    
    
    
    </mapper>
    View Code
  • 相关阅读:
    《自动化测试工程师进阶之路》系列课程
    开发测试工程师系列课程
    AQA新加入手机自动化测试版块
    持续集成体系搭建服务
    开源自动化测试White与UIA
    TIB自动化测试快讯 自动化测试空间一周精选(201119)
    周六广州软件测试俱乐部圆桌会议3期
    Java白盒测试训练
    TIB自动化测试快讯 自动化测试空间一周精选(2012220)
    TIB自动化测试快讯 自动化测试空间一周精选(201226)
  • 原文地址:https://www.cnblogs.com/xiaoruirui/p/11776305.html
Copyright © 2011-2022 走看看