zhangxiaoyu лет назад: 2
Родитель
Сommit
419080d470

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

@@ -1,5 +1,6 @@
1 1
 package com.mapper;
2 2
 
3
+import com.pojo.dto.AccountVO;
3 4
 import com.pojo.entity.AccountEntity;
4 5
 import com.pojo.entity.AccountRealEntity;
5 6
 import org.apache.ibatis.annotations.Mapper;
@@ -13,4 +14,8 @@ public interface AccountMapper {
13 14
     void updateOne(AccountEntity accountEntity);
14 15
     void insertOne(AccountEntity accountEntity);
15 16
     AccountRealEntity findByResultMap();
17
+    // 多对一查询
18
+    AccountVO findVO(Integer id);
19
+
20
+
16 21
 }

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

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

+ 17 - 0
mybatis_train/src/main/java/com/pojo/dto/AccountVO.java

@@ -0,0 +1,17 @@
1
+package com.pojo.dto;
2
+
3
+import com.pojo.entity.AccountEntity;
4
+import com.pojo.entity.DepartmentEntity;
5
+import lombok.Data;
6
+
7
+@Data
8
+public class AccountVO  {
9
+    private Integer id;
10
+    private String userName;
11
+    private Double balance;
12
+    // 部门Id
13
+    private Integer depId;
14
+    // 部门信息
15
+    private DepartmentEntity departmentEntity;
16
+
17
+}

+ 13 - 0
mybatis_train/src/main/java/com/pojo/dto/DepartmentVO.java

@@ -0,0 +1,13 @@
1
+package com.pojo.dto;
2
+
3
+import com.pojo.entity.AccountEntity;
4
+import com.pojo.entity.DepartmentEntity;
5
+import lombok.Data;
6
+
7
+import java.util.List;
8
+
9
+@Data
10
+public class DepartmentVO extends DepartmentEntity {
11
+    private List<AccountEntity> accountEntities;
12
+
13
+}

+ 2 - 0
mybatis_train/src/main/java/com/pojo/entity/AccountRealEntity.java

@@ -7,4 +7,6 @@ public class AccountRealEntity {
7 7
     private Integer id;
8 8
     private String userName;
9 9
     private Double balance;
10
+    // 部门Id
11
+    private Integer depId;
10 12
 }

+ 9 - 0
mybatis_train/src/main/java/com/pojo/entity/DepartmentEntity.java

@@ -0,0 +1,9 @@
1
+package com.pojo.entity;
2
+
3
+import lombok.Data;
4
+
5
+@Data
6
+public class DepartmentEntity {
7
+    public Integer depId;
8
+    public String depName;
9
+}

+ 17 - 0
mybatis_train/src/main/resources/com/mapper/AccountMapper.xml

@@ -7,6 +7,18 @@
7 7
         <result property="userName" column="user_name"></result>
8 8
         <result property="balance" column="balance"></result>
9 9
     </resultMap>
10
+    <resultMap id="voMap" type="com.pojo.dto.AccountVO">
11
+        <id property="id" column="id"></id>
12
+        <result property="balance" column="balance"></result>
13
+        <result property="depId" column="dep_id"></result>
14
+        <result property="userName" column="user_name"></result>
15
+<!--        association专门用来处理多对一关系映射-->
16
+        <association property="departmentEntity" javaType="com.pojo.entity.DepartmentEntity">
17
+            <id property="depId" column="dep_id"></id>
18
+            <result property="depName" column="dep_name"></result>
19
+        </association>
20
+    </resultMap>
21
+
10 22
     <select id="findAll" resultType="account"
11 23
             parameterType="java.lang.Integer"
12 24
     >
@@ -21,4 +33,9 @@
21 33
     <select id="findByResultMap" resultType="com.pojo.entity.AccountRealEntity">
22 34
         select * from account where id = 3
23 35
     </select>
36
+    <select id="findVO" resultMap="voMap" parameterType="java.lang.Integer">
37
+        select a.*,b.dep_name from account a left join department b on a.dep_id = b.dep_id
38
+        where a.id = #{id}
39
+    </select>
40
+
24 41
 </mapper>

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

