Browse Source

mybatis 各种查询功能

zhang 2 years ago
parent
commit
1713e97d43

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

@@ -3,11 +3,13 @@ package com.mapper;
3
 import com.pojo.dto.AccountVO;
3
 import com.pojo.dto.AccountVO;
4
 import com.pojo.entity.AccountEntity;
4
 import com.pojo.entity.AccountEntity;
5
 import com.pojo.entity.AccountRealEntity;
5
 import com.pojo.entity.AccountRealEntity;
6
+import org.apache.ibatis.annotations.MapKey;
6
 import org.apache.ibatis.annotations.Mapper;
7
 import org.apache.ibatis.annotations.Mapper;
7
 import org.springframework.stereotype.Component;
8
 import org.springframework.stereotype.Component;
8
 import org.springframework.stereotype.Repository;
9
 import org.springframework.stereotype.Repository;
9
 
10
 
10
 import java.util.List;
11
 import java.util.List;
12
+import java.util.Map;
11
 
13
 
12
 public interface AccountMapper {
14
 public interface AccountMapper {
13
     List<AccountEntity> findAll(Integer id);
15
     List<AccountEntity> findAll(Integer id);
@@ -17,6 +19,8 @@ public interface AccountMapper {
17
     // 多对一查询
19
     // 多对一查询
18
     AccountVO findVO(Integer id);
20
     AccountVO findVO(Integer id);
19
     AccountVO findByStep(Integer id);
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
 package com.mapper;
1
 package com.mapper;
2
 
2
 
3
 import com.pojo.entity.UserEntity;
3
 import com.pojo.entity.UserEntity;
4
+import org.apache.ibatis.annotations.MapKey;
4
 import org.apache.ibatis.annotations.Mapper;
5
 import org.apache.ibatis.annotations.Mapper;
5
 import org.apache.ibatis.annotations.Param;
6
 import org.apache.ibatis.annotations.Param;
6
 
7
 
@@ -14,4 +15,7 @@ public interface UserMapper {
14
     UserEntity findUserByParam(String name,Integer age);
15
     UserEntity findUserByParam(String name,Integer age);
15
     UserEntity findUserByMap(Map<String,Object> map);
16
     UserEntity findUserByMap(Map<String,Object> map);
16
     UserEntity findUserByAnnoParam(@Param("users") UserEntity userEntity);
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
     <select id="findByStep" parameterType="java.lang.Integer" resultMap="finByStepMap">
51
     <select id="findByStep" parameterType="java.lang.Integer" resultMap="finByStepMap">
52
         select * from account where id= #{id}
52
         select * from account where id= #{id}
53
     </select>
53
     </select>
54
+    <select id="findAllMap"  resultType="map">
55
+        select * from account
56
+    </select>
54
 </mapper>
57
 </mapper>

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

@@ -44,5 +44,12 @@
44
         select *
44
         select *
45
         from user where name = #{param1.name}  and age = #{users.age};
45
         from user where name = #{param1.name}  and age = #{users.age};
46
     </select>
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
 </mapper>
55
 </mapper>

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

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

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

@@ -10,6 +10,7 @@ import org.junit.Test;
10
 import java.io.IOException;
10
 import java.io.IOException;
11
 import java.io.InputStream;
11
 import java.io.InputStream;
12
 import java.util.HashMap;
12
 import java.util.HashMap;
13
+import java.util.Map;
13
 
14
 
14
 public class TestUserParam {
15
 public class TestUserParam {
15
     UserMapper userMapper;
16
     UserMapper userMapper;
@@ -41,4 +42,18 @@ public class TestUserParam {
41
         userEntity.setAge(11);
42
         userEntity.setAge(11);
42
         UserEntity userByAnnoParam = userMapper.findUserByAnnoParam(userEntity);
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
 }