mybatis 中一对多关系简单应用
1:应用场景
主要需要实现是一个实体类中存在一个字段,该字段为一个集合,集合存放的是另外一个实体类类型
如教室类(Class)中有一个学生类(student)的集合,即表示一个教室中 存在多个学生
项目保存路径: D:\海同\mybatis\9.16\FirstMybatis
2:相关代码
创建数据库中表语句
CREATE TABLE teacher( t_id INT PRIMARY KEY AUTO_INCREMENT, t_name VARCHAR(20) ); CREATE TABLE class( c_id INT PRIMARY KEY AUTO_INCREMENT, c_name VARCHAR(20), teacher_id INT ); ALTER TABLE class ADD CONSTRAINT fk_teacher_id FOREIGN KEY (teacher_id) REFERENCES teacher(t_id); INSERT INTO teacher(t_name) VALUES('LS1'); INSERT INTO teacher(t_name) VALUES('LS2'); INSERT INTO class(c_name, teacher_id) VALUES('bj_a', 1); INSERT INTO class(c_name, teacher_id) VALUES('bj_b', 2); CREATE TABLE student( s_id INT PRIMARY KEY AUTO_INCREMENT, s_name VARCHAR(20), class_id INT ); INSERT INTO student(s_name, class_id) VALUES('xs_A', 1); INSERT INTO student(s_name, class_id) VALUES('xs_B', 1); INSERT INTO student(s_name, class_id) VALUES('xs_C', 1); INSERT INTO student(s_name, class_id) VALUES('xs_D', 2); INSERT INTO student(s_name, class_id) VALUES('xs_E', 2); INSERT INTO student(s_name, class_id) VALUES('xs_F', 2);
2:实体类
package com.yuwenhui.entity; /** * Created by Administrator on 2017/9/18 0018. */ public class Student { private Integer id; private String name; public Student() { } public Student(Integer id, String name) { this.id = id; this.name = name; } 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; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + '}'; } }
package com.yuwenhui.entity; import java.util.List; /** * Created by Administrator on 2017/9/18 0018. */ public class ClassOne2Many { private Integer id; private String name; private Teacher teacher; private List<Student> students; public ClassOne2Many() { } public ClassOne2Many(Integer id, String name, Teacher teacher, List<Student> students) { this.id = id; this.name = name; this.teacher = teacher; this.students = students; } @Override public String toString() { return "ClassOne2Many{" + "id=" + id + ", name='" + name + '\'' + ", teacher=" + teacher + ", students=" + students + '}'; } 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 Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } public List<Student> getStudents() { return students; } public void setStudents(List<Student> students) { this.students = students; } }
映射文件
<?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.yuwenhui.entity.classOne2ManyMapper"> <!--方法一:多表联合查询--> <select id="sleectClassOne2Many1" resultMap="ClassOne2ManyResultMapper1" parameterType="Integer"> SELECT * FROM class c,teacher t,student s WHERE c.c_id = s.class_id AND c.teacher_id = t.t_id AND c.c_id = #{id}; </select> <resultMap id="ClassOne2ManyResultMapper1" type="_ClassOne2Many"> <id property="id" column="c_id"/> <result property="name" column="c_name"/> <association property="teacher" javaType="_Teacher"> <id property="id" column="t_id"/> <result property="name" column="t_name"/> </association> <!--表示一对多,collection 表示一个集合--> <collection property="students" ofType="_Student" > <id property="id" column="s_id"/> <result property="name" column="s_name"/> </collection> </resultMap> <!--方法二: 执行多次sql语句--> <select id="sleectClassOne2Many2" resultMap="ClassOne2ManyResultMapper2" parameterType="Integer"> SELECT * FROM class c WHERE c.c_id = #{id}; </select> <resultMap id="ClassOne2ManyResultMapper2" type="_ClassOne2Many"> <id property="id" column="c_id"/> <result property="name" column="c_name"/> <association property="teacher" javaType="_Teacher"> <id property="id" column="t_id"/> <result property="name" column="t_name"/> </association> <!--表示一对多--> <collection property="students" ofType="_Student" column="c_id" select="selectStudent2"> <!--<id property="id" column="s_id"/>--> <!--<result property="name" column="s_name"/>--> </collection> </resultMap> <select id="selectStudent2" resultMap="studentResultMapper" parameterType="Integer"> SELECT * FROM student WHERE class_id = #{id}; </select> <resultMap id="studentResultMapper" type="_Student"> <id property="id" column="s_id"/> <result property="name" column="s_name"/> </resultMap> </mapper>
测试:
@Before public void befor() { String resource = "conf.xml"; // InputStream resourceAsStream = TestMybatis.class.getClassLoader().getResourceAsStream(resource); Reader reader = null; try { reader = Resources.getResourceAsReader(resource); } catch (IOException e) { e.printStackTrace(); } sessionFactory = new SqlSessionFactoryBuilder().build(reader); session = sessionFactory.openSession(); } /** * 测试一对多关系 * 一个教室有多个学生 * 多表联合查询 */ @Test public void testOne2Many(){ String statement = "com.yuwenhui.entity.classOne2ManyMapper"+".sleectClassOne2Many1"; ClassOne2Many clazz = session.selectOne(statement,1); System.out.println(clazz); } /** * 测试一对多关系 * 一个教室有多个学生 * 多次发送sql语句 */ @Test public void testOne2Many2(){ String statement = "com.yuwenhui.entity.classOne2ManyMapper"+".sleectClassOne2Many2"; ClassOne2Many clazz = session.selectOne(statement,2); System.out.println(clazz); } @After public void after() { session.close(); }