mybitis 返回Map 而不是List處理

關於使用mybitis返回Map集合 而不是List<Map>集合 java

在網上也找過一些資料,可能是拿 spring

sqlMapClient.queryForMap的例子 sql

說事,要不就是用List<Map>的例子來拿分走人 apache

小白的我想找個完整例子偷懶,無奈不遂,只好本身動手豐衣足食了 session

關於原理,且看以下分解 粗陋之處多多見諒 mybatis

查看SqlSession 接口 app

具體關於Map集合的 以下截圖 this

clip_image002

若是直接使用返回的對象爲List<Map> spa

而不是Map .net

查看其具體實現被DefaultSqlSession SqlSessionManager SqlSessionTemplate 實現

clip_image004

此時調用的爲DefaultSqlSession

查看其對應代碼

其中關鍵部分爲DefaultResultHandler :處理結果集的地方

clip_image006

查看DefaultMapHandler具體處理 如圖所示

clip_image008

其中是實現了ResultHandler的接口

關於ResultHandler 以下圖所示

clip_image010

根據以上關鍵問題已經出來了

在DefaultMapResultHandler 中的結果處理

即以下圖部分

clip_image012

因此本身寫了個Map結果集的處理

如圖所示

clip_image014

部分有重合 將就着看

clip_image016

=============分割線

代碼部分

先貼下相關配置

<bean id="sqlSessionFactory_bit" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dataSource_bit" />

<property name="configLocation" value="classpath:bit-sql-map-config.xml" />

</bean>

<bean id="userDAO"

class="UserDAO">

<property name="sqlSessionFactory" ref="sqlSessionFactory_bit" />

</bean>

Sql語句

<select id="getOsListByDate" parameterType="BaseCriteria " resultType="java.util.Map">

select OS ,OS_COUNT,CREATED_DATE

from VIEW_USER_OS

where CREATED_DATE=#{createdDate}

<if test="isPaging">

limit #{startIndex}, #{offset}

</if>

</select>

先列下代碼

/**

* 條件查詢

* @author skyyan

*

*/

public class BaseCriteria {

private static final long serialVersionUID=7580485976991375195L;

private int pageSize;

private int pageNo;

private String createdDate;

private boolean isPaging;//是否分頁若是爲true則表示分頁,若是爲 false則表示不分頁

public int getPageSize() {

return pageSize;

}

public void setPageSize(int pageSize) {

this.pageSize=pageSize;

}

public int getPageNo() {

return pageNo;

}

public void setPageNo(int pageNo) {

this.pageNo=pageNo;

}

public int getStartIndex() {

if(pageNo<=0){

this.pageNo=1;

}

return (pageNo - 1) * pageSize;

}

public int getOffset() {

return pageSize;

}

public boolean isPaging() {

return isPaging;

}

public void setPaging(boolean isPaging) {

this.isPaging = isPaging;

}

public void setCreatedDate(String createdDate){

this.createdDate=createdDate;

}

public String getCreatedDate() {

return createdDate;

}

}

======Map 結果集處理類

import java.util.Map;

import org.apache.ibatis.reflection.MetaObject;

import org.apache.ibatis.reflection.factory.ObjectFactory;

import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;

import org.apache.ibatis.session.ResultContext;

import org.apache.ibatis.session.ResultHandler;

/***

* mybitis 返回Map對象處理

* @author skyyan

*

* @param <K>

* @param <V>

*/

@SuppressWarnings({"unchecked","rawtypes"})

public class MapResultHandler<K, V> implements ResultHandler {

private final Map mappedResults;

private final String mapKey;

private final ObjectFactory objectFactory;

private final ObjectWrapperFactory objectWrapperFactory;

private final String mapValue;

public MapResultHandler(String mapKey, String mapValue,ObjectFactory objectFactory,

ObjectWrapperFactory objectWrapperFactory) {

this.objectFactory = objectFactory;

this.objectWrapperFactory = objectWrapperFactory;

this.mappedResults = ((Map) objectFactory.create(Map.class));

this.mapKey = mapKey;

this.mapValue=mapValue;

}

public void handleResult(ResultContext context) {

Object value = context.getResultObject();

Object key=null;

if(value instanceof Map){

Map<K,V> temp=(Map<K,V>)value;

value=temp.get(this.mapValue);

key=temp.get(this.mapKey);

}

if(key==null){

MetaObject mo = MetaObject.forObject( context.getResultObject(), this.objectFactory,

this.objectWrapperFactory);

key = mo.getValue(this.mapKey);

}

this.mappedResults.put(key, value);

}

public Map<K, V> getMappedResults() {

return this.mappedResults;

}

}

**************DAO實現

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import org.apache.ibatis.executor.result.DefaultResultContext;

import org.apache.ibatis.reflection.factory.ObjectFactory;

import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;

import org.apache.ibatis.session.RowBounds;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import BaseCriteria;

import MapResultHandler;

/***

*

* @author skyyan

*

* @param <K>

* @param <V>

*/

public class BaseDAO<K,V> extends SqlSessionDaoSupport{

/****

* 返回Map對象<String,String>

* @param paramString 執行的語句 如bit_clientUser_statistics.getOsListByDate

* @param criteria 參數

* @param mapKey 所返集合的key;此名字與查詢語句中的列名對應,表示已那個字段做爲key

* @param mapValue 所返集合的value,此名字與查詢語句中的列名對應,表示已那個字段做爲value

* @return

*/

public Map<K,V> getMap(String paramString,BaseCriteria criteria,String mapKey,String mapValue){

ObjectFactory objectFactory = this.getSqlSession().getConfiguration()

.getObjectFactory();

List list = this.getSqlSession().selectList(paramString, criteria,RowBounds.DEFAULT);

ObjectWrapperFactory objectWrapperFactory = this.getSqlSession()

.getConfiguration().getObjectWrapperFactory();

MapResultHandler mapResultHandler = new MapResultHandler(

mapKey,mapValue, objectFactory, objectWrapperFactory);

DefaultResultContext context = new DefaultResultContext();

for (Iterator i$ = list.iterator(); i$.hasNext();) {

Object o = i$.next();

context.nextResultObject(o);

mapResultHandler.handleResult(context);

}

Map selectedMap = mapResultHandler.getMappedResults();

return selectedMap;

}

}

能夠直接使用

clip_image018

clip_image020

返回結果

clip_image022

 

 

注意:可是此時須要注意一些排重問題,由於這個是統計用 因此不存在key重複的狀況,因此看具體應用狀況,在具體自行處理

相關文章
相關標籤/搜索