文章目錄
關鍵在於練習 resultMap、association、collection 標籤的使用
一對一 (student -> class)
student 包含個人基本信息和內嵌對象 classes。
在查詢student時,需要聯合 student表和classes表進行查詢。
數據表
CREATE TABLE classes(
id int not null primary key,
name varchar(20)
);
insert into classes (id,name) values (1,"一班"),(2,"二班"),(3,"三班");
CREATE TABLE student(
id int not null primary key auto_increment,
name varchar(10),
cid int,
foreign key (cid) references classes(id)
);
insert into student (id, name, cid)
values (1,"KawYang",1),(2,"KawYang",2),(3,"張三",2),(4,"李四",3),(5,"Tom",3),(6,"Jary",3);
實體類
- Classes類對象
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Classes{
private int id;
private String name;
}
- Student類對象
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student{
private int id;
private String name;
private Classes classes;
}
Repository
編寫數據庫操作的接口
public interface IStudentRepository {
/**
* find by id
* @param id
* @return student
*/
Student findById(int id);
/**
* find all
* @return student list
*/
List<Student> findAll();
}
Mapper
mapper🏷️ : namespace
屬性映射到 student
的操作接口。
resultMap🏷️ : 配置類對象 --> 在 select
標籤中,使用 resultMap
屬性進行映射。
association🏷️ : 注入內嵌對象,javaType
將內嵌對象映射到實體類。
<?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.kawyang.repository.IStudentRepository">
<resultMap id="student" type="com.kawyang.entity.Student">
<id property="id" column="sid"/>
<result property="name" column="sname" />
<association property="classes" ofType="com.kawyang.entity.Classes">
<id property="id" column="cid" />
<result property="name" column="cname" />
</association>
</resultMap>
<select id="findById" resultMap="student" parameterType="int">
select s.id as sid,s.name as sname, c.id as cid, c.name as cname
from student s,classes c
where s.cid=c.id and s.id=#{id}
</select>
<select id="findAll" resultMap="student">
select s.id as sid,s.name as sname, c.id as cid, c.name as cname
from student s,classes c
where s.cid=c.id
</select>
</mapper>
一對多 (class -> studentes)
一個班級含有多個👨🎓,通過查詢班級信息,能夠將班級中的所有學生信息查詢出來
需要聯合 classes類和student進行查詢。
數據表
同上
實體類
- ClassesStu
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ClassesStu{
int id;
String name;
List<Student> students;
}
Repository
public interface IClassesStuRepository {
/**
* find classesStu by id
* @param id key
* @return classesStu
*/
ClassesStu findById(int id);
/**
* find all classesStu
* @return classesStu list
*/
List<ClassesStu> findAll();
}
Mapper
配置文件resultMap
<resultMap id="classesStu" type="com.kawyang.entity.ClassesStu">
<id property="id" column="cid" />
<result property="name" column="cname" />
<collection property="students" ofType="com.kawyang.entity.Student" >
<id property="id" column="sid" />
<result property="name" column="sname" />
</collection>
</resultMap>
collection🏷️ : 將查詢的 cid & cname
相同的結果,將 sid 和 sname 封裝成 ofType
指定類型的對象,並將多個對象以集合的方式返回。
==> Preparing: select c.id as cid, c.name as cname, s.id as sid, s.name as sname from student s, classes c where c.id=s.cid
==> Parameters:
<== Columns: cid, cname, sid, sname
<== Row: 1, 一班, 1, KawYang
<== Row: 2, 二班, 2, KawYang
<== Row: 2, 二班, 3, 張三
<== Row: 3, 三班, 4, 李四
<== Row: 3, 三班, 5, Tom
<== Row: 3, 三班, 6, Jary
<== Total: 6
多對多 (goods <-> consumers)
一種商品可以被多個消費者購買
一個消費者可以購買多個商品
商品與消費者事多對多的關係。
數據表
good : 商品信息表
consumer : 消費者信息表
con_good : 中間表
CREATE TABLE good(
id int primary key not null ,
name varchar(30)
);
insert into good (id, name) VALUES (1,"電視"),(2, "電冰箱"),(3, "洗衣機"),(4,"筆記本");
CREATE TABLE consumer(
id int primary key not null,
name varchar(30)
);
insert into consumer (id, name) VALUES (1,"KawYang"),(2,"李四"),(3,"張三");
CREATE TABLE con_good(
id int primary key not null ,
gid int,
cid int,
foreign key (gid) references good(id),
foreign key (cid) references consumer(id)
);
insert into con_good (id, gid, cid) VALUES (1,1,1),(2,3,2),(3,1,3),(4,2,1),(5,4,2),(6,3,3);
Mapper
多對多相當於兩個一對多的關係,需要配置兩個xml文件
- IGoodMapper
<mapper namespace="com.kawyang.repository.IGoodRepository">
<resultMap id="good" type="com.kawyang.entity.Good">
<id property="id" column="gid"/>
<result property="name" column="gname" />
<collection property="consumers" ofType="com.kawyang.entity.Consumer">
<id property="id" column="cid" />
<result property="name" column="cname" />
</collection>
</resultMap>
<select id="findAll" resultMap="good" >
select g.id as gid,g.name as gname, c.id as cid, c.name as cname
from good g,consumer c,con_good m
where g.id=m.gid and m.cid=c.id
</select>
<select id="findById" resultMap="good" parameterType="int">
select c.id as cid,c.name as cname,g.id as gid,g.name as gname
from Good as g,Consumer as c, Con_good m
where c.id = m.cid and g.id=m.gid and g.id=#{id}
</select>
</mapper>
- IConsumerMapper
<mapper namespace="com.kawyang.repository.IConsumerRepository">
<resultMap id="consumer" type="com.kawyang.entity.Consumer" >
<id column="id" property="id" />
<result column="name" property="name" />
<collection property="goods" ofType="com.kawyang.entity.Good" >
<id column="gid" property="id" />
<result column="gname" property="name" />
</collection>
</resultMap>
<select id="findAll" resultMap="consumer" >
select c.id as id,c.name as name,g.id as gid,g.name as gname
from Good as g,Consumer as c, Con_good m
where c.id = m.cid and g.id=m.gid;
</select>
<select id="findById" resultMap="consumer" parameterType="int">
select c.id as id,c.name as name,g.id as gid,g.name as gname
from Good as g,Consumer as c, Con_good m
where c.id = m.cid and g.id=m.gid and g.id=#{id}
</select>
</mapper>