zoukankan      html  css  js  c++  java
  • 【Mybatis】Mybatis一对多、多对一

    Mybatis多对一【association】

    1. 多对一
    /**
     * 添加部门实体字段
     * 第一步:多方添加一方的实体类:com/hxh/basic/project/vo/UserVo.java:18
     */
    private GradeVo gradeVo;
    

    /*第二步:在一方Mapper中添加根据编号检索*/
    <select id="get" resultType="com.hxh.basic.project.vo.GradeVo" parameterType="int">
    	select * from grade where id = #{id};
    </select>
    

    /*第三步:在多方Mapper中进行resultMap映射查询*/
    <resultMap id="TestMybatis" type="com.demo.pojo.Student" autoMapping="true">
    	<id property="stuNo" column="stuNo"></id>
    	<association property="grade" javaType="com.demo.pojo.Grade" column="gradeId"
    				 select="com.demo.dao.GradeDao.get"></association>
    </resultMap>
    
    <select id="getAll" resultMap="TestMybatis">
    	select * from student
    </select>
    

    /**
     * 第四步:调用Controller查询结果
     * 请求路径:http://localhost:8080/basic_project/user/getAll
    */
    {
        "code": 0,
        "message": "ok",
        "data": [
            {
                "id": 6,
                "nickname": "Consumer",
                "username": "于瑶",
                "birthday": "2021-03-03T00:00:00",
                "gradeVo": {
                    "id": 1,
                    "gradeNane": "一年级"
                }
            },
            {
                "id": 7,
                "nickname": "Company",
                "username": "白志超",
                "birthday": "2021-03-05T00:00:00",
                "gradeVo": {
                    "id": 0,
                    "gradeNane": "幼儿园"
                }
            }
        ]
    }
    


    Mybatis一对多【collection】

    1. 一对多
    /**
     * 添加用户字段集合
     * 第一步:多方实体类添加用户集合:com/hxh/basic/project/vo/GradeVo.java:27
     */
    private List<UserVo> list;
    

    /*第二步:在多方Mapper中添加根据状态检索*/
    <select id="get" resultType="com.hxh.basic.project.vo.UserVo" parameterType="int">
    	select * from user where status = #{status};
    </select>
    

    /*第三步:在一方Mapper中进行resultMap映射查询*/
    <resultMap id="resultMap" type="com.hxh.basic.project.vo.GradeVo" autoMapping="true">
    	<id property="id" column="id"></id>
    	<collection property="list" ofType="com.hxh.basic.project.vo.UserVo" javaType="java.util.List" column="id"
    				select="com.hxh.basic.project.mapper.UserMapper.get"></collection>
    </resultMap>
    
    <select id="getAll" resultMap="resultMap">
    	select * from grade;
    </select>
    

    /**
     * 第四步:调用Controller查询结果
     * 请求路径:http://localhost:8080/basic_project/grade/getAll
    */
    {
        "code": 0,
        "message": "ok",
        "data": [
            {
                "id": 0,
                "gradeNane": "幼儿园",
                "list": [
                    {
                        "id": 5,
                        "nickname": "Producer",
                        "username": "高佳琪",
                        "birthday": "2021-03-03T00:00:00"
                    }
                ]
            },
            {
                "id": 1,
                "gradeNane": "一年级",
                "list": [
                    {
                        "id": 4,
                        "nickname": "LouisVan",
                        "username": "王莹",
                        "birthday": "2021-03-03T00:00:00"
                    }
                ]
            },
            {
                "id": 2,
                "gradeNane": "二年级",
                "list": []
            }
        ]
    }
    

    概念

    1. 一对多和多对一
      • 一对多:collection【多方添加一方的实体类、property:实体类变量名称、JavaType:实体类的返回值类型、column:当前的外键,如status、select:调用一方的ID查询】
      • 多对一:association【一方添加多方集合、property:多方的变量名称、ofType:集合中多方的返回值类型、javaType:List包路径、column:当前ID,select:调用多方的根据外键查询】
  • 相关阅读:
    模块化、结构化的代码,何尝不是在讲人生
    DOS操作文件或文件夹
    OSQL
    DOS 命令大全
    Red Tea
    SetInterval_1
    SetInterval
    分库分表——Sharding-Sphere
    线上redis热key问题
    线上redis bgsave导致服务响应延迟
  • 原文地址:https://www.cnblogs.com/Twittery/p/14503817.html
Copyright © 2011-2022 走看看