Bläddra i källkod

mybatis project 分步查询提交

zhang 2 år sedan
förälder
incheckning
c9f9fd41ec

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

@@ -16,6 +16,7 @@ public interface AccountMapper {
16 16
     AccountRealEntity findByResultMap();
17 17
     // 多对一查询
18 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 1
 package com.mapper;
2 2
 
3 3
 import com.pojo.dto.DepartmentVO;
4
+import com.pojo.entity.DepartmentEntity;
4 5
 
5 6
 public interface DepartmentMapper {
6 7
 
7 8
     // 一对多查询
8 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 18
             <result property="depName" column="dep_name"></result>
19 19
         </association>
20 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 33
     <select id="findAll" resultType="account"
23 34
             parameterType="java.lang.Integer"
24 35
     >
@@ -37,5 +48,8 @@
37 48
         select a.*,b.dep_name from account a left join department b on a.dep_id = b.dep_id
38 49
         where a.id = #{id}
39 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 55
 </mapper>

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

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

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

@@ -32,6 +32,7 @@ public class Test3 {
32 32
         sqlSession = build.openSession(true);
33 33
         sqlSession2 = build.openSession(true);
34 34
         userMapper = sqlSession.getMapper(UserMapper.class);
35
+        accountMapper = sqlSession.getMapper(AccountMapper.class);
35 36
     }
36 37
     @Test
37 38
     public void testCondition()  {
@@ -78,6 +79,11 @@ public class Test3 {
78 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 88
     @Test
83 89
     public void delete() {