Mybatis Select Returns a value of map, select two columns of the table field as Key,value

Source: Internet
Author: User



The project needs to be upgraded from Ibatis to Mybatis,dao with a method that returns the map type, which is a query statement that queries two fields and returns the value of the result list field A as the value of the key field B as a result;



Ibatis DAO inherits the Sqlmapclientdaosupport class queryForMap (string statement, Object param, string key, String value) method can be directly implemented;



MyBatis sqlsession only Selectmap (string statement, Object parameter, string mapkey), this method assigns the specified field in the result set as the key, Value is the element object of the result set list, the source code is as follows:


 
/** * The selectMap is a special case in that it is designed to convert a list
   * of results into a Map based on one of the properties in the resulting
   * objects.
   * @param <K> the returned Map keys type
   * @param <V> the returned Map values type
   * @param statement Unique identifier matching the statement to use.
   * @param parameter A parameter object to pass to the statement.
   * @param mapKey The property to use as key for each value in the list.
   * @return Map containing key pair data. */ <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey);


On the internet to check some blog posts, the final reference: http://blog.csdn.net/sou_liu/article/details/47755635, organized as follows:



The main idea is: rewrite the Resulthandler interface, and then use the Sqlsession Select method, the XML inside the mapping file return value is configured to HashMap on it.



XML configuration:


 
 
<resultMap id="ipDeptResult" type="java.util.HashMap">
    <result property="key" column="ip"/>
    <result property="value" column="dept"/>
</resultMap>

SQL query statement select out two fields IP and Dept,resultmap configured as the Ipdeptresult defined above.



Look at the implementation of handler know why Resultmap inside the property written key and value, in fact, is completely custom. Handler calls the Handleresult method on each record of the result set, we rewrite it for format conversion.


 
 
public class MapResultHandler implements ResultHandler {
     @SuppressWarnings ("rawtypes")
     private final Map mappedResults = new HashMap ();

     @SuppressWarnings ("unchecked")
     @Override
     public void handleResult (ResultContext context) {
         @SuppressWarnings ("rawtypes")
         Map map = (Map) context.getResultObject ();
         mappedResults.put (map.get ("key"), map.get ("value")); // property value in xml configuration, corresponding column
     }
     public Map getMappedResults () {
         return mappedResults;
     }
}


Call class:




 
@Override public <K,V> Map<K,V> queryForMap(String statement, Object params) {
    MapResultHandler handler = new MapResultHandler();
    sqlSession.select(statement, params, handler); return handler.getMappedResults();
}





Mybatis Select Returns a value of map, select two columns of the table field as Key,value


Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.