Bläddra i källkod

mybatis project 一对多 分步查询提交

zhang 2 år sedan
förälder
incheckning
45e37101aa

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

@@ -13,7 +13,7 @@ public interface AccountMapper {
13 13
     List<AccountEntity> findAll(Integer id);
14 14
     void updateOne(AccountEntity accountEntity);
15 15
     void insertOne(AccountEntity accountEntity);
16
-    AccountRealEntity findByResultMap();
16
+    AccountRealEntity findByDepId(Integer depId);
17 17
     // 多对一查询
18 18
     AccountVO findVO(Integer id);
19 19
     AccountVO findByStep(Integer id);

+ 3 - 0
mybatis_train/src/main/java/com/mapper/DepartmentMapper.java

@@ -8,4 +8,7 @@ public interface DepartmentMapper {
8 8
     // 一对多查询
9 9
     DepartmentVO oneFindMore(Integer id);
10 10
     DepartmentEntity findById(Integer depId);
11
+    // 一对多分步查询
12
+    DepartmentVO findByStep(Integer id);
13
+
11 14
 }

+ 4 - 5
mybatis_train/src/main/resources/com/mapper/AccountMapper.xml

@@ -25,11 +25,10 @@
25 25
         <result property="depId" column="dep_id"></result>
26 26
         <association property="departmentEntity"
27 27
             select="com.mapper.DepartmentMapper.findById"
28
-                     column="dep_id"
28
+            column="dep_id"
29 29
         ></association>
30 30
     </resultMap>
31
-    
32
-    
31
+
33 32
     <select id="findAll" resultType="account"
34 33
             parameterType="java.lang.Integer"
35 34
     >
@@ -41,8 +40,8 @@
41 40
     <update id="updateOne" parameterType="account">
42 41
         update account set user_name= #{user_name},balance=#{balance} where id = #{id}
43 42
     </update>
44
-    <select id="findByResultMap" resultType="com.pojo.entity.AccountRealEntity">
45
-        select * from account where id = 3
43
+    <select id="findByDepId" resultType="com.pojo.entity.AccountRealEntity">
44
+        select * from account where dep_id = #{dep_id}
46 45
     </select>
47 46
     <select id="findVO" resultMap="voMap" parameterType="java.lang.Integer">
48 47
         select a.*,b.dep_name from account a left join department b on a.dep_id = b.dep_id

+ 14 - 0
mybatis_train/src/main/resources/com/mapper/DepartmentMapper.xml

@@ -13,10 +13,24 @@
13 13
             <result property="depId" column="dep_id"></result>
14 14
         </collection>
15 15
     </resultMap>
16
+    <resultMap id="findByStep" type="com.pojo.dto.DepartmentVO">
17
+        <id property="depId" column="dep_id"></id>
18
+        <result property="depName" column="dep_name"></result>
19
+        <collection property="accountEntities"
20
+                    select="com.mapper.AccountMapper.findByDepId"
21
+                    column="dep_id">
22
+        </collection>
23
+    </resultMap>
24
+
16 25
 
17 26
     <select id="findById" parameterType="java.lang.Integer" resultType="com.pojo.entity.DepartmentEntity">
18 27
         select * from department where dep_id = #{depId}
19 28
     </select>
29
+
30
+    <select id="findByStep" parameterType="java.lang.Integer" resultMap="findByStep">
31
+        select * from department where dep_id = #{depId}
32
+    </select>
33
+
20 34
     <select id="oneFindMore" parameterType="java.lang.Integer" resultMap="oneMore">
21 35
         select a.*,b.* from department a left join account b on a.dep_id = b.dep_id
22 36
         where a.dep_id = #{id1}

+ 1 - 0
mybatis_train/src/main/resources/mybatisConfig.xml

@@ -7,6 +7,7 @@
7 7
     <settings>
8 8
         <setting name="logImpl" value="LOG4J2"/>
9 9
         <setting name="mapUnderscoreToCamelCase" value="true"/>
10
+        <setting name="lazyLoadingEnabled" value="true"/>
10 11
     </settings>
11 12
     <!--    Mapper文件注册-->
12 13
     <typeAliases>

+ 1 - 1
mybatis_train/src/test/java/Test1.java

@@ -51,6 +51,6 @@ public class Test1 {
51 51
         SqlSessionFactory sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder().build(resourceAsStream);
52 52
         SqlSession sqlSession = sqlSessionFactoryBuilder.openSession(true);
53 53
         AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
54
-        AccountRealEntity byResultMap = mapper.findByResultMap();
54
+//        AccountRealEntity byResultMap = mapper.findByResultMap();
55 55
     }
56 56
 }

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

@@ -82,6 +82,8 @@ public class Test3 {
82 82
     @Test
83 83
     public void testFindStep() {
84 84
         AccountVO byStep = accountMapper.findByStep(16);
85
+        System.out.println(byStep.getUserName());
86
+        return;
85 87
     }
86 88
 
87 89
 

+ 13 - 0
mybatis_train/src/test/java/TestResultMap.java

@@ -30,8 +30,21 @@ public class TestResultMap {
30 30
     public void resultMap1() {
31 31
         AccountVO vo = accountMapper.findVO(16);
32 32
     }
33
+
34
+    /**
35
+     * 测试一对多
36
+     */
33 37
     @Test
34 38
     public void findOneMore() {
35 39
         DepartmentVO departmentVO = departmentMapper.oneFindMore(1);
36 40
     }
41
+    /**
42
+     * 测试一对多 分步
43
+     *
44
+     */
45
+    @Test
46
+    public void findOneByStep() {
47
+        DepartmentVO byStep = departmentMapper.findByStep(1);
48
+        System.out.println(byStep.getDepName());
49
+    }
37 50
 }