- package test.ORMapping;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * 这是一个用来解决在查询过程
- * 那些自动设置
- * <pre>
- * Eg1:
- * ptmt.setString(1,vo.getString(xxx));
- * ptmt.setInt(2,vo.getInt(xxx))
- * Eg 2:
- * ptmt.setName(rs.getString(xxx));
- * </pre>
- * 用来解决这些设置时麻烦的事,动态自动实例化成对象,
- * 在下面的事例中Eg1没有给出.
- * <code>
- * 特别注明:在查询过程所要使用的sql语句有一定的限制:
- * 必需是如:select id as Id ,name as Name from test where id = 2;
- * </code>
- * @author xiangyuan
- * @version 1.0
- */
- public class AutoMapping {
- /**
- * 得到所有的表中字段名称
- * @param rs
- * @return
- * @throws SQLException
- */
- public String[] getColumns(ResultSet rs) throws SQLException {
- ResultSetMetaData rmeta = rs.getMetaData();
- int columns = rmeta.getColumnCount();
- String[] colNames = new String[columns];
- for(int index = 1;index < columns;index ++) {
- colNames[index - 1] = rmeta.getColumnLabel(index);
- }
- return colNames;
- }
- /**
- * 根据传入的sql语句与类与封装对应的对象
- * @param rs
- * @param clazz
- * @return
- * @throws SQLException
- * @throws IllegalAccessException
- * @throws InstantiationException
- * @throws InvocationTargetException
- * @throws IllegalArgumentException
- */
- @SuppressWarnings("unchecked")
- public Object setObjectValues(ResultSet rs,Class clazz) throws SQLException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
- String[] colNames = getColumns(rs);
- Object obj = null;
- Method[] methods = clazz.getMethods();
- if (rs.next()) {
- obj = clazz.newInstance();
- invokeMethod(obj,rs,colNames,methods);
- }
- return obj;
- }
- /**
- * 得到一个集合对象
- * @param rs
- * @param clazz
- * @return
- * @throws SQLException
- * @throws InstantiationException
- * @throws IllegalAccessException
- * @throws IllegalArgumentException
- * @throws InvocationTargetException
- */
- @SuppressWarnings("unchecked")
- public List<Object> getAllObjects(ResultSet rs, Class clazz)
- throws SQLException, InstantiationException,
- IllegalAccessException, IllegalArgumentException, InvocationTargetException {
- String[] colNames = getColumns(rs);
- Object obj = null;
- Method[] methods = clazz.getMethods();
- List<Object> list = new ArrayList<Object>();
- while (rs.next()) {
- obj = clazz.newInstance();
- invokeMethod(obj,rs,colNames,methods);
- list.add(obj);
- }
- return list;
- }
- /**
- * 动态加载值
- * @param colNames
- * @param methods
- * @throws SQLException
- * @throws InvocationTargetException
- * @throws IllegalAccessException
- * @throws IllegalArgumentException
- */
- private void invokeMethod(Object obj,ResultSet rs,String[] colNames,Method[] methods) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SQLException {
- for (int i = 0; i < colNames.length; i++) {
- String column = colNames[i];
- String method = "set" + column;
- for(Method m : methods) {
- if (m.getName().equals(method)) {
- m.invoke(obj,rs.getObject(column));
- break;
- }
- }
- }
- }
- }