Browse Source

mybatis project 分步查询提交

zhang 2 years ago
parent
commit
c9f9fd41ec

+ 1 - 0
mybatis_train/src/main/java/com/mapper/AccountMapper.java

@@ -16,6 +16,7 @@ public interface AccountMapper {
16
     AccountRealEntity findByResultMap();
16
     AccountRealEntity findByResultMap();
17
     // 多对一查询
17
     // 多对一查询
18
     AccountVO findVO(Integer id);
18
     AccountVO findVO(Integer id);
19
+    AccountVO findByStep(Integer id);
19
 
20
 
20
 
21
 
21
 }
22
 }

+ 2 - 1
mybatis_train/src/main/java/com/mapper/DepartmentMapper.java

@@ -1,10 +1,11 @@
1
 package com.mapper;
1
 package com.mapper;
2
 
2
 
3
 import com.pojo.dto.DepartmentVO;
3
 import com.pojo.dto.DepartmentVO;
4
+import com.pojo.entity.DepartmentEntity;
4
 
5
 
5
 public interface DepartmentMapper {
6
 public interface DepartmentMapper {
6
 
7
 
7
     // 一对多查询
8
     // 一对多查询
8
     DepartmentVO oneFindMore(Integer id);
9
     DepartmentVO oneFindMore(Integer id);
9
-
10
+    DepartmentEntity findById(Integer depId);
10
 }
11
 }

+ 16 - 2
mybatis_train/src/main/resources/com/mapper/AccountMapper.xml

@@ -18,7 +18,18 @@
18
             <result property="depName" column="dep_name"></result>
18
             <result property="depName" column="dep_name"></result>
19
         </association>
19
         </association>
20
     </resultMap>
20
     </resultMap>
21
-
21
+    <resultMap id="finByStepMap" type="com.pojo.dto.AccountVO">
22
+        <id property="id" column="id"></id>
23
+        <result property="userName" column="user_name"></result>
24
+        <result property="balance" column="balance"></result>
25
+        <result property="depId" column="dep_id"></result>
26
+        <association property="departmentEntity"
27
+            select="com.mapper.DepartmentMapper.findById"
28
+                     column="dep_id"
29
+        ></association>
30
+    </resultMap>
31
+    
32
+    
22
     <select id="findAll" resultType="account"
33
     <select id="findAll" resultType="account"
23
             parameterType="java.lang.Integer"
34
             parameterType="java.lang.Integer"
24
     >
35
     >
@@ -37,5 +48,8 @@
37
         select a.*,b.dep_name from account a left join department b on a.dep_id = b.dep_id
48
         select a.*,b.dep_name from account a left join department b on a.dep_id = b.dep_id
38
         where a.id = #{id}
49
         where a.id = #{id}
39
     </select>
50
     </select>
40
-
51
+<!--    分步查询 1-->
52
+    <select id="findByStep" parameterType="java.lang.Integer" resultMap="finByStepMap">
53
+        select * from account where id= #{id}
54
+    </select>
41
 </mapper>
55
 </mapper>

+ 4 - 1
mybatis_train/src/main/resources/com/mapper/DepartmentMapper.xml

@@ -12,8 +12,11 @@
12
             <result property="balance" column="balance"></result>
12
             <result property="balance" column="balance"></result>
13
             <result property="depId" column="dep_id"></result>
13
             <result property="depId" column="dep_id"></result>
14
         </collection>
14
         </collection>
15
-
16
     </resultMap>
15
     </resultMap>
16
+
17
+    <select id="findById" parameterType="java.lang.Integer" resultType="com.pojo.entity.DepartmentEntity">
18
+        select * from department where dep_id = #{depId}
19
+    </select>
17
     <select id="oneFindMore" parameterType="java.lang.Integer" resultMap="oneMore">
20
     <select id="oneFindMore" parameterType="java.lang.Integer" resultMap="oneMore">
18
         select a.*,b.* from department a left join account b on a.dep_id = b.dep_id
21
         select a.*,b.* from department a left join account b on a.dep_id = b.dep_id
19
         where a.dep_id = #{id1}
22
         where a.dep_id = #{id1}

+ 6 - 0
mybatis_train/src/test/java/Test3.java

@@ -32,6 +32,7 @@ public class Test3 {
32
         sqlSession = build.openSession(true);
32
         sqlSession = build.openSession(true);
33
         sqlSession2 = build.openSession(true);
33
         sqlSession2 = build.openSession(true);
34
         userMapper = sqlSession.getMapper(UserMapper.class);
34
         userMapper = sqlSession.getMapper(UserMapper.class);
35
+        accountMapper = sqlSession.getMapper(AccountMapper.class);
35
     }
36
     }
36
     @Test
37
     @Test
37
     public void testCondition()  {
38
     public void testCondition()  {
@@ -78,6 +79,11 @@ public class Test3 {
78
         AccountVO vo = accountMapper.findVO(16);
79
         AccountVO vo = accountMapper.findVO(16);
79
 
80
 
80
     }
81
     }
82
+    @Test
83
+    public void testFindStep() {
84
+        AccountVO byStep = accountMapper.findByStep(16);
85
+    }
86
+
81
 
87
 
82
     @Test
88
     @Test
83
     public void delete() {
89
     public void delete() {