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

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

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

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


建表及插入数据:


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());
	}

}

Mybatis中的一对多及多对一举例
上一篇:Mybatis一对一关系映射


下一篇:减小APK大小