Tuesday, January 21, 2014

[JAVA] Automatic Mapping ValueObject(VO) Example

It was very useful.
On spring framework.



source code:
package com.kaudo.common.util;

import org.apache.commons.collections.map.ListOrderedMap;

public class DataMap extends ListOrderedMap {

    private static final long serialVersionUID=6723434363565852261L;

    @Override
    public Object put(Object key, Object value){
        return super.put(convert2CamelCase((String)key),value);
    }

    public static String convert2CamelCase(String underScore){
        if (underScore.indexOf('_') < 0
            && Character.isLowerCase(underScore.charAt(0))){
            return underScore;
        }
        StringBuilder result = new StringBuilder();
        boolean nextUpper = false;
        int len = underScore.length();
        for (int i = 0; i < len; i++){
            char currentChar = underScore.charAt(i);
            if (currentChar == '_'){
                nextUpper = true;
            } else {
                if (nextUpper){
                    result.append(Character.toUpperCase(currentChar));
                    nextUpper = false;
                } else {
                    result.append(Character.toLowerCase(currentChar));
                }
            }
        }
        return result.toString();
    }
}
when used in ibatis xml
<sqlMap namespace="LolDao">

 <typeAlias  alias="lolVo" type="com.kaudo.www.lol.vo.LolVo"/>
 <typeAlias  alias="dataMap" type="com.kaudo.common.util.DataMap"/>
...
...
...
 <select id="selChampionList" resultClass="dataMap"  parameterClass="lolVo">
  <![CDATA[
  SELECT /*+ LolDao.selChampionList 목록 조회 */
   id,
   name,
   displayName,
   title,
...
...
when used in dao
...
...
 @SuppressWarnings("unchecked")
 public List selChampionList(LolVo lolVo) throws SQLException {
  return (List)this.getSqlMapClient().queryForList("LolDao.selChampionList",lolVo);
 }
...
...
...

No comments:

Post a Comment