Browse Source

mybatis project 一对多 分步查询提交

zhang 2 years ago
parent
commit
45e37101aa

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

@@ -13,7 +13,7 @@ public interface AccountMapper {
13
     List<AccountEntity> findAll(Integer id);
13
     List<AccountEntity> findAll(Integer id);
14
     void updateOne(AccountEntity accountEntity);
14
     void updateOne(AccountEntity accountEntity);
15
     void insertOne(AccountEntity accountEntity);
15
     void insertOne(AccountEntity accountEntity);
16
-    AccountRealEntity findByResultMap();
16
+    AccountRealEntity findByDepId(Integer depId);
17
     // 多对一查询
17
     // 多对一查询
18
     AccountVO findVO(Integer id);
18
     AccountVO findVO(Integer id);
19
     AccountVO findByStep(Integer id);
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
     DepartmentVO oneFindMore(Integer id);
9
     DepartmentVO oneFindMore(Integer id);
10
     DepartmentEntity findById(Integer depId);
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
         <result property="depId" column="dep_id"></result>
25
         <result property="depId" column="dep_id"></result>
26
         <association property="departmentEntity"
26
         <association property="departmentEntity"
27
             select="com.mapper.DepartmentMapper.findById"
27
             select="com.mapper.DepartmentMapper.findById"
28
-                     column="dep_id"
28
+            column="dep_id"
29
         ></association>
29
         ></association>
30
     </resultMap>
30
     </resultMap>
31
-    
32
-    
31
+
33
     <select id="findAll" resultType="account"
32
     <select id="findAll" resultType="account"
34
             parameterType="java.lang.Integer"
33
             parameterType="java.lang.Integer"
35
     >
34
     >
@@ -41,8 +40,8 @@
41
     <update id="updateOne" parameterType="account">
40
     <update id="updateOne" parameterType="account">
42
         update account set user_name= #{user_name},balance=#{balance} where id = #{id}
41
         update account set user_name= #{user_name},balance=#{balance} where id = #{id}
43
     </update>
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
     </select>
45
     </select>
47
     <select id="findVO" resultMap="voMap" parameterType="java.lang.Integer">
46
     <select id="findVO" resultMap="voMap" parameterType="java.lang.Integer">
48
         select a.*,b.dep_name from account a left join department b on a.dep_id = b.dep_id
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
             <result property="depId" column="dep_id"></result>
13
             <result property="depId" column="dep_id"></result>
14
         </collection>
14
         </collection>
15
     </resultMap>
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
     <select id="findById" parameterType="java.lang.Integer" resultType="com.pojo.entity.DepartmentEntity">
26
     <select id="findById" parameterType="java.lang.Integer" resultType="com.pojo.entity.DepartmentEntity">
18
         select * from department where dep_id = #{depId}
27
         select * from department where dep_id = #{depId}
19
     </select>
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
     <select id="oneFindMore" parameterType="java.lang.Integer" resultMap="oneMore">
34
     <select id="oneFindMore" parameterType="java.lang.Integer" resultMap="oneMore">
21
         select a.*,b.* from department a left join account b on a.dep_id = b.dep_id
35
         select a.*,b.* from department a left join account b on a.dep_id = b.dep_id
22
         where a.dep_id = #{id1}
36
         where a.dep_id = #{id1}

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

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

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

@@ -51,6 +51,6 @@ public class Test1 {
51
         SqlSessionFactory sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder().build(resourceAsStream);
51
         SqlSessionFactory sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder().build(resourceAsStream);
52
         SqlSession sqlSession = sqlSessionFactoryBuilder.openSession(true);
52
         SqlSession sqlSession = sqlSessionFactoryBuilder.openSession(true);
53
         AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
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
     @Test
82
     @Test
83
     public void testFindStep() {
83
     public void testFindStep() {
84
         AccountVO byStep = accountMapper.findByStep(16);
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
     public void resultMap1() {
30
     public void resultMap1() {
31
         AccountVO vo = accountMapper.findVO(16);
31
         AccountVO vo = accountMapper.findVO(16);
32
     }
32
     }
33
+
34
+    /**
35
+     * 测试一对多
36
+     */
33
     @Test
37
     @Test
34
     public void findOneMore() {
38
     public void findOneMore() {
35
         DepartmentVO departmentVO = departmentMapper.oneFindMore(1);
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
 }