Ver código fonte

mybatis 各种查询功能

zhang 2 anos atrás
pai
commit
1713e97d43

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

@@ -3,11 +3,13 @@ package com.mapper;
3 3
 import com.pojo.dto.AccountVO;
4 4
 import com.pojo.entity.AccountEntity;
5 5
 import com.pojo.entity.AccountRealEntity;
6
+import org.apache.ibatis.annotations.MapKey;
6 7
 import org.apache.ibatis.annotations.Mapper;
7 8
 import org.springframework.stereotype.Component;
8 9
 import org.springframework.stereotype.Repository;
9 10
 
10 11
 import java.util.List;
12
+import java.util.Map;
11 13
 
12 14
 public interface AccountMapper {
13 15
     List<AccountEntity> findAll(Integer id);
@@ -17,6 +19,8 @@ public interface AccountMapper {
17 19
     // 多对一查询
18 20
     AccountVO findVO(Integer id);
19 21
     AccountVO findByStep(Integer id);
22
+    @MapKey("dep_id")
23
+    Map<Integer,AccountRealEntity> findAllMap();
20 24
 
21 25
 
22 26
 }

+ 4 - 0
mybatis_train/src/main/java/com/mapper/UserMapper.java

@@ -1,6 +1,7 @@
1 1
 package com.mapper;
2 2
 
3 3
 import com.pojo.entity.UserEntity;
4
+import org.apache.ibatis.annotations.MapKey;
4 5
 import org.apache.ibatis.annotations.Mapper;
5 6
 import org.apache.ibatis.annotations.Param;
6 7
 
@@ -14,4 +15,7 @@ public interface UserMapper {
14 15
     UserEntity findUserByParam(String name,Integer age);
15 16
     UserEntity findUserByMap(Map<String,Object> map);
16 17
     UserEntity findUserByAnnoParam(@Param("users") UserEntity userEntity);
18
+    Map<String,Object> findByMap(@Param("id") Integer id);
19
+    @MapKey("address")
20
+    Map<String,Object> findAllByMap();
17 21
 }

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

@@ -51,4 +51,7 @@
51 51
     <select id="findByStep" parameterType="java.lang.Integer" resultMap="finByStepMap">
52 52
         select * from account where id= #{id}
53 53
     </select>
54
+    <select id="findAllMap"  resultType="map">
55
+        select * from account
56
+    </select>
54 57
 </mapper>

+ 8 - 1
mybatis_train/src/main/resources/com/mapper/UserMapper.xml

@@ -44,5 +44,12 @@
44 44
         select *
45 45
         from user where name = #{param1.name}  and age = #{users.age};
46 46
     </select>
47
-
47
+    <select id="findByMap" resultType="map">
48
+        select *
49
+        from user where id = #{id}
50
+    </select>
51
+    <select id="findAllByMap" resultType="map">
52
+        select *
53
+        from user
54
+    </select>
48 55
 </mapper>

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

@@ -1,6 +1,7 @@
1 1
 import com.mapper.AccountMapper;
2 2
 import com.mapper.UserMapper;
3 3
 import com.pojo.dto.AccountVO;
4
+import com.pojo.entity.AccountRealEntity;
4 5
 import com.pojo.entity.UserEntity;
5 6
 import org.apache.ibatis.io.Resources;
6 7
 import org.apache.ibatis.session.SqlSession;
@@ -14,6 +15,7 @@ import java.io.IOException;
14 15
 import java.io.InputStream;
15 16
 import java.util.ArrayList;
16 17
 import java.util.List;
18
+import java.util.Map;
17 19
 
18 20
 /**
19 21
  * 动态sql查询
@@ -85,6 +87,11 @@ public class Test3 {
85 87
         System.out.println(byStep.getUserName());
86 88
         return;
87 89
     }
90
+    @Test
91
+    public void testAllMap() {
92
+        Map<Integer, AccountRealEntity> allMap = accountMapper.findAllMap();
93
+
94
+    }
88 95
 
89 96
 
90 97
     @Test

+ 15 - 0
mybatis_train/src/test/java/TestUserParam.java

@@ -10,6 +10,7 @@ import org.junit.Test;
10 10
 import java.io.IOException;
11 11
 import java.io.InputStream;
12 12
 import java.util.HashMap;
13
+import java.util.Map;
13 14
 
14 15
 public class TestUserParam {
15 16
     UserMapper userMapper;
@@ -41,4 +42,18 @@ public class TestUserParam {
41 42
         userEntity.setAge(11);
42 43
         UserEntity userByAnnoParam = userMapper.findUserByAnnoParam(userEntity);
43 44
     }
45
+
46
+    /**
47
+     * 测试查到到map
48
+     *
49
+     */
50
+    @Test
51
+    public void findByMap() {
52
+        Map<String, Object> byMap = userMapper.findByMap(1);
53
+        System.out.println(byMap.get("name"));
54
+    }
55
+    @Test
56
+    public void findAllByMap() {
57
+        Map<String, Object> byMap = userMapper.findAllByMap();
58
+    }
44 59
 }