@@ -0,0 +1,22 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<!DOCTYPE mapper
3
+        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4
+        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5
+<mapper namespace="com.mapper.DepartmentMapper">
6
+    <resultMap id="oneMore" type="com.pojo.dto.DepartmentVO">
7
+        <id property="depId" column="dep_id"></id>
8
+        <result property="depName" column="dep_name"></result>
9
+        <collection property="accountEntities" ofType="com.pojo.entity.AccountRealEntity">
10
+            <id property="id" column="id"></id>
11
+            <result property="userName" column="user_name"></result>
12
+            <result property="balance" column="balance"></result>
13
+            <result property="depId" column="dep_id"></result>
14
+        </collection>
15
+
16
+    </resultMap>
17
+    <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
19
+        where a.dep_id = #{id1}
20
+    </select>
21
+
22
+</mapper>

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

@@ -34,6 +34,7 @@
34 34
 <!--        使用class方式引入-->
35 35
         <mapper class="com.mapper.AccountMapper"></mapper>
36 36
         <mapper class="com.mapper.UserMapper"></mapper>
37
+        <mapper class="com.mapper.DepartmentMapper"></mapper>
37 38
 <!--        <package name="com.mapper"/>-->
38 39
     </mappers>
39 40
 

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

@@ -1,4 +1,6 @@
1
+import com.mapper.AccountMapper;
1 2
 import com.mapper.UserMapper;
3
+import com.pojo.dto.AccountVO;
2 4
 import com.pojo.entity.UserEntity;
3 5
 import org.apache.ibatis.io.Resources;
4 6
 import org.apache.ibatis.session.SqlSession;
@@ -20,6 +22,7 @@ import java.util.List;
20 22
 public class Test3 {
21 23
     UserMapper userMapper;
22 24
     UserMapper userMapper2;
25
+    AccountMapper accountMapper;
23 26
     SqlSession sqlSession;
24 27
     SqlSession sqlSession2;
25 28
     @Before
@@ -71,6 +74,8 @@ public class Test3 {
71 74
         List<UserEntity> userForEach1 = mapper1.findUserForEach(integers);
72 75
         sqlSession.close();
73 76
         List<UserEntity> userForEach2 = mapper2.findUserForEach(integers);
77
+        accountMapper = sqlSession2.getMapper(AccountMapper.class);
78
+        AccountVO vo = accountMapper.findVO(16);
74 79
 
75 80
     }
76 81
 

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

@@ -0,0 +1,37 @@
1
+import com.mapper.AccountMapper;
2
+import com.mapper.DepartmentMapper;
3
+import com.mapper.UserMapper;
4
+import com.pojo.dto.AccountVO;
5
+import com.pojo.dto.DepartmentVO;
6
+import org.apache.ibatis.io.Resources;
7
+import org.apache.ibatis.session.SqlSession;
8
+import org.apache.ibatis.session.SqlSessionFactory;
9
+import org.apache.ibatis.session.SqlSessionFactoryBuilder;
10
+import org.junit.Before;
11
+import org.junit.Test;
12
+
13
+import java.io.IOException;
14
+import java.io.InputStream;
15
+
16
+public class TestResultMap {
17
+    SqlSession sqlSession;
18
+    AccountMapper accountMapper;
19
+    DepartmentMapper departmentMapper;
20
+    @Before
21
+    public void before() throws IOException {
22
+        InputStream resourceAsStream = Resources.getResourceAsStream("mybatisConfig.xml");
23
+        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
24
+        sqlSession = build.openSession(true);
25
+        accountMapper = sqlSession.getMapper(AccountMapper.class);
26
+        departmentMapper = sqlSession.getMapper(DepartmentMapper.class);
27
+    }
28
+
29
+    @Test
30
+    public void resultMap1() {
31
+        AccountVO vo = accountMapper.findVO(16);
32
+    }
33
+    @Test
34
+    public void findOneMore() {
35
+        DepartmentVO departmentVO = departmentMapper.oneFindMore(1);
36
+    }
37
+}