Mybatis source code analysis: @Mapkey use

@Mapkey use

  When multi-valued queries, usually make the method return type is set to List <Dto> type, Mybatis provides another way for us to solve, in the form of KV query results are saved in a Map, this implementation requires only @Mapkey can be labeled on the method.

  The following code: a student query table contains id, name, age three fields, using the id number @mapkey as K, as a result of the query using @Mapkey V. K is preferably set to a unique, otherwise the result will follow overwrites the query to the results, value if set @Mapkey set is empty or does not match with the results of the field, then found records K became null, the query result is always the last one. This can be the source of DefaultMapResultHandler <K, V> derived class, code, see last, final K key = (K) mo.getValue (mapKey); @Mapkey this code is the type specified in the corresponding field in the query the value value value value then the key as the final result.

 1 package com.zzz.mybatis.mapper;
 2 
 3 import java.util.List;
 4 import java.util.Map;
 5 
 6 import org.apache.ibatis.annotations.Flush;
 7 import org.apache.ibatis.annotations.MapKey;
 8 import org.apache.ibatis.annotations.Select;
 9 
10 public interface SqlMapper {
11     
12     public List<Map<String, Object>> list();
13     
14     @Flush
15     public void flush();
16     
17     @MapKey(value="id")
18     @Select("select id,name,age from student")
19     public Map<String,Map<String,String>> listByMapkey();
20 }

 

Related test class

 1 package com.zzz.mybatis.service;
 2 
 3 import java.util.List;
 4 import java.util.Map;
 5 
 6 import org.apache.ibatis.session.SqlSession;
 7 import org.apache.ibatis.session.SqlSessionFactory;
 8 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 9 import org.junit.Before;
10 import org.junit.Test;
11 
12 import com.zzz.mybatis.mapper.SqlMapper;
13 import com.zzz.mybatis.mapper.StudentMapper;
14 
15 public class StudentMapperTest {
16     private SqlSession session;
17     
18     private  StudentMapper mapper;
19     
20     @Before
21     public void before() {
22         SqlSessionFactoryBuilder builder =new SqlSessionFactoryBuilder();
23         SqlSessionFactory sessionFactory=builder.build(Class.class.getResourceAsStream("/mybatis-config.xml"));
24         this.session=sessionFactory.openSession(false);
25     }
26     @Test
27     public void getStuByMapkey() {
28         mapper=session.getMapper(StudentMapper.class);
29         mapper.flush();
30         Map<String, Map<String, String>> rs=mapper.listByMapkey();
31         System.out.println(rs.toString());
32     }
33 }
The test results 
{1 = {name = zhangsan, id = 1, age = 20}, 2 = {name = lisi, id = 2, age = 30}, 3 = {name = wangwu, id = 3, age = 40} }

 

DefaultMapResultHandler<K, V>

1   @Override
2   public void handleResult(ResultContext<? extends V> context) {
3     final V value = context.getResultObject();
4     final MetaObject mo = MetaObject.forObject(value, objectFactory, objectWrapperFactory, reflectorFactory);
5     // TODO is that assignment always true?
6     final K key = (K) mo.getValue(mapKey);
7     mappedResults.put(key, value);
8   }

 

Guess you like

Origin www.cnblogs.com/zhengzuozhanglina/p/11223594.html