zoukankan      html  css  js  c++  java
  • JDBC 利用反射技术将查询结果封装为对象(简单ORM实现)

    ORM(Object Relational Mapping)对象关系映射

     

    public class ORMTest {

        public static void main(String[] args) throws SQLException,

                IllegalAccessException, InvocationTargetException, Exception {

            User user = (User) getObject(

                    "select id as Id, name as Name, birthday as Birthday, money as Money  from user where id=1",

                    User.class);

            System.out.println(user);

     

            Bean b = (Bean) getObject(

                    "select id as Id, name as Name, birthday as Birthday, money as Money from user where id=1",

                    Bean.class);

            System.out.println(b);

        }

     

        static List<Object> getObjects(String sql, Class clazz)

                throws SQLException, Exception, IllegalAccessException,

                InvocationTargetException {

            Connection conn = null;

            PreparedStatement ps = null;

            ResultSet rs = null;

            try {

                conn = JdbcUtils.getConnection();

                ps = conn.prepareStatement(sql);

                rs = ps.executeQuery();

                String[] colNames = getColNames(rs);

     

                List<Object> objects = new ArrayList<Object>();

                Method[] ms = clazz.getMethods();

                while (rs.next()) {

                    Object object = clazz.newInstance();

                    for (int i = 0; i < colNames.length; i++) {

                        String colName = colNames[i];

                        String methodName = "set" + colName;

                        // Object value = rs.getObject(colName);

                        // try {

                        // Method m = clazz

                        // .getMethod(methodName, value.getClass());

                        // if (m != null)

                        // m.invoke(object, value);

                        // } catch (NoSuchMethodException e) {

                        // e.printStackTrace();

                        // //

                        // }

                        for (Method m : ms) {

                            if (methodName.equals(m.getName())) {

                                m.invoke(object, rs.getObject(colName));

                                break;

                            }

                        }

                        objects.add(object);

                    }

                }

                return objects;

            } finally {

                JdbcUtils.free(rs, ps, conn);

            }

        }

     

        private static String[] getColNames(ResultSet rs) throws SQLException {

            ResultSetMetaData rsmd = rs.getMetaData();

            int count = rsmd.getColumnCount();

            String[] colNames = new String[count];

            for (int i = 1; i <= count; i++) {

                colNames[i - 1] = rsmd.getColumnLabel(i);

            }

            return colNames;

        }

     

        static Object getObject(String sql, Class clazz) throws SQLException,

                Exception, IllegalAccessException, InvocationTargetException {

            Connection conn = null;

            PreparedStatement ps = null;

            ResultSet rs = null;

            try {

                conn = JdbcUtils.getConnection();

                ps = conn.prepareStatement(sql);

                rs = ps.executeQuery();

                String[] colNames = getColNames(rs);

     

                Object object = null;

                Method[] ms = clazz.getMethods();

                if (rs.next()) {

                    object = clazz.newInstance();

                    for (int i = 0; i < colNames.length; i++) {

                        String colName = colNames[i];

                        String methodName = "set" + colName;

                        // Object value = rs.getObject(colName);

                        // try {

                        // Method m = clazz

                        // .getMethod(methodName, value.getClass());

                        // if (m != null)

                        // m.invoke(object, value);

                        // } catch (NoSuchMethodException e) {

                        // e.printStackTrace();

                        // //

                        // }

                        for (Method m : ms) {

                            if (methodName.equals(m.getName())) {

                                m.invoke(object, rs.getObject(colName));

                                break;

                            }

                        }

                    }

                }

                return object;

            } finally {

                JdbcUtils.free(rs, ps, conn);

            }

        }

    }

  • 相关阅读:
    企业移动化?AppCan教你正确的打开方式
    企业该如何挑选移动平台?
    除了移动开发,一个好平台还需要具备什么功能?
    canvas绘制工作流之绘制节点
    canvas与工作流的不解之缘
    一个工作流引擎诞生前的准备工作
    欢迎大家Follow me!微软MVP罗勇(Dynamics CRM方向2015-2018年)欢迎您!
    Dynamics 365定制:在实体的列表界面添加按钮
    Dynamics 365 Customer Engagement中自定义工作流活动的调试
    Dynamics CRM 2015/2016新特性之二十七:使用Web API查询元数据
  • 原文地址:https://www.cnblogs.com/flying607/p/3462760.html
Copyright © 2011-2022 走看看