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
  • 相关阅读:
    CSS揭秘(六用户体验)
    CSS揭秘(五字体排印)
    CSS揭秘(四视觉效果)
    java split() 使用 . 来分割,转义(“” "|" "*" "+")
    springboot get请求405 Method Not Allowed
    websocket前端消息读取问题
    java解决中文乱码问题(jar包运行时中文返回前端数据或者控制台输出乱码问题)
    解决mysql 允许执行 XA RECOVER语句(atomikos 解决分布式事务报错)
    navicat修改mysql密码
    mybatisplus添加字段填充
  • 原文地址:https://www.cnblogs.com/xiaoruirui/p/11776305.html
Copyright © 2011-2022 走看看