Mybatis实践教程(七)-一对一的关联关系

创建一对一关系的数据表,我们在这里用丈夫和妻子的关系来表示数据库中的一对一关系,

下面我们通过实例来表示Mybatis中丈夫和妻子一对一关联关系。

需求:查询丈夫和妻子数据信息。

首先创建表结构t_husband,t_wife

CREATE TABLE `t_husband` (
  `id` bigint(30) NOT NULL AUTO_INCREMENT,
  `husband_name` varchar(55) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `wife_id` bigint(30) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

  CREATE TABLE `t_wife` (
  `id` bigint(30) NOT NULL AUTO_INCREMENT,
  `wife_name` varchar(30) NOT NULL,
  `age` int(11) DEFAULT NULL,
  `hus_id` bigint(30) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

创建Husband、Wife实体类

package com.example.springbootmybatis.domain;

public class Husband {
    private Long id;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getHusbandName() {
        return husbandName;
    }

    public void setHusbandName(String husbandName) {
        this.husbandName = husbandName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Long getWifeId() {
        return wifeId;
    }

    public void setWifeId(Long wifeId) {
        this.wifeId = wifeId;
    }

    public Wife getWife() {
        return wife;
    }

    public void setWife(Wife wife) {
        this.wife = wife;
    }

    private String husbandName;
    private Integer age;
    private Long wifeId;
    private Wife wife;

    @Override
    public String toString() {
        return "Husband{" +
                "id=" + id +
                ", husbandName='" + husbandName + '\'' +
                ", age=" + age +
                ", wifeId=" + wifeId +
                ", wife=" + wife +
                '}';
    }
}
package com.example.springbootmybatis.domain;

public class Wife {
    private Long id;
    private String wifeName;

    public Long getId() {
        return id;
    }



    public void setId(Long id) {
        this.id = id;
    }

    public String getWifeName() {
        return wifeName;
    }

    public void setWifeName(String wifeName) {
        this.wifeName = wifeName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Long getHusBandId() {
        return HusBandId;
    }

    public void setHusBandId(Long husBandId) {
        HusBandId = husBandId;
    }

    public Husband getHusband() {
        return husband;
    }

    public void setHusband(Husband husband) {
        this.husband = husband;
    }

    private Integer age;
    private Long HusBandId;
    private  Husband husband;
    @Override
    public String toString() {
        return "Wife{" +
                "id=" + id +
                ", wifeName='" + wifeName + '\'' +
                ", age=" + age +
                ", HusBandId=" + HusBandId +
                '}';
    }
}

HusBandService接口

package com.example.springbootmybatis.service;

import com.example.springbootmybatis.domain.Husband;

import java.util.List;

public interface HusBandService {

    List<Husband> one2oneMapping();
}

HusBandServiceImpl实现类

package com.example.springbootmybatis.service.impl;

import com.example.springbootmybatis.domain.Husband;
import com.example.springbootmybatis.mapper.HusbandMapper;
import com.example.springbootmybatis.service.HusBandService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class HusbandServiceImpl implements HusBandService {

    @Resource
    HusbandMapper husbandMapper;
    @Override
    public List<Husband> one2oneMapping() {
        return husbandMapper.one2oneMapping();
    }
}

HusBandServiceMapper接口

package com.example.springbootmybatis.service.impl;

import com.example.springbootmybatis.domain.Husband;
import com.example.springbootmybatis.mapper.HusbandMapper;
import com.example.springbootmybatis.service.HusBandService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class HusbandServiceImpl implements HusBandService {

    @Resource
    HusbandMapper husbandMapper;
    @Override
    public List<Husband> one2oneMapping() {
        return husbandMapper.one2oneMapping();
    }
}

HusBandMapper.xml

<?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">

<!--命名空间-->
<mapper namespace="com.example.springbootmybatis.mapper.HusbandMapper">
    <!-- 该resultMap用于实体对象User与表映射-->
    <resultMap id="HusbandResult" type="com.example.springbootmybatis.domain.Husband">
        <id property="id" column="id"/>
        <result property="husbandName" column="husband_name"/>
        <result property="age" column="age"/>
        <result property="wifeId" column="wife_id"/>
        <!--使用association标签来表示一对一关系映射中嵌套的对象信息 -->
        <association property="wife">
            <result property="id" column="id" />
            <result property="wifeName" column="wife_name" />
            <result property="age" column="age" />
            <result property="husbandId" column="hus_id" />
        </association>
    </resultMap>


    <!--查询丈夫和妻子的信息 一对一关系-->
    <select id="one2oneMapping" resultMap="HusbandResult">
       select h.*,w.wife_name,w.age,w.hus_id from t_husband h left  join  t_wife w on h.wife_id = w.id
    </select>


</mapper>

测试类

/**
 * 一对一关联关系查询
 */
@Test
public void one2oneMapping(){
    List<Husband> list = husBandService.one2oneMapping();
    for (Husband husbandRes:list) {
        System.out.println(husbandRes.toString());
    }
}

执行测试类,返回结果

Husband{id=1, husbandName='zhangsan', age=25, wifeId=1, wife=Wife{id=1, wifeName='zhang wife', age=25, husBandId=1}}
Husband{id=2, husbandName='wangwu', age=23, wifeId=2, wife=Wife{id=2, wifeName='lily', age=23, husBandId=2}}

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis-Plus是一款MyBatis的增强工具,它提供了很多实用的功能,比如一对多的关联查询。在MyBatis-Plus,一对多的关联查询可以通过使用@TableName注解和@TableField注解来实现。 假设我们有两张表,一张是学生表,另一张是课程表,一个学生可以选多门课程,那么我们就可以用一对多关联查询来查询某个学生选的所有课程。 首先,在学生表定义一个属性List<Course> courses,并使用@TableField注解将该属性与课程表的外键关联起来: ``` public class Student { @TableId private Long id; private String name; @TableField(exist = false) private List<Course> courses; } ``` 然后,在课程表定义一个属性Long studentId,并使用@TableField注解将该属性与学生表的主键关联起来: ``` public class Course { @TableId private Long id; private String name; @TableField("student_id") private Long studentId; } ``` 最后,我们使用MyBatis-Plus提供的wrapper类进行关联查询: ``` QueryWrapper<Student> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("id", studentId); List<Student> students = studentMapper.selectList(queryWrapper); for (Student student : students) { QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>(); courseQueryWrapper.eq("student_id", student.getId()); List<Course> courses = courseMapper.selectList(courseQueryWrapper); student.setCourses(courses); } ``` 以上就是MyBatis-Plus实现一对多关联查询的方法。如果您还有其他问题或需要进一步的帮助,请随时提出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值