mybatis 多对一,一对多
准备两张表(class表---student表)
建表脚本
Create Table CREATE TABLE `tb_class` ( `id` int NOT NULL AUTO_INCREMENT, `code` varchar(18) DEFAULT NULL, `NAME` varchar(18) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 Create Table CREATE TABLE `tb_student` ( `id` int NOT NULL AUTO_INCREMENT, `NAME` varchar(18) DEFAULT NULL, `sex` varchar(18) DEFAULT NULL, `age` int DEFAULT NULL, `class_id` int DEFAULT NULL, PRIMARY KEY (`id`), KEY `class_id` (`class_id`), CONSTRAINT `tb_student_ibfk_1` FOREIGN KEY (`class_id`) REFERENCES `tb_class` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8
各自表格对应的实体类
package cn.liziy.entity; import java.io.Serializable; import java.util.List; /** * @ClassName Clazz * @Author:Liziy * @Date 2020/8/6 17:27 * @Description: 班级实体类 **/ public class Clazz implements Serializable { private static final long serialVersionUID = -8931598000359337543L; private Integer id; private String code; private String name; //班级和学生是一对多的关系,一个班级可以有多个学生 private List<Student> students; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Student> getStudents() { return students; } public void setStudents(List<Student> students) { this.students = students; } }
package cn.liziy.entity; import java.io.Serializable; /** * @ClassName Student * @Author:Liziy * @Date 2020/8/6 17:29 * @Description: 学生实体类 **/ public class Student implements Serializable { private static final long serialVersionUID = -1952744231788123787L; private Integer id; private String name; private String sex; private Integer age; private Clazz clazz; 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 String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Clazz getClazz() { return clazz; } public void setClazz(Clazz clazz) { this.clazz = clazz; } }
多对一(多个学生对应一个班级)
Studentmapper.xml 和 StudentDao 的编写部分
<?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="cn.liziy.dao.StudentDao"> <!-- 查询所有学生信息, 多表连接使用resultMap --> <select id="selectAllStu" resultMap="studentResultMap"> SELECT s.id, s.name, s.sex, s.age, class_id, c.id cid, c.code, c.name cname FROM tb_student s LEFT JOIN tb_class c ON class_id = c.id </select> <!-- 映射Student对象的resultMap --> <resultMap id="studentResultMap" type="cn.liziy.entity.Student"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="sex" column="sex"/> <result property="age" column="age"/> <!-- 多对一关联:association --> <association property="clazz" javaType="cn.liziy.entity.Clazz" > <id property="id" column="cid"/> <result property="code" column="code"/> <result property="name" column="cname"/> </association> </resultMap> </mapper>
package cn.liziy.dao; import cn.liziy.entity.Student; import java.util.List; public interface StudentDao { /** * 查询所有学生信息 * @return */ List<Student> selectAllStu(); }
studentservice层
package cn.liziy.service; import cn.liziy.entity.Student; import java.util.List; /** * @ClassName StudentService * @Author:Liziy * @Date 2020/8/7 12:02 * @Description: **/ public interface StudentService { /** * 查询所有学生信息 * @return */ List<Student> selectAllStu(); }
package cn.liziy.service.impl; import cn.liziy.dao.StudentDao; import cn.liziy.entity.Student; import cn.liziy.service.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * @ClassName StudentServiceImpl * @Author:Liziy * @Date 2020/8/7 12:03 * @Description: **/ @Service public class StudentServiceImpl implements StudentService { @Autowired StudentDao studentDao; @Override public List<Student> selectAllStu() { return studentDao.selectAllStu(); } }
StudentController
package cn.liziy.controller; import cn.liziy.entity.Student; import cn.liziy.service.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; /** * @ClassName StudentController * @Author:Liziy * @Date 2020/8/7 12:04 * @Description: **/ @Controller public class StudentController { @Autowired StudentService studentService; /** * 查询所有学生信息 多对一 * @return */ @GetMapping("/students/all") public @ResponseBody List<Student> studentAll(){ return studentService.selectAllStu(); } }
控制台日志
json数据
每个学生都对应一个班级(多对一)
一对多(一个班级有多个学生)
ClazzMapper和ClazzDao的编写部分
<?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="cn.liziy.dao.ClazzDao"> <!-- 查询所有班级信息, 返回resultMap --> <select id="selectAllClazz" resultMap="clazzMap"> SELECT c.id cid, c.code, c.name cname, s.id, s.name, s.sex, s.age FROM tb_class c LEFT JOIN tb_student s on c.id = s.class_id </select> <!-- 映射Clazz对象的resultMap --> <resultMap id="clazzMap" type="cn.liziy.entity.Clazz"> <id property="id" column="cid"/> <result property="code" column="code"/> <result property="name" column="cname"/> <!-- 一对多关联映射:collection fetchType="lazy"表示懒加载 --> <collection property="students" javaType="ArrayList" column="id" ofType="cn.liziy.entity.Student"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="sex" column="sex"/> <result property="age" column="age"/> </collection> </resultMap> </mapper>
package cn.liziy.dao; import cn.liziy.entity.Clazz; import java.util.List; /** * @Author liziyang * @Date 22:23 2020/8/6 * @Description 班级数据访问接口 **/ public interface ClazzDao { /** * 查询所有的班级信息 * @return */ List<Clazz> selectAllClazz(); }
CalzzService
package cn.liziy.service; import cn.liziy.entity.Clazz; import java.util.List; public interface ClazzService { /** * 查询所有的班级信息 * @return */ List<Clazz> selectAllClazz(); }
package cn.liziy.service.impl; import cn.liziy.dao.ClazzDao; import cn.liziy.entity.Clazz; import cn.liziy.service.ClazzService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * @ClassName ClazzServiceImpl * @Author:Liziy * @Date 2020/8/6 22:40 * @Description: **/ @Service public class ClazzServiceImpl implements ClazzService { @Autowired ClazzDao clazzDao; @Override public List<Clazz> selectAllClazz() { return clazzDao.selectAllClazz(); } }
ClazzController
package cn.liziy.controller; import cn.liziy.entity.Clazz; import cn.liziy.service.ClazzService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; /** * @ClassName ClazzController * @Author:Liziy * @Date 2020/8/6 22:37 * @Description: **/ @Controller public class ClazzController { @Autowired ClazzService clazzService; /** * 查询所有班级信息 一对多 * @return */ @GetMapping("/clazz/all") public @ResponseBody List<Clazz> Clazz(){ return clazzService.selectAllClazz(); } }
控制台日志
json数据
班级和各自班级所属的学生(一对多)