- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Mybatis一对多和多对一处理的深入讲解由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
SQL:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
create
table
teacher(
id
int
not
null
,
name
varchar
(30)
default
null
,
primary
key
(id)
);
insert
into
teacher (id,
name
)
values
(1,
'蔡老师'
);
create
table
student(
id
int
not
null
,
name
varchar
(30)
default
null
,
tid
int
default
null
,
constraint
fk_tid
foreign
key
(tid)
references
teacher(id)
);
insert
into
student(id,
name
, tid)
VALUES
(1,
'小名'
, 1);
insert
into
student(id,
name
, tid)
VALUES
(2,
'小红'
, 1);
insert
into
student(id,
name
, tid)
VALUES
(3,
'小亮'
, 1);
insert
into
student(id,
name
, tid)
VALUES
(4,
'小兰'
, 1);
insert
into
student(id,
name
, tid)
VALUES
(5,
'笑笑'
, 1);
|
1
2
|
//查询所有的学生信息以及对应的老师的信息
List<Student> queryStudentAndTeacher();
|
1
2
3
4
5
6
7
8
9
10
|
@Data
@AllArgsConstructor
@NoArgsConstructor
public
class
Student {
private
int
id;
private
String name;
//学生需要关联一个老师
private
Teacher teacher;
}
|
1
2
3
4
5
6
7
|
@Data
@AllArgsConstructor
@NoArgsConstructor
public
class
Teacher {
private
int
id;
private
String name;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<!--思路:
1
.查询所有的学生
2
.根据查询出来的学生的tid寻找对应的老师 寻找对应的老师,子查询
-->
<resultMap id=
"rStuAndTea"
type=
"student"
>
<result property=
"id"
column=
"id"
/>
<result property=
"name"
column=
"name"
/>
<!-- 复杂的属性我们需要单独处理
指定属性的类型
对象使用association javaType
集合使用collection ofType
-->
<association property=
"teacher"
column=
"tid"
javaType=
"Teacher"
select=
"getTeacher"
/>
</resultMap>
<select id=
"queryStudentAndTeacher"
resultMap=
"rStuAndTea"
>
select * from student
</select>
<select id=
"getTeacher"
resultType=
"teacher"
>
select * from teacher where id = #{id}
</select>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<!--方式二 按照结果嵌套处理-->
<resultMap id=
"rStuAndTea2"
type=
"student"
>
<result property=
"id"
column=
"sid"
/>
<result property=
"name"
column=
"sname"
/>
<association property=
"teacher"
javaType=
"Teacher"
>
<result property=
"name"
column=
"tname"
/>
</association>
</resultMap>
<select id=
"queryStudentAndTeacher2"
resultMap=
"rStuAndTea2"
>
select s.id sid, s.name sname, t.name tname
from student s, teacher t
where s.tid = t.id
</select>
|
1
2
|
//查询指定老师的信息及其所有的学生
Teacher queryTeaAndStu(
@Param
(
"tid"
)
int
id);
|
1
2
3
4
5
6
7
8
9
10
|
@Data
@AllArgsConstructor
@NoArgsConstructor
public
class
Teacher {
private
int
id;
private
String name;
//一个老师拥有多个学生
private
List<Student> students;
}
|
1
2
3
4
5
6
7
8
|
@Data
@AllArgsConstructor
@NoArgsConstructor
public
class
Student {
private
int
id;
private
String name;
private
int
tid;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<!--按照结果嵌套查询-->
<resultMap id=
"rTeaAndStu"
type=
"teacher"
>
<result property=
"id"
column=
"tid"
/>
<result property=
"name"
column=
"tname"
/>
<collection property=
"students"
ofType=
"Student"
>
<result property=
"id"
column=
"sid"
/>
<result property=
"name"
column=
"sname"
/>
<result property=
"tid"
column=
"tid"
/>
</collection>
</resultMap>
<select id=
"queryTeaAndStu"
resultMap=
"rTeaAndStu"
>
select s.id sid, s.name sname, t.name tname, t.id tid
from student s, teacher t
where s.tid = t.id and t.id = #{tid}
</select>
|
1
2
3
4
5
6
7
8
9
10
11
|
<!--按照查询嵌套处理-->
<select id=
"queryTeaAndStu2"
resultMap=
"rTeaAndStu2"
>
select * from teacher where id = #{tid}
</select>
<resultMap id=
"rTeaAndStu2"
type=
"teacher"
>
<collection property=
"students"
javaType=
"ArrayList"
ofType=
"Student"
select=
"queryStudentByTeacherId"
column=
"id"
/>
</resultMap>
<select id=
"queryStudentByTeacherId"
resultType=
"Student"
>
select * from student where tid = #{tid}
</select>
|
小结 。
注意点:
到此这篇关于Mybatis一对多和多对一处理的文章就介绍到这了,更多相关Mybatis一对多和多对一处理内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。
原文链接:https://blog.csdn.net/weixin_61200215/article/details/120256339 。
最后此篇关于Mybatis一对多和多对一处理的深入讲解的文章就讲到这里了,如果你想了解更多关于Mybatis一对多和多对一处理的深入讲解的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我有一个 ProductDescription ViewController,它从我放置在许多 ViewController 中的 ProductTable UITableView 调用。 对于 St
首先,是的,我使用的是 DistinctRootEntityResultTransformer。 我有以下(Fluent NHibernate)映射: public FirstObjectMap()
我有两个模型:Book 和 Author。每个Author有很多本书,每本书有很多作者,所以我在Author和Book之间建立了ManyToMany关系,如下所示: class Author(mode
我正在尝试映射两个具有一对一关系的类之间的关系。在互联网上查找后,似乎人们更喜欢使用多对一来映射它。 例如,有一个 Order 类和 Bill 类。比尔持有发票的 FK。 这是我为比尔绘制的 map
假设有以下实体类: public class Player { public virtual int ID { get; set; } public virtual string Name { g
我想尝试一下 dynamodb 我能够保存单个对象。现在我想尝试创建一个多对一的关联。 许多任务应附加到单个用户。 @DynamoDBTable(tableName = "User") public
所以,情况如下: 表ComputerInventory,其中包含{computerInventoryID(主键)、TagID(唯一)、名称等} 表reviewStatus,其中包含{reviewSta
我在使用 hibernate 进行多对一映射时遇到问题。我有两个表,表 A 和表 B。我在这些表之间映射了多对一关系。两个表可以一起使用,也可以单独使用。 用 Java 中的类来表示表,例如: cla
我的实体: @Entity public class Film { @Id @GeneratedValue(strategy = IDENTITY) private long film
我必须制作拼贴项目的域层。我们的标准很少,比如我们必须使用 Hibernate,而且数据库也是固定的。 数据库的相关部分看起来几乎像这样: BusEntity(表 1) 总线 ID 公交车具体信息 总
如果有这两个实体: @Entity @Table(name = "CUSTOMER") public class Customer { @Id @GeneratedValue(stra
我正在尝试找出在多对一关系中检索单个最新结果的最有效方法。 示例: 实体 A - 团队(名称)实体 B - 员工(姓名,已创建) 团队>员工 我想在 Employee 上创建一个获取请求,返回每个团队
假设我有一个MySQL表read,记录了一个userid和一个articleid,记录了用户阅读了哪些文章。我现在想列出已阅读文章 1、2 和 3 的用户。 我知道以下是不可能的,但它说明了这个想法:
我的两个实体之间存在多对一关系。现在我希望当没有更多的 child 时将 parent 移除。 我的情况: 类(class)联系人 类(class)组织 一个组织有几个联系人,每个联系人都有一个组织。
我有下表: A 和 B A 有一个复合 PK:id int,类型 string。 B 只有 A 的 PK 之一:id int。 如何使用 B 的 id 和常量 type='typeB' 定义从 A 到
我正在为我的项目使用 Hibernate 3 映射一些实体,并简单地解释说我有这样的东西: Student 实体(tstudent 表) UniversityStudent 实体(tuniversit
我有一个成员表:成员(id, name, gender, head_id) 我想要的是建立以户主(家庭成员)为基础的家庭关系。就像这样:一个成员属于一个家庭(由成员定义) 也许我可以将其拆分为 2 个
示例 父类 @OneToMany(mappedBy = "parent") private List childs; 子类 @ManyToOne(cascade = CascadeType.ALL)
我有以下化学过程数据集,由 5 个连续输入向量组成,产生 1 个输出。每个输入每分钟采样一次,而输出操作系统每 5 采样一次。 虽然我相信输出取决于之前的 5 个输入向量,但我决定为我的设计寻找 LS
我正在将我的应用程序从原则 1 转换为原则 2.4我确实从数据库自动映射,但缺少一些关系: 表:产品产品 ID、标题、价格 表:位置产品id , 产品id , 位置id , 数量 因此每个 Locat
我是一名优秀的程序员,十分优秀!