Package com.loc.pojo

Source Code of com.loc.pojo.BaseBean

package com.loc.pojo;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

public abstract class BaseBean {

  @SuppressWarnings("unchecked")
  public static RowMapper mapRow(final Class clazz) throws Exception{
    return new RowMapper(){
      public Object mapRow(ResultSet resultset, int rowNum)
          throws SQLException {
        Field[] fields = clazz.getDeclaredFields();
        Object o = null;
        try {
          o = clazz.newInstance();
        } catch (InstantiationException e1) {
          e1.printStackTrace();
        } catch (IllegalAccessException e1) {
          e1.printStackTrace();
        }
        for(Field field:fields){
          Method m;
          try {
            m = clazz.getMethod("set"+toUpp(field.getName()), new Class[]{field.getType()});
            m.invoke(o, new Object[]{resultset.getObject(field.getName())});
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
        return o;
      }
      private String toUpp(String str) {
        if (str != null && str != "") {
          str = str.substring(0, 1).toUpperCase() + str.substring(1);
        }
        return str;
      }
    };
  }
}
TOP

Related Classes of com.loc.pojo.BaseBean

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.