mybatis一对多关联映射


一、参考

MyBatis 一对多关联映射

二、使用步骤

1.数据库

学生信息表
在这里插入图片描述
学生成绩表
在这里插入图片描述

一个学生信息对应多个成绩信息;

2.后端接口案例

2.1 学生成绩查询

StudentScore.java

package com.student.model;

import java.io.Serializable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.lang.Integer;

/**
 * 分数表 StudentScore
 *
 * @author fuce_自动生成
 * @email 115889198@qq.com
 * @date 2022-02-01 14:20:56
 */
@ApiModel(value = "StudentScore", description = "分数表")
public class StudentScore implements Serializable {

private static final long serialVersionUID = 1L;


    /**
     * 主键
     **/
    @ApiModelProperty(value = "主键")
    private Integer id;

    /**
     * 学生id
     **/
    @ApiModelProperty(value = "学生id")
    private Integer studentId;

    /**
     * 课程id
     **/
    @ApiModelProperty(value = "课程id")
    private Integer curriculumId;

    /**
     * 分数
     **/
    @ApiModelProperty(value = "分数")
    private Integer score;


    public Integer getId() {
        return id;
    }

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


    public Integer getStudentId() {
        return studentId;
    }

    public void setStudentId(Integer studentId) {
        this.studentId = studentId;
    }


    public Integer getCurriculumId() {
        return curriculumId;
    }

    public void setCurriculumId(Integer curriculumId) {
        this.curriculumId = curriculumId;
    }


    public Integer getScore() {
        return score;
    }

    public void setScore(Integer score) {
        this.score = score;
    }


    public StudentScore() {
        super();
    }


    public StudentScore(Integer id, Integer studentId, Integer curriculumId, Integer score) {

        this.id = id;
        this.studentId = studentId;
        this.curriculumId = curriculumId;
        this.score = score;

    }

    @Override
    public String toString() {
        return "StudentScore{" +
                "id=" + id +
                ", studentId=" + studentId +
                ", curriculumId=" + curriculumId +
                ", score=" + score +
                '}';
    }
}

StudentScoreMapper.java

package com.student.mapper;

import java.util.List;
import com.student.model.StudentScore;
import org.springframework.stereotype.Repository;

/**
 * 分数表 StudentScoreMapper
 * @author zjg
 * @date 2022-02-01 14:20:56
 */
@Repository
public interface StudentScoreMapper {
    List<StudentScore> selectByStudentID(Integer studentID);
}

StudentScoreMapper.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.student.mapper.StudentScoreMapper">
  <resultMap id="BaseResultMap" type="com.student.model.StudentScore">
    <result column="id" jdbcType="INTEGER" property="id" />
    <result column="student_id" jdbcType="INTEGER" property="studentId" />
    <result column="curriculum_id" jdbcType="INTEGER" property="curriculumId" />
    <result column="score" jdbcType="INTEGER" property="score" />
  </resultMap>
  <sql id="Base_Column_List">
	 id,student_id,curriculum_id,score
  </sql>
  <select id="selectByStudentID" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from t_student_score
    where student_id = #{studentID,jdbcType=INTEGER}
  </select>
</mapper>

2.2 学生信息查询

StudentInfo.java

package com.student.model;

import java.io.Serializable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.lang.Character;
import java.util.ArrayList;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.lang.Integer;

/**
 * 学生基本信息 StudentInfo 
 * @date 2022-01-27 22:12:56
 */
 @ApiModel(value="StudentInfo", description="学生基本信息")
public class StudentInfo implements Serializable {

	private static final long serialVersionUID = 1L;
	
		
	/** 学号 **/
	@ApiModelProperty(value = "学号")
	private Integer id;
		
	/** 姓名 **/
	@ApiModelProperty(value = "姓名")
	private String name;
		
	/** 年龄 **/
	@ApiModelProperty(value = "年龄")
	private Integer age;
		
	/** 出生日期 **/
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
	@ApiModelProperty(value = "出生日期")
	private Date birthday;
		
	/** 民族 **/
	@ApiModelProperty(value = "民族")
	private String nation;
		
