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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?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">
<!--namespace对应mapper接口所在的位置-->
<mapper namespace="com.dy.pipIrrGlobal.daoDemo.DemoUserMapper">
    <resultMap id="userMap" type="com.dy.pipIrrGlobal.pojoDemo.DemoUser">
        <id property="id" column="id" />
        <result property="name" column="name"/>
        <result property="password" column="password"/>
        <result property="roleId" column="roleId"/>
        <result property="disabled" column="disabled" typeHandler="com.dy.common.mybatis.envm.EnumCodeTypeHandler" javaType="com.dy.common.mybatis.envm.Disabled"/>
        <result property="deleted" column="deleted" typeHandler="com.dy.common.mybatis.envm.EnumCodeTypeHandler" javaType="com.dy.common.mybatis.envm.Deleted"/>
    </resultMap>
 
    <resultMap id="addressMap" type="com.dy.pipIrrGlobal.pojoDemo.DemoAddress">
        <id property="id" column="id" />
        <result property="name" column="name"/>
        <result property="userId" column="userId"/>
    </resultMap>
 
    <resultMap id="userAddressMap1" extends="userMap" type="com.dy.pipIrrGlobal.pojoDemo.DemoUser">
        <association property="address" columnPrefix="addr_" resultMap="addressMap" />
    </resultMap>
 
    <resultMap id="userAddressMap2" extends="userMap" type="com.dy.pipIrrGlobal.pojoDemo.DemoUser">
        <association property="address" columnPrefix="addr_" resultMap="com.dy.pipIrrGlobal.daoDemo.DemoAddressMapper.addressMap" />
    </resultMap>
 
    <resultMap id="userAddressMap3" extends="userMap" type="com.dy.pipIrrGlobal.pojoDemo.DemoUser">
        <association property="address" column="{userId=id}" select="com.dy.pipIrrGlobal.daoDemo.DemoAddressMapper.selectByUserId" />
    </resultMap>
 
    <!-- fetchType="lazy" 还需要配置aggressiveLazyLoading=false -->
    <resultMap id="userAddressMap4" extends="userMap" type="com.dy.pipIrrGlobal.pojoDemo.DemoUser">
        <association property="address" fetchType="lazy" column="{userId=id}" select="com.dy.pipIrrGlobal.daoDemo.DemoAddressMapper.selectByUserId" />
    </resultMap>
 
    <!-- 关联的嵌套结果映射,优点:数据库查询次数少,压力小,缺点:SQL复杂 -->
    <select id="selectById1" resultType="com.dy.pipIrrGlobal.pojoDemo.DemoUser">
        select u.*,
               adr.id "address.id",
               adr.name "address.name",
               adr.userId "address.userId"
        from demo_user u
        inner join demo_address adr on u.id = adr.userId
        where u.id = #{id}
    </select>
 
 
    <!-- 关联的嵌套结果映射,优点:数据库查询次数少,压力小,缺点:SQL复杂 -->
    <select id="selectById2" resultMap="userAddressMap1">
        select u.*,
               adr.id addr_id,
               adr.name addr_name,
               adr.userId addr_userId
        from demo_user u
                 inner join demo_address adr on u.id = adr.userId
        where u.id = #{id}
    </select>
 
 
    <!-- 关联的嵌套结果映射,优点:数据库查询次数少,压力小,缺点:SQL复杂 -->
    <select id="selectById3" resultMap="userAddressMap2">
        select u.*,
               adr.id addr_id,
               adr.name addr_name,
               adr.userId addr_userId
        from demo_user u
                 inner join demo_address adr on u.id = adr.userId
        where u.id = #{id}
    </select>
 
 
 
    <!-- 关联嵌套查询,优点:SQL简单,缺点:数据库可能查询多次数,压力大, -->
    <select id="selectById4" resultMap="userAddressMap3">
        select u.*
        from demo_user u
        where u.id = #{id}
    </select>
 
 
    <!-- 关联嵌套查询,优点:SQL简单,缺点:数据库可能查询多次数,压力大, -->
    <select id="selectById5" resultMap="userAddressMap4">
        select u.*
        from demo_user u
        where u.id = #{id}
    </select>
 
    <!-- 第三方关联查询 -->
    <select id="selectByRoleId" resultMap="userAddressMap4">
        select * from demo_user where roleId=#{roleId}
    </select>
 
    <!--对应mapper接口方法public void insert(DemoUser user);-->
    <insert id="putin" parameterType="com.dy.pipIrrGlobal.pojoDemo.DemoUser">
        insert into
        demo_user (id, name, password, roleId, disabled, deleted)
        values (#{id},#{name},#{password},#{roleId},
                #{disabled, typeHandler=com.dy.common.mybatis.envm.EnumCodeTypeHandler, jdbcType=TINYINT},
                #{deleted, typeHandler=com.dy.common.mybatis.envm.EnumCodeTypeHandler, jdbcType=TINYINT}
                );
    </insert>
 
</mapper>