java架构师之路(持续更新中)

1:

数据访问方式:

数据持久层,对象和关系数据库之间的企业级映射解决方案,解决--对象范例和关系范例之间的不匹配。

J2EE的三层结构是指表示层(Presentation),业务逻辑层(Business Logic)以及基础架构层(Infrastructure)

解决方案:

许多开发者用JDBC进行数据库程序的开发,此中方式很多情况下都使用DAO模式,采用SQL进行查询。不过JDBC是低级别的数据库访问方式,JDBC并不支持面向对象的数据库表示。JDBC数据库表示完全围绕关系数据库模型。在大型应用程序的DAO中书写这样的代码,维护量是非常大的

2:

JDBC实现查询分析:七步

1、加载JDBC驱动

2、创建连接

3、创建对象JDBC statements 对象

4、sql语句传参

5、获取结果

6、转换处理结果

7、关闭连接

JDBC源码:

public static List<Map<String,Object>> queryForList(){  
    Connection connection = null;  
    ResultSet rs = null;  
    PreparedStatement stmt = null;  
    List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();  
          
    try {  
        // 加载JDBC驱动  
        Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();  
        String url = "jdbc:oracle:thin:@localhost:1521:ORACLEDB";  
              
        String user = "trainer";   
        String password = "trainer";   
              
        // 获取数据库连接  
        connection = DriverManager.getConnection(url,user,password);   
              
        String sql = "select * from userinfo where user_id = ? ";  
        // 创建Statement对象(每一个Statement为一次数据库执行请求)  
        stmt = connection.prepareStatement(sql);  
              
        // 设置传入参数  
        stmt.setString(1, "zhangsan");  
              
        // 执行SQL语句  
        rs = stmt.executeQuery();  
              
        // 处理查询结果(将查询结果转换成List<Map>格式)  
        ResultSetMetaData rsmd = rs.getMetaData();  
        int num = rsmd.getColumnCount();  
              
        while(rs.next()){  
            Map map = new HashMap();  
            for(int i = 0;i < num;i++){  
                String columnName = rsmd.getColumnName(i+1);  
                map.put(columnName,rs.getString(columnName));  
            }  
            resultList.add(map);  
        }  
              
    } catch (Exception e) {  
        e.printStackTrace();  
    } finally {  
        try {  
            // 关闭结果集  
            if (rs != null) {  
                rs.close();  
                rs = null;  
            }  
            // 关闭执行  
            if (stmt != null) {  
                stmt.close();  
                stmt = null;  
            }  
            if (connection != null) {  
                connection.close();  
                connection = null;  
            }  
        } catch (SQLException e) {  
            e.printStackTrace();  
        }  
    }        
    return resultList;  
}

3:

JDBC演变到Mybatis过程

第一步优化:

原文地址:https://www.cnblogs.com/westlife-11358/p/12006314.html