	/** 证件类型 **/
	@ApiModelProperty(value = "证件类型")
	private String idType;
		
	/** 证件号码 **/
	@ApiModelProperty(value = "证件号码")
	private String idNumber;
		
	/** 手机号 **/
	@ApiModelProperty(value = "手机号")
	private Integer tel;
		
	/** 入学时间 **/
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
	@ApiModelProperty(value = "入学时间")
	private Date admissionTime;
		
	/** 家庭住址 **/
	@ApiModelProperty(value = "家庭住址")
	private String address;
		
	/** 院系 **/
	@ApiModelProperty(value = "院系")
	private String faculty;
		
	/** 专业 **/
	@ApiModelProperty(value = "专业")
	private String major;
		
	/** 班级 **/
	@ApiModelProperty(value = "班级")
	private Integer classID;
		
	/** 辅导员 **/
	@ApiModelProperty(value = "辅导员")
	private String instructor;
		
	/** 是否在籍(0:否;1:是) **/
	@ApiModelProperty(value = "是否在籍(0:否;1:是)")
	private Character registered;

	/** 分数信息**/
	@ApiModelProperty(value = "分数信息StudentScore")
	private ArrayList<StudentScore> studentScore;

	public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
	 
			
	public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
	 
			
	public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
	 
			
	public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
	 
			
	public String getNation() {
        return nation;
    }

    public void setNation(String nation) {
        this.nation = nation;
    }
	 
			
	public String getIdType() {
        return idType;
    }

    public void setIdType(String idType) {
        this.idType = idType;
    }
	 
			
	public String getIdNumber() {
        return idNumber;
    }

    public void setIdNumber(String idNumber) {
        this.idNumber = idNumber;
    }
	 
			
	public Integer getTel() {
        return tel;
    }

    public void setTel(Integer tel) {
        this.tel = tel;
    }
	 
			
	public Date getAdmissionTime() {
        return admissionTime;
    }

    public void setAdmissionTime(Date admissionTime) {
        this.admissionTime = admissionTime;
    }
	 
			
	public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
	 
			
	public String getFaculty() {
        return faculty;
    }

    public void setFaculty(String faculty) {
        this.faculty = faculty;
    }
	 
			
	public String getMajor() {
        return major;
    }

    public void setMajor(String major) {
        this.major = major;
    }
	 
			
	public Integer getClassID() {
        return classID;
    }

    public void setClassID(Integer classID) {
        this.classID = classID;
    }
	 
			
	public String getInstructor() {
        return instructor;
    }

    public void setInstructor(String instructor) {
        this.instructor = instructor;
    }
	 
			
	public Character getRegistered() {
        return registered;
    }

    public void setRegistered(Character registered) {
        this.registered = registered;
    }

	public ArrayList<StudentScore> getStudentScore() {
		return studentScore;
	}

	public void setStudentScore(ArrayList<StudentScore> studentScore) {
		this.studentScore = studentScore;
	}

	public StudentInfo() {
        super();
    }
	public StudentInfo(Integer id, String name, Integer age, Date birthday, String nation, String idType, String idNumber, Integer tel, Date admissionTime, String address, String faculty, String major, Integer classID, String instructor, Character registered) {
		this.id = id;
		this.name = name;
		this.age = age;
		this.birthday = birthday;
		this.nation = nation;
		this.idType = idType;
		this.idNumber = idNumber;
		this.tel = tel;
		this.admissionTime = admissionTime;
		this.address = address;
		this.faculty = faculty;
		this.major = major;
		this.classID = classID;
		this.instructor = instructor;
		this.registered = registered;
	}

	@Override
	public String toString() {
		return "StudentInfo{" +
				"id=" + id +
				", name='" + name + '\'' +
				", age=" + age +
				", birthday=" + birthday +
				", nation='" + nation + '\'' +
				", idType='" + idType + '\'' +
				", idNumber='" + idNumber + '\'' +
				", tel=" + tel +
				", admissionTime=" + admissionTime +
				", address='" + address + '\'' +
				", faculty='" + faculty + '\'' +
				", major='" + major + '\'' +
				", classID=" + classID +
				", instructor='" + instructor + '\'' +
				", registered=" + registered +
				", studentScore=" + studentScore +
				'}';
	}
}

StudentInfoMapper.java

package com.student.mapper;

import java.util.List;
import com.student.model.StudentInfo;
import com.student.model.example.StudentInfoExample;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

/**
 * 学生基本信息 StudentInfoMapper
 * @author zjg_自动生成
 * @date 2022-01-27 22:12:56
 */
@Repository
public interface StudentInfoMapper {
    List<StudentInfo> selectByExample(StudentInfoExample example);	  	
}

StudentInfoMapper.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.student.mapper.StudentInfoMapper">
  <resultMap id="BaseResultMap" type="com.student.model.StudentInfo">
    <result column="id" jdbcType="INTEGER" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="age" jdbcType="INTEGER" property="age" />
    <result column="birthday" jdbcType="TIMESTAMP" property="birthday" />
    <result column="nation" jdbcType="VARCHAR" property="nation" />
    <result column="id_type" jdbcType="VARCHAR" property="idType" />
    <result column="id_number" jdbcType="VARCHAR" property="idNumber" />
    <result column="tel" jdbcType="INTEGER" property="tel" />
    <result column="admission_time" jdbcType="TIMESTAMP" property="admissionTime" />
    <result column="address" jdbcType="VARCHAR" property="address" />
    <result column="faculty" jdbcType="VARCHAR" property="faculty" />
    <result column="major" jdbcType="VARCHAR" property="major" />
    <result column="class" jdbcType="INTEGER" property="classID" />
    <result column="instructor" jdbcType="VARCHAR" property="instructor" />
    <result column="registered" jdbcType="CHAR" property="registered" />
    <!-- 一对多关联映射:collection fetchType="lazy"表示懒加载 -->
    <collection property="studentScore" javaType="ArrayList"
                column="id" ofType="com.student.model.StudentScore"
                select="com.student.mapper.StudentScoreMapper.selectByStudentID"
                fetchType="lazy">
      <id property="id" column="id" jdbcType="INTEGER" />
      <result column="student_id" jdbcType="INTEGER" property="studentId" />
      <result column="curriculum_id" jdbcType="INTEGER" property="curriculumId" />
      <result column="score" jdbcType="INTEGER" property="score" />
    </collection>
  </resultMap>
  <sql id="Base_Column_List">
	  	  	      	id,
       	  	      	name,
       	  	      	age,
       	  	      	birthday,
       	  	      	nation,
       	  	      	id_type,
       	  	      	id_number,
       	  	      	tel,
       	  	      	admission_time,
       	  	      	address,
       	  	      	faculty,
       	  	      	major,
       	  	      	class,
       	  	      	instructor,
       	  	    	registered
       	</sql>
  <select id="selectByExample" parameterType="com.student.model.example.StudentInfoExample" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from t_student_info
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
</mapper>

3.测试案例

package com.student;

import com.student.mapper.StudentInfoMapper;
import com.student.mapper.StudentScoreMapper;
import com.student.model.StudentInfo;
import com.student.model.StudentScore;
import com.student.model.example.StudentInfoExample;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Create by zjg on 2022/9/25
 */
@RunWith(SpringRunner.class) //作用:让当前类在容器环境下进行测试
@SpringBootTest(classes = SpringbootStart.class)//作用:声明当前类是springboot的测试类并且获取入口类上的相关信息 SpringBootApplication是入口类类名
public class SpringbootStartTest {
    @Autowired
    private StudentInfoMapper studentInfoMapper;
    @Test
    public void test(){
        queryStudentInfo();
    }
    public void queryStudentInfo(){
        StudentInfoExample studentInfoExample = new StudentInfoExample();
        studentInfoExample.createCriteria().andIdEqualTo(0);
        List<StudentInfo> studentInfoList = studentInfoMapper.selectByExample(studentInfoExample);
        studentInfoList.forEach(System.out::println);
    }
}

在这里插入图片描述


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值