• springboot+mybatis实现一对一,一对多


    一对一

    准备工作

    数据库表

    DROP TABLE IF EXISTS `account`;
    CREATE TABLE `account` (
      `id` varchar(100) DEFAULT NULL,
      `uid` varchar(100) DEFAULT NULL,
      `money` decimal(10,0) DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
    
    INSERT INTO `account` VALUES ('1','41',10000),('2','42',20000),('3','43',30000);
    
    DROP TABLE IF EXISTS `user`;
    CREATE TABLE `user` (
      `id` varchar(100) DEFAULT NULL,
      `user_name` varchar(100) DEFAULT NULL,
      `birthday` date DEFAULT NULL COMMENT '生日',
      `sex` varchar(100) DEFAULT NULL,
      `address` varchar(100) DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
    
    INSERT INTO `user` VALUES ('41','小王','2022-02-27','男','河南'),('42','小李','2021-07-01','女','江苏'),('43','小红','2019-08-01','女','湖北');
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    实体类

    Account.java

    package com.jack.springbootmybatis.pojo;
    
    import org.springframework.format.annotation.DateTimeFormat;
    
    import java.util.Date;
    
    public class Account {
        /**
         * @description 编号
         */
        private String id;
        /**
         * @description 用户名
         */
        private String userName;
        /**
         * @description 生日
         */
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        private Date birthday;
        /**
         * @description 性别
         */
        private String sex;
        /**
         * @description 家庭住址
         */
        private String address;
    
        private User user;
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public Date getBirthday() {
            return birthday;
        }
    
        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }
    
        public String getSex() {
            return sex;
        }
    
        public void setSex(String sex) {
            this.sex = sex;
        }
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    
        public User getUser() {
            return user;
        }
    
        public void setUser(User user) {
            this.user = user;
        }
    
        @Override
        public String toString() {
            return "Account{" +
                    "id='" + id + '\'' +
                    ", userName='" + userName + '\'' +
                    ", birthday=" + birthday +
                    ", sex='" + sex + '\'' +
                    ", address='" + address + '\'' +
                    ", user=" + user +
                    '}';
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92

    User.java

    package com.geekmice.onetomany.bo;
    
    import java.util.Date;
    
    public class User {
        /**
         * @description 用户编号
         */
        private String id;
        /**
         * @description 用户名
         */
        private String userName;
        /**
         * @description 出生年月日
         */
        private Date birthday;
        /**
         * @description 性别
         */
        private String sex;
        /**
         * @description 家庭地址
         */
        private String address;
    
        private Account account;
    
        public Account getAccount() {
            return account;
        }
    
        public void setAccount(Account account) {
            this.account = account;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "id='" + id + '\'' +
                    ", userName='" + userName + '\'' +
                    ", birthday=" + birthday +
                    ", sex='" + sex + '\'' +
                    ", address='" + address + '\'' +
                    ", account=" + account +
                    '}';
        }
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public Date getBirthday() {
            return birthday;
        }
    
        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }
    
        public String getSex() {
            return sex;
        }
    
        public void setSex(String sex) {
            this.sex = sex;
        }
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90

    对应的xml

    AccountToUserMapper.xml

    
    DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="com.geekmice.onetomany.dao.IUserAccountDao" >
        <resultMap id="userAccountMap" type="com.geekmice.onetomany.bo.User">
            <id property="id" column="id"/>
            <result property="userName" column="user_name"/>
            <result property="birthday" column="birthday"/>
            <result property="sex" column="sex"/>
            <result property="address" column="address"/>
            <association property="account" javaType="com.geekmice.onetomany.bo.Account">
                <result property="uId" column="uid"/>
                <result property="money" column="money"/>
            association>
        resultMap>
        <select id="getAccountByUser" resultMap="userAccountMap">
         SELECT
            a.uid,
            a.money,
            b.user_name,
            b.id,
            b.birthday,
            b.sex,
            b.address
        FROM
            account a
        LEFT JOIN `user` b
                ON
            a.uid = b.id
            <where>
                <if test="userName != null and userName != '' ">
                  and  b.user_name = #{userName}
                if>
            where>
    
        select>
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    对应的controller

    
    @RestController
    public class UserAccountController {
        @Resource
        private IUserAccountDao iUserAccountDao;
    
        @PostMapping("/getAccountByUser")
        public User getAccountByUser(@RequestBody User user){
            return iUserAccountDao.getAccountByUser(user);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    postman调用
    在这里插入图片描述

    一对多

    理解:
    一个学生对应多门科目成绩

    数据库表

    DROP TABLE IF EXISTS `tmp_student`;
    
    CREATE TABLE `tmp_student` (
      `id` varchar(100) DEFAULT NULL,
      `name` varchar(100) DEFAULT NULL,
      `t_id` varchar(100) DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
    
    INSERT INTO `tmp_student` VALUES ('1','小王','1'),('2','小红','2'),('3','小张','2'),('4','小李','2'),('5','小黄','5'),('1','小王','1'),('2','小红','2'),('3','小张','2'),('4','小李','2'),('5','小黄','5');
    
    
    DROP TABLE IF EXISTS `tmp_teacher`;
    
    CREATE TABLE `tmp_teacher` (
      `id` varchar(100) DEFAULT NULL,
      `name` varchar(100) DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
    
    /*!40000 ALTER TABLE `tmp_teacher` DISABLE KEYS */;
    INSERT INTO `tmp_teacher` VALUES ('1','王老师'),('2','刘老师'),('3','张老师'),('4','陆老师'),('5','华老师');
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    实体类

    TmpTeacher.java

    package com.geekmice.onetomany.bo;
    
    import java.util.List;
    
    public class TmpTeacher {
        /**
         * @descripton 教师编号
         */
        private String tId;
        /**
         * @description 教师名
         */
        private String name;
        private List<TmpStudent> studentList;
    
        public String gettId() {
            return tId;
        }
    
        public void settId(String tId) {
            this.tId = tId;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public List<TmpStudent> getStudentList() {
            return studentList;
        }
    
        public void setStudentList(List<TmpStudent> studentList) {
            this.studentList = studentList;
        }
    
        @Override
        public String toString() {
            return "TmpTeacher{" +
                    "tId='" + tId + '\'' +
                    ", name='" + name + '\'' +
                    ", studentList=" + studentList +
                    '}';
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    TmpStudent .java

    package com.geekmice.onetomany.bo;
    
    import java.util.List;
    
    public class TmpStudent {
        /**
         * @description 学生编号
         */
        private String id;
        /**
         * @description 学生姓名
         */
        private String name;
    
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        public String toString() {
            return "TmpStudent{" +
                    "id='" + id + '\'' +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    对应dao层

    package com.geekmice.onetomany.dao;
    
    import com.geekmice.onetomany.bo.TmpTeacher;
    import org.apache.ibatis.annotations.Mapper;
    
    @Mapper
    public interface ITeacherStudentDao {
        TmpTeacher getTeacherInfo();
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    对应xml

    
    DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="com.geekmice.onetomany.dao.ITeacherStudentDao">
        <resultMap id="teacherStudentInfo" type="com.geekmice.onetomany.bo.TmpTeacher">
            <id column="bId" property="tId"/>
            <result column="teaName" property="name"/>
            <collection property="studentList" ofType="com.geekmice.onetomany.bo.TmpStudent">
                <result column="stuName" property="name"/>
                <result column="aId" property="id"/>
            collection>
        resultMap>
        <select id="getTeacherInfo" resultMap="teacherStudentInfo" >
            SELECT
                b.id bId,
                a.id aId,
                a.name stuName,
                b.name teaName
            FROM
                tmp_student a
            LEFT JOIN tmp_teacher b
            ON
                a.t_id = b.id
                where   b.name = '刘老师'
    
    
    
    
    
        select>
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    对应controller

    package com.geekmice.onetomany.controller;
    
    import com.geekmice.onetomany.bo.TmpTeacher;
    import com.geekmice.onetomany.dao.ITeacherStudentDao;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class TeacherStudentController {
        @Autowired
        private ITeacherStudentDao iTeacherStudentDao;
    
        @GetMapping("/getTeacherInfo")
        public TmpTeacher getTeacherInfo() {
            return iTeacherStudentDao.getTeacherInfo();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    postman调用

    在这里插入图片描述

    参考博客

    1.mybatis一对多&&多对一处理

    2.MyBatis 实现一对多有几种方式,怎么操作的?

  • 相关阅读:
    linux上安装bluesky的步骤
    日均数亿推送稳定性监控实践 | 得物技术
    第二十二章《记事本》第3节:项目完整代码
    RHEL 软件包管理 rpm yum 源码编译
    IDEA一键启动多个微服务
    Python构建企业微信智能应答机器人(含服务器部署)
    more than one ‘primary‘ bean found among candidates: xxxTransactionManager
    4d动感影院座椅5d动感影院体验馆大型7D互动影院
    SQL(Structured Query Language)—结构化查询语言
    Python爬虫项目实战案例-批量下载网易云榜单音乐保存至本地
  • 原文地址:https://blog.csdn.net/greek7777/article/details/126219551