JDBC------ORM

一、ORM(Object Relationship Mapping,对象关系映射)的基本思想:

1)表结构跟类对应,表中字段和类的属性对应,表中记录和对象对应;

2)让javabean的属性名和类型尽量和数据库保持一致!

3)一条记录对应一个对象。将这些查询到的对象放到容器中(List,Set,Map)

  • 将表中的一条记录封装到Object数组中
  • 将表中的一条记录封装到Map中
  • 将表中的一条记录封装到javabean对象中
/*
* 测试使用Object[] 来封装一条记录
* 使用List<Object[]>存储多条记录
* */
public class Demo01 {
    public static void main(String[] args) {
        Connection conn = JDBCUtil.getMysqlConn();
        PreparedStatement ps =null;
        ResultSet rs = null;
        List<Object[]> list = new ArrayList<>();
        try {
            ps = conn.prepareStatement("select * from emp where id>?");
            ps.setInt(1, 1);
            rs = ps.executeQuery();
            while (rs.next()) {
                Object[] objs = new Object[3];//一个Object数组封装了一条记录的信息
                objs[0] = rs.getString(2);
                objs[1] = rs.getDouble(3);
                objs[2] = rs.getObject(4);
                list.add(objs);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.close(rs, ps, conn);
        }
        for (Object[] objs : list) {
            System.out.println(""+objs[0]+"-->" + objs[1] +"-->"+ objs[2]);
        }

    }
}
/*
* 测试使用Map 来封装一条记录
* 使用List<Map>Map<Map>存储多条记录
* */
public class Demo02 {
    public static void test01() {
        Connection conn = JDBCUtil.getMysqlConn();
        PreparedStatement ps =null;
        ResultSet rs = null;
        Map<String, Object> row = new HashMap<>(); //使用一个Map封装一条记录
        try {
            ps = conn.prepareStatement("select * from emp where id=?");
            ps.setInt(1, 1);
            rs = ps.executeQuery();
            while (rs.next()) {
                row.put("id", rs.getInt(1));
                row.put("empname", rs.getString(2));
                row.put("salary", rs.getObject(3));
                row.put("age", rs.getObject(5));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.close(rs, ps, conn);
        }

        //遍历Map,就是遍历这一行的多列信息
        for (String key : row.keySet()) {
            System.out.print(key + "--" + row.get(key) + "\t");
        }
    }

    public static void test02() {
        Connection conn = JDBCUtil.getMysqlConn();
        PreparedStatement ps =null;
        ResultSet rs = null;
        List<Map<String, Object>> list = new ArrayList<>();
        try {
            ps = conn.prepareStatement("select * from emp where id>?");
            ps.setInt(1, 1);
            rs = ps.executeQuery();
            while (rs.next()) {
                Map<String, Object> row = new HashMap<>();
                row.put("id", rs.getInt(1));
                row.put("empname", rs.getString(2));
                row.put("salary", rs.getObject(3));
                row.put("age", rs.getObject(5));
                list.add(row);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.close(rs, ps, conn);
        }

        for (Map<String,Object> row : list) {
            //遍历Map,就是遍历这一行的多列信息
            for (String key : row.keySet()) {
                System.out.print(key + "--" + row.get(key) + "\t");
            }

            System.out.println();
        }

    }

    public static void test03() {
        Connection conn = JDBCUtil.getMysqlConn();
        PreparedStatement ps =null;
        ResultSet rs = null;
        Map<String, Map<String, Object>> maps = new HashMap<>();
        try {
            ps = conn.prepareStatement("select * from emp where id>?");
            ps.setInt(1, 1);
            rs = ps.executeQuery();
            while (rs.next()) {
                Map<String, Object> row = new HashMap<>();
                row.put("id", rs.getInt(1));
                row.put("empname", rs.getString(2));
                row.put("salary", rs.getObject(3));
                row.put("age", rs.getObject(5));
                maps.put(rs.getString(2), row);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.close(rs, ps, conn);
        }

        for (String empname : maps.keySet()) {
             Map<String,Object> row =maps.get(empname);
            //遍历Map,就是遍历这一行的多列信息
            for (String key : row.keySet()) {
                System.out.print(key + "--" +row.get(key) + "\t");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {

        test03();
    }
}

/*
* 测试使用javabean对象 来封装一条记录
* 使用List<javabean>存储多条记录
* */
public class Demo03 {

    public static void test01() {
        Connection conn = JDBCUtil.getMysqlConn();
        PreparedStatement ps =null;
        ResultSet rs = null;
        Emp emp = null;

        try {
            ps = conn.prepareStatement("select * from emp where id=?");
            ps.setInt(1, 1);
            rs = ps.executeQuery();
            while (rs.next()) {
                emp = new Emp(rs.getInt(1),
                        rs.getString(2),
                        rs.getDouble(3),
                        rs.getDate(4),
                        rs.getInt(5),
                        rs.getInt(6));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.close(rs, ps, conn);
        }

        System.out.println(emp.toString());
    }
    public static void test02() {
        Connection conn = JDBCUtil.getMysqlConn();
        PreparedStatement ps =null;
        ResultSet rs = null;
        List<Emp> list = new ArrayList<>();

        try {
            ps = conn.prepareStatement("select * from emp where id>?");
            ps.setInt(1, 1);
            rs = ps.executeQuery();
            while (rs.next()) {
                Emp emp = new Emp();
                emp = new Emp(rs.getInt(1),
                        rs.getString(2),
                        rs.getDouble(3),
                        rs.getDate(4),
                        rs.getInt(5),
                        rs.getInt(6));

                list.add(emp);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.close(rs, ps, conn);
        }

        for (Emp emp : list) {
            System.out.println(emp.toString());
        }
    }

    public static void main(String[] args) {
        test01();
        test02();
    }
}

猜你喜欢

转载自blog.csdn.net/yuyinghe0612/article/details/80546483
Orm
今日推荐