集合嵌套查询和集合嵌套结果的区别
嵌套查询是多条sql语句分开写并配置,嵌套结果是一条sql语句关联查询并配置,实质效果是一样的。嵌套语句的查询会导致数据库访问次数不定,进而有可能影响到性能。
1.创建2张表,建立主外键关系
2.建立实体类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
package com.yw.test06;
public class Class
{
private int id;
private String name;
public int getId()
{
return id;
}
public void setId( int id)
{
this .id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this .name = name;
}
@Override
public String toString()
{
return "Class [id=" + id + ", name=" + name + "]" ;
}
}
package com.yw.test06;
public class Student
{
private int id;
private String name;
private int age;
private Class c;
public int getId()
{
return id;
}
public void setId( int id)
{
this .id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this .name = name;
}
public int getAge()
{
return age;
}
public void setAge( int age)
{
this .age = age;
}
public Class getC()
{
return c;
}
public void setC(Class c)
{
this .c = c;
}
@Override
public String toString()
{
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", c=" + c + "]" ;
}
}
|
3.修改配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<? xml version = "1.0" encoding = "UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
< configuration >
< properties resource = "config.properties" >
</ properties >
< typeAliases >
<!-- <typeAlias type="com.yw.test06.StudentMapper" alias="Student" />
<typeAlias type="com.yw.test06.ClassMapper" alias="Class" /> -->
< package name = "com.yw.test06" />
</ typeAliases >
< environments default = "development" >
< environment id = "development" >
< transactionManager type = "JDBC" />
< dataSource type = "POOLED" >
< property name = "driver" value = "${driver}" />
< property name = "url" value = "${url}" />
< property name = "username" value = "${username}" />
< property name = "password" value = "${password}" />
</ dataSource >
</ environment >
</ environments >
< mappers >
<!-- <mapper resource="org/mybatis/example/BlogMapper.xml"/> -->
< mapper resource = "com/yw/test06/StudentMapper.xml" />
</ mappers >
</ configuration >
|
4.建立映射文件
1)嵌套查询
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<? 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.yw.test07.StudentMapper" >
< select id = "selectStudent" resultMap = "studentResult" >
SELECT * FROM student s
WHERE s.ID = #{id}
</ select >
< resultMap type = "Student" id = "studentResult" >
< association property = "c" column = "c_id" javaType = "Class"
select = "selectClass" />
</ resultMap >
< select id = "selectClass" resultType = "Class" >
SELECT * FROM class WHERE ID= #{id}
</ select >
</ mapper >
|
2)嵌套结果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<? 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.yw.test06.StudentMapper" >
< resultMap id = "studentResult" type = "com.yw.test06.Student" >
< id property = "id" column = "id" />
< result property = "name" column = "name" />
< result property = "age" column = "age" />
< association property = "c" resultMap = "classResult"
javaType = "Class" ></ association >
</ resultMap >
< resultMap type = "com.yw.test06.Class" id = "classResult" >
< id property = "id" column = "id" />
< result property = "name" column = "name" />
</ resultMap >
< select id = "selectStudent" resultMap = "studentResult" >
SELECT s.id,c.id,s.name,s.age,c.name from student s left join class c on c.id=s.c_id where s.id=#{id}
</ select >
</ mapper >
|
5.创建测试类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
package com.yw.test06;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class Test01
{
public static void main(String[] args) throws IOException
{
String resource = "com/yw/test06/mybatis-config.xml" ;
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession( false );
try {
Student user = (Student) session.selectOne( "com.yw.test06.StudentMapper.selectStudent" , 1 );
System.out.println(user);
} finally {
session.close();
}
}
}
|
MyBatis 嵌套查询解析
Mybatis表现关联关系比hibernate简单,没有分那么细致one-to-many、many-to-one、one-to-one。而是只有两种association(一)、collection(多),表现很简洁。下面通过一个实例,来展示一下Mybatis对于常见的一对多和多对一关系复杂映射是怎样处理的。
以最简单的用户表订单表这个最简单的一对多做示例
对应的JavaBean
User:
1
2
3
4
5
6
7
|
public class User {
private int id;
private String name;
private Double age;
private List<User_orders> orders;
// get set 省
}
|
User_orders:
1
2
3
4
5
|
public class User_orders {
private int id;
private String name;
// get set 省
}
|
对应的数据库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
mysql> desc user ;
+ -------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+ -------+-------------+------+-----+---------+----------------+
| id | int (11) | NO | PRI | NULL | auto_increment |
| name | varchar (20) | NO | | NULL | |
| age | double | YES | | NULL | |
+ -------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> desc user_orders;
+ ---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+ ---------+-------------+------+-----+---------+----------------+
| id | int (11) | NO | PRI | NULL | auto_increment |
| name | varchar (20) | NO | | NULL | |
| user_id | int (5) | YES | MUL | NULL | |
+ ---------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
|
现在查询一个user的id查询出所有信息.如果不考虑关联查询,我们会先根据user的id在user表中查询出name,age然后设置给User类的时候,再根据该user的id在user_orders表中查询出所有订单并设置给User类。这样的话,在底层最起码调用两次查询语句,得到需要的信息,然后再组装User对象。
嵌套语句查询
mybatis提供了一种机制,叫做嵌套语句查询,可以大大简化上述的操作,加入配置及代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<resultMap type= "domain.User" id= "user" >
<id column = "id" property= "id" />
<result column = "age" property= "age" />
<collection column = "id" property= "orders" ofType= "domain.User_orders"
select = "selectOrderByUser" >
<id column = "id" property= "id" />
<result column = "name" property= "name" />
</collection>
</resultMap>
< select id= "selectOrderByUser" parameterType= "integer" resultType= "domain.User_orders" >
select id, name from user_orders where user_id = #{id}
</ select >
< select id= "findById" resultMap= "user" parameterType= "integer" >
select * from user where id = #{id}
</ select >
|
测试(可以成功查询到所有信息):
1
2
3
4
5
6
7
8
9
10
|
String config = "sqlMapConfig.xml" ;
InputStream inputStream = Resources.getResourceAsStream(config);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
// 执行在bean配置文件中定义的sql语句
User user = session.selectOne( "UserMapper.findById" , 1 );
//一句即可获取到复杂的User对象。
System.out.println(user);
session.commit();
session.close();
|
嵌套语句查询的原理
在上面的代码中,Mybatis会执行以下流程:
1.先执行 findById 对应的语句从User表里获取到ResultSet结果集;
2.取出ResultSet下一条有效记录,然后根据resultMap定义的映射规格,通过这条记录的数据来构建对应的一个User 对象。
当要对User中的orders属性进行赋值的时候,发现有一个关联的查询,此时Mybatis会先执行这个select查询语句,得到返回的结果,将结果设置到user的orders属性上这种关联的嵌套查询,有一个非常好的作用就是:可以重用select语句,通过简单的select语句之间的组合来构造复杂的对象。想如上的两个select完全可以独立使用。
嵌套查询的多对一
上面的关联查询查询其实是对于一对多的查询,即从user中查出user_order的信息。
现在从user_order中查user的信息.
在User_order表中增加字段user:
1
2
3
4
5
6
|
public class User_orders {
private int id;
private String name;
private User user;
//xxx
}
|
配置select:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
< resultMap type = "domain.User_orders" id = "user_order" >
< id column = "id" property = "id" />
< result column = "name" property = "name" />
< association property = "user" column = "user_id" javaType = "domain.User" select = "selectUserByOrderId" >
< id column = "id" property = "id" />
< result column = "age" property = "age" />
</ association >
</ resultMap >
< select id = "selectUserByOrderId" parameterType = "INTEGER" resultType = "domain.User" >
select id,age from user where id = #{id}
</ select >
< select id = "findOne" resultMap = "user_order" parameterType = "integer" >
select * from user_orders where id=#{id}
</ select >
|
测试:
1
2
3
4
5
6
7
|
SqlSession session = sqlSessionFactory.openSession();
// 执行在bean配置文件中定义的sql语句
User_orders user_orders= session.selectOne( "User_ordersMapper.findOne" , 1);
System. out .println(user_orders);
//查询到了user_order对应的 user 的信息
session. commit ();
session. close ();
|
嵌套查询的N+1问题
尽管嵌套查询大量的简化了存在关联关系的查询,但它的弊端也比较明显:即所谓的N+1问题。关联的嵌套查询显示得到一个结果集,然后根据这个结果集的每一条记录进行关联查询。
现在假设嵌套查询就一个(即resultMap 内部就一个association标签),现查询的结果集返回条数为N,那么关联查询语句将会被执行N次,加上自身返回结果集查询1次,共需要访问数据库N+1次。如果N比较大的话,这样的数据库访问消耗是非常大的!所以使用这种嵌套语句查询的使用者一定要考虑慎重考虑,确保N值不会很大。
以上面一对多(根据user的id查询order)的例子为例,select 语句本身会返回user条数为1 的结果集,由于它存在有1条关联的语句查询,它需要共访问数据库 1*(1+1)=2次数据库。
嵌套结果查询
嵌套语句的查询会导致数据库访问次数不定,进而有可能影响到性能。Mybatis还支持一种嵌套结果的查询:即对于一对多,多对多,多对一的情况的查询,Mybatis通过联合查询,将结果从数据库内一次性查出来,然后根据其一对多,多对一,多对多的关系和ResultMap中的配置,进行结果的转换,构建需要的对象。
重新定义User的结果映射 resultMap
1
2
3
4
5
6
7
8
|
< resultMap type = "domain.User" id = "user_auto" >
< id column = "id" property = "id" />
< result column = "age" property = "age" />
< collection column = "id" property = "orders" ofType = "domain.User_orders" >
< id column = "order_id" property = "id" />
< result column = "name" property = "name" />
</ collection >
</ resultMap >
|
对应的sql语句如下:
1
2
3
4
|
< select id= "findAuth" resultMap= "user_auto" >
select u.id,u.age,o.id as order_id ,o. name ,o.user_id as user_id from user u left outer join user_orders o
on o.user_id = u.id
</ select >
|
嵌套结果查询的执行步骤
1.根据表的对应关系,进行join操作,获取到结果集;
根据结果集的信息和user 的resultMap定义信息,对返回的结果集在内存中进行组装、赋值,构造User;
返回构造出来的结果List 结果。
对于关联的结果查询,如果是多对一的关系,则通过形如 <association property="user" column="user_id" javaType="domain.User" > 进行配置,Mybatis会通过column属性对应的user_id 值去从内存中取数据,并且封装成User_order对象;
如果是一对多的关系,就如User和User_order之间的关系,通过形如 <collection column="id" property="orders" ofType="domain.User_orders">进行配置,MyBatis通过 id去内存中取User_orders对象,封装成List;
对于关联结果的查询,只需要查询数据库一次,然后对结果的整合和组装全部放在了内存中。
以上是通过查询User表所有信息来演示了一对多和多对一的映射对象处理。希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_36468810/article/details/109356533