Mybatis中的一对多及多对一举例

简介: 这次要连数据库建表及插入的数据都列表, 推导和理解起来,就通畅多了。

这次要连数据库建表及插入的数据都列表,

推导和理解起来,就通畅多了。

adea227c0357b072d69added026f4d6eeb5b512f


建表及插入数据:


CREATE TABLE tb_clazz(
  id INT PRIMARY KEY AUTO_INCREMENT,
  CODE VARCHAR(18),
  NAME VARCHAR(18)
);

INSERT INTO tb_clazz(CODE, NAME) VALUES('j1601', 'Java');
INSERT INTO tb_clazz(CODE, NAME) VALUES('j1602', 'Python');
INSERT INTO tb_clazz(CODE, NAME) VALUES('j1603', 'Javascript');
INSERT INTO tb_clazz(CODE, NAME) VALUES('j1604', 'Go');

CREATE TABLE tb_student(
  id INT PRIMARY KEY AUTO_INCREMENT,
  NAME VARCHAR(18),
  SEX CHAR(18),
  AGE INT,
  clazz_id INT,
  FOREIGN KEY (clazz_id) REFERENCES tb_clazz(id)
);

INSERT INTO tb_student(NAME, SEX, AGE, clazz_id) VALUES('jack', 'M', 22, 1);
INSERT INTO tb_student(NAME, SEX, AGE, clazz_id) VALUES('rose', 'F', 18, 1);
INSERT INTO tb_student(NAME, SEX, AGE, clazz_id) VALUES('tom', 'M', 25, 2);
INSERT INTO tb_student(NAME, SEX, AGE, clazz_id) VALUES('mary', 'F', 20, 2);
INSERT INTO tb_student(NAME, SEX, AGE, clazz_id) VALUES('tommy', 'M', 25, 4);
INSERT INTO tb_student(NAME, SEX, AGE, clazz_id) VALUES('jacky', 'F', 20, 4);

clazz.java


package org.fkit.domain;

import java.util.List;

public class Clazz {
	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;
	}
	@Override
	public String toString() {
		return "Clazz [id=" + id + ", code=" + code + ", name=" + name + "]";
	}
	
	
	
}

Student.java


package org.fkit.domain;

public class Student {
	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;
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + "]";
	}
	
	

}

ClassMapper.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="org.fkit.mapper.ClazzMapper">
	<resultMap id="clazzResultMap" type="org.fkit.domain.Clazz">
		<id property="id" column="id" />
		<result property="code" column="code" />
		<result property="name" column="name" />
		<collection property="students" javaType="Arraylist"
			column="id" ofType="org.fkit.domain.Student"
			select="org.fkit.mapper.StudentMapper.selectStudentByClazzId" 
			fetchType="lazy">
			<id property="id" column="id"/>
			<result property="name" column="name"/>
			<result property="sex" column="sez"/>
			<result property="age" column="age"/>
		</collection>
	</resultMap>
	
	<select id="selectClazzById" parameterType="int" resultMap="clazzResultMap">
		SELECT * FROM tb_clazz where id = #{id}
	</select>
</mapper>

StudentMapper.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="org.fkit.mapper.StudentMapper">
	<resultMap type="org.fkit.domain.Student" id="studentResultMap">
		<id property="id" column="id"/>
		<result property="name" column="name" />
		<result property="sex" column="sex" />
		<result property="age" column="age" />
		<association property="clazz" javaType="org.fkit.domain.Clazz">
			<id property="id" column="id"/>
			<result property="code" column="code" />
			<result property="name" column="name" />
		</association>
	</resultMap>
	
	<select id="selectStudentById" parameterType="int" resultMap="studentResultMap">
		SELECT * FROM tb_clazz c, tb_student s
		WHERE c.id = s.clazz_id
		AND s.id = #{id}
	</select>
	
	<select id="selectStudentByClazzId" parameterType="int" resultMap="studentResultMap">
		SELECT * FROM tb_student WHERE clazz_id = #{id}
	</select>
	
</mapper>

OneToManyTest.java


package org.fkit.tst;


import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.fkit.domain.Clazz;
import org.fkit.domain.Student;
import org.fkit.factory.FKSqlSessionFactory;
import org.fkit.mapper.ClazzMapper;
import org.fkit.mapper.StudentMapper;


public class OneToManyTest {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		SqlSession session = FKSqlSessionFactory.getSqlSession();
		
		OneToManyTest t = new OneToManyTest();
		t.testSelectClazzById(session);
		t.testSelectStudentById(session);

		session.commit();
		session.close();

	}
	
	public void testSelectClazzById(SqlSession session) {
		ClazzMapper cm = session.getMapper(ClazzMapper.class);
		Clazz clazz = cm.selectClazzById(4);
		System.out.println(clazz.getId() + " " + clazz.getCode() + " " + clazz.getName()); 
		List<Student> students = clazz.getStudents();
		for (Student stu : students) {
			System.out.println(stu);
		}
	}
	
	public void testSelectStudentById(SqlSession session) {
		StudentMapper sm = session.getMapper(StudentMapper.class);
		Student stu = sm.selectStudentById(1);
		System.out.println(stu);
		System.out.println(stu.getClazz());
	}

}

f88a9d87da3b47c5ebe73c1aa52f14884a9a6eee
目录
相关文章
|
2月前
|
SQL XML Java
mybatis复习04高级查询 一对多,多对一的映射处理,collection和association标签的使用
文章介绍了MyBatis中高级查询的一对多和多对一映射处理,包括创建数据库表、抽象对应的实体类、使用resultMap中的association和collection标签进行映射处理,以及如何实现级联查询和分步查询。此外,还补充了延迟加载的设置和用法。
mybatis复习04高级查询 一对多,多对一的映射处理,collection和association标签的使用
|
1月前
|
SQL XML Java
Mybatis中一对一和一对多的处理
这篇文章讲解了在Mybatis中如何处理一对一和一对多的关系映射,包括使用association和collection标签的具体方法。
21 1
|
6月前
|
SQL 缓存 Java
mybatis 一对多查询
mybatis 一对多查询
121 0
|
3月前
|
Java 数据库连接 mybatis
后端框架的学习----mybatis框架(9、多对一处理和一对多处理)
这篇文章介绍了在MyBatis框架中如何处理多对一和一对多的关联查询,通过定义`<resultMap>`和使用`<association>`与`<collection>`元素来实现对象间的关联映射。
|
5月前
|
Java 数据库连接 mybatis
Mybatis基于注解的一对一和一对多查询
Mybatis基于注解的一对一和一对多查询
|
5月前
|
SQL Java 数据库连接
Mybatis中一对多mapper配置
Mybatis中一对多mapper配置
|
6月前
|
Java 数据库连接 mybatis
mybatis的一对多
mybatis的一对多
|
6月前
|
XML SQL Java
mybatis的一对多,多对一,以及多对对的配置和使用
mybatis的一对多,多对一,以及多对对的配置和使用
37 2
|
6月前
|
存储 XML Java
mybatis使用内部类处理一对多类型数据2
mybatis使用内部类处理一对多类型数据2
63 0
|
1月前
|
Java 数据库连接 Maven
mybatis使用一:springboot整合mybatis、mybatis generator,使用逆向工程生成java代码。
这篇文章介绍了如何在Spring Boot项目中整合MyBatis和MyBatis Generator,使用逆向工程来自动生成Java代码,包括实体类、Mapper文件和Example文件,以提高开发效率。
114 2
mybatis使用一:springboot整合mybatis、mybatis generator,使用逆向工程生成java代码。