阅读 45 SEO

MyBatis多表操作查询功能

这篇文章主要介绍了MyBatis多表操作,包括一对一查询,一对多查询的模型,多对多查询的需求,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下

一对一查询

用户表和订单表的关系为,一个用户多个订单,一个订单只从属一个用户
一对一查询的需求:查询一个订单,与此同时查询出该订单所属的用户

在这里插入图片描述

在只查询order表的时候,也要查询user表,所以需要将所有数据全部查出进行封装SELECT *,o.id oid FROM orders o,USER u WHERE o.uid=u.id

在这里插入图片描述

创建Order和User实体

order

1
2
3
4
5
6
public class Order {
    private int id;
    private Date ordertime;
    private double total;
    //表示当前订单属于哪一个用户
    private User user;

user

1
2
3
4
5
public class User {
    private int id;
    private String username;
    private String password;
    private Date birthday;

创建OrderMapper接口

1
2
3
4
public interface UserMapper {
//查询全部的方法
    public List<Order> findAll();
}

配置OrderMapper.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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.zg.mapper.OrderMapper" >
 
    <resultMap id="orderMap" type="order">
        <!--手动指定字段与实体的映射关系-->
        <!--comlumn:数据表的字段名称 property:实体的属性名称-->
        <id column="oid" property="id"></id>
        <result column="ordertime" property="ordertime"></result>
        <result column="total" property="total"></result>
        <result column="uid" property="user.id"></result>
        <result column="username" property="user.username"></result>
        <result column="password" property="user.password"></result>
        <result column="birthday" property="user.birthday"></result>
 
    </resultMap>
 
    <!--这里不能使用resultType=“order”,因为order中没有user中的字段,只有一个user对象-->
    <select id="findAll" resultMap="orderMap">
        SELECT *,o.id oid FROM orders o,USER u WHERE o.uid=u.id
    </select>
 
</mapper>

sqlMapConfig.xml

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
<?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">
<!--主要配置mybatis的核心配置-->
<configuration>
    <!--通过properties标签加载外部properties文件-->
    <properties resource="jdbc.properties"></properties>
    <!--自定义别名-->
    <typeAliases>
        <typeAlias type="com.zg.domain.User" alias="user"></typeAlias>
        <typeAlias type="com.zg.domain.Order" alias="order"></typeAlias>
    </typeAliases>
 
    <!--配置当前数据源的环境-->
    <environments default="developement">
        <environment id="developement">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
    <!--加载映射文件-->
    <mappers>
        <mapper resource="com.zg.mapper/UserMapper.xml"></mapper>
        <mapper resource="com.zg.mapper/OrderMapper.xml"></mapper>
    </mappers>
 
 
</configuration>

在这里插入图片描述

在一对一配置的时候,在order实体中创建了一个user,所以property属性都使用user.** 的方式进行编写,但是这里还可以使用association

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
<?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.zg.mapper.OrderMapper" >
 
    <resultMap id="orderMap" type="order">
        <!--手动指定字段与实体的映射关系-->
        <!--comlumn:数据表的字段名称 property:实体的属性名称-->
        <id column="oid" property="id"></id>
        <result column="ordertime" property="ordertime"></result>
        <result column="total" property="total"></result>
        <!--<result column="uid" property="user.id"></result>
        <result column="username" property="user.username"></result>
        <result column="password" property="user.password"></result>
        <result column="birthday" property="user.birthday"></result>-->
        <!--以上被封装到user内的还可以使用association进行配置-->
        <!--association匹配的意思,在order中有个属性叫user-->
        <!-- property="user"当前order实体中的属性名称,javaType="user"当前实体order中的属性类型user-->
        <association property="user" javaType="user">
            <id column="uid" property="id"></id>
            <result column="username" property="username"></result>
            <result column="password" property="password"></result>
            <result column="birthday" property="birthday"></result>
        </association>
         
 
    </resultMap>
 
    <!--这里不能使用resultType=“order”,因为order中没有user中的字段,只有一个user对象-->
    <select id="findAll" resultMap="orderMap">
        SELECT *,o.id oid FROM orders o,USER u WHERE o.uid=u.id
    </select>
 
</mapper>

一对多查询的模型

用户表和订单表的关系为,一个用户有多个订单,一个当但只从属一个用户
一对多查询需求:查询一个用户,与此同时查询出该用户具有的订单

在这里插入图片描述

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
package com.zg.domain;
 
import java.util.Date;
import java.util.List;
 
public class User {
    private int id;
    private String username;
    private String password;
    private Date birthday;
 
    //描述当前用户存在哪些订单
    private List<Order>  orderList;
 
    public List<Order> getOrderList() {
        return orderList;
    }
 
    public void setOrderList(List<Order> orderList) {
        this.orderList = orderList;
    }
 
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", birthday=" + birthday +
                ", orderList=" + orderList +
                '}';
    }
 
    public int getId() {
        return id;
    }
 
    public void setId(int id) {
        this.id = id;
    }
 
    public String getUsername() {
        return username;
    }
 
    public void setUsername(String username) {
        this.username = username;
    }
 
    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
 
    public Date getBirthday() {
        return birthday;
    }
 
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}

在这里插入图片描述

修改User实体

Java技术迷

在这里插入图片描述

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
package com.zg.domain;
 
import java.util.Date;
import java.util.List;
 
public class User {
    private int id;
    private String username;
    private String password;
    private Date birthday;
 
    //描述当前用户存在哪些订单
    private List<Order>  orderList;
 
    public List<Order> getOrderList() {
        return orderList;
    }
 
    public void setOrderList(List<Order> orderList) {
        this.orderList = orderList;
    }
 
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", birthday=" + birthday +
                ", orderList=" + orderList +
                '}';
    }
 
    public int getId() {
        return id;
    }
 
    public void setId(int id) {
        this.id = id;
    }
 
    public String getUsername() {
        return username;
    }
 
    public void setUsername(String username) {
        this.username = username;
    }
 
    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {