Java tutorial
/** * Copyright 2016 benjobs * <p> * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package com.jredrain.dao; import org.apache.commons.lang3.StringUtils; import com.jredrain.common.utils.IgnoreCaseMap; import org.hibernate.transform.BasicTransformerAdapter; import java.io.Serializable; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; /** * Map,key??,value,key?? * */ @SuppressWarnings("unchecked") public class MapResultTransFormer extends BasicTransformerAdapter implements Serializable { private static final long serialVersionUID = -6669576443089592161L; /** * ? */ public static interface ColumnHandler { /** * ?,?null,result,key name<br/> * ??,null,?{@link MapResultTransFormer} * * @param name ?? * @param value * @param result Map * @return */ Object handle(String name, Object value, Map<String, Object> result); } /** * ? */ protected ColumnHandler columnHandler; /** * ???? */ protected Map<String, String> aliasMap = new HashMap<String, String>(); /** * */ public MapResultTransFormer() { } /** * @param columnHandler ?,????? */ public MapResultTransFormer(ColumnHandler columnHandler) { this.columnHandler = columnHandler; } /** * ?????<br/> * : ?List("userName","userId"),SQL:<code> select username,userID from table</code> ??userName,userId<br/> * <code> select * from table </code>?? * * @param aliases ?? * @param columnHandler ?,????? */ public MapResultTransFormer(List<String> aliases, ColumnHandler columnHandler) { this(columnHandler); for (String alias : aliases) { setAlias(alias, alias); } } /** * ??????<br/> * : ?Map("userName":"name","userId":"id"),SQL:<code> select username,userID from table</code> ??name,id<br/> * <code> select * from table </code>????<br/> * ?:?Mapkey?? * * @param aliasMap ?? * @param columnHandler ?,????? */ public MapResultTransFormer(Map<String, String> aliasMap, ColumnHandler columnHandler) { this(columnHandler); for (Entry<String, String> entry : aliasMap.entrySet()) { setAlias(entry.getKey(), entry.getValue()); } } /** * {@inheritDoc} */ public Object transformTuple(Object[] tuple, String[] aliases) { Map result = new IgnoreCaseMap(tuple.length); for (int i = 0; i < tuple.length; i++) { String name = getAlias(aliases[i]); Object value = tuple[i]; if (columnHandler != null) { try { if ((value = columnHandler.handle(name, value, result)) != null) { result.put(name, value); } } catch (Exception e) { System.err.println("failed to handle alias[" + name + "] with value[" + value + "]"); } } else { result.put(name, value); } } return result; } /** * @param columnHandler ?,???? */ public MapResultTransFormer setColumnHandler(ColumnHandler columnHandler) { this.columnHandler = columnHandler; return this; } /** * ?? * * @param name */ protected void setAlias(String name, String alias) { aliasMap.put(name.toLowerCase(), alias); } /** * ???? * * @param name * @return */ protected String getAlias(String name) { return StringUtils.defaultIfEmpty(aliasMap.get(name.toLowerCase()), name); } }