Java 持久层概述

JDBC

Java Database Connectivity 是一系列接口规范。Java 程序都是通过 JDBC 连接数据库的、然后通过其执行SQL、对数据库进行操作。

DBC 只是 Sun 公司定义的接口规范、具体实现是交由各个数据库厂商去实现的、因为每个数据库都有其特殊性、这些是 Java 规范没办法确定的

微信公众号:CoderLi

 import java.sql.*;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 ​
 public class JdbcExample {
 ​
     public static void main(String[] args) {
         JdbcExample example = new JdbcExample();
         Role role = example.getRole(1L);
         System.out.printf("role_name => " + role.getRoleName());
     }
 ​
     public Role getRole(Long id) {
         Connection connection = this.getConnection();
         PreparedStatement ps = null;
         ResultSet rs = null;
         try {
             // 操作 Connection,打开 Statement 对象
             ps = connection.prepareStatement("select id,role_name,note from t_role where id = ?");
             ps.setLong(1,id);
             // 通过 Statement 执行 SQL,返回结果到 ResultSet 对象
             rs = ps.executeQuery();
             // 通过 ResultSet 读取数据,然后通过代码转化为具体的 POJO 对象
             while (rs.next()){
                 Long roleId = rs.getLong("id");
                 String roleName = rs.getString("role_name");
                 String note = rs.getString("note");
                 Role role = new Role();
                 role.setId(id);
                 role.setRoleName(roleName);
                 role.setNote(note);
                 return role;
             }
         } catch (SQLException e) {
             Logger.getLogger(JdbcExample.class.getName()).log(Level.SEVERE,null,e);
         } finally {
             this.close(rs,ps,connection);
         }
         return null;
     }
     private Connection getConnection(){
         // 使用 JDBC 编程需要连接数据库,注册驱动和数据库信息
         Connection connection = null;
         try {
             Class.forName("com.mysql.jdbc.Driver");
             String url = "jdbc:mysql://localhost:3306/mybatis1?characterEncoding=utf8";
             String user = "root";
             String password = "root";
             connection = DriverManager.getConnection(url,user,password);
         } catch (ClassNotFoundException | SQLException e) {
             Logger.getLogger(JdbcExample.class.getName()).log(Level.SEVERE,null,e);
             return null;
         }
         return connection;
     }
     private void close(ResultSet rs,Statement stmt,Connection connection){
         // 关闭数据库相关资源
         try {
             if (rs != null && !rs.isClosed()){
                 rs.close();
             }
         } catch (SQLException e) {
             Logger.getLogger(JdbcExample.class.getName()).log(Level.SEVERE,null,e);
         }
         try {
             if (stmt != null && !stmt.isClosed()){
                 stmt.close();
             }
         } catch (SQLException e) {
             Logger.getLogger(JdbcExample.class.getName()).log(Level.SEVERE,null,e);
         }
         try {
             if (connection != null && !connection.isClosed()){
                 connection.close();
             }
         } catch (SQLException e) {
             Logger.getLogger(JdbcExample.class.getName()).log(Level.SEVERE,null,e);
         }
     }
 }
复制代码
  • 注册数据库驱动类,指定数据库地址,其中包括 DB 的用户名、密码及其他连接信息;
  • 调用 DriverManager.getConnection() 方法创建 Connection 连接到数据库;
  • 调用 Connection 的 createStatement() 或 prepareStatement() 方法,创建 Statement 对象,此时会指定 SQL(或是 SQL 语句模板 + SQL 参数);
  • 通过 Statement 对象执行 SQL 语句,得到 ResultSet 对象,也就是查询结果集;
  • 遍历 ResultSet,从结果集中读取数据,并将每一行数据库记录转换成一个 JavaBean 对象;
  • 关闭 ResultSet 结果集、Statement 对象及数据库 Connection,从而释放这些对象占用的底层资源。

ORM

ORM(Object Relational Mapping,对象-关系映射)框架来封装 1~6 步的重复性代码,实现对象模型、关系模型之间的转换。

微信公众号:CoderLi

微信公众号:CoderLi

常见的 ORM 框架 Mybatis、Hibernate

JPA

JPA 是在 JDK 5.0 后提出的 Java 持久化规范(JSR 338)。JPA 规范本身是为了整合市面上已有的 ORM 框架,结束 Hibernate、EclipseLink、JDO 等 ORM 框架各自为战的割裂局面,简化 Java 持久层开发。

微信公众号:CoderLi

Spring Data JPA

Spring Data JPA 是符合 JPA 规范的一个 Repository 层的实现

微信公众号:CoderLi

虽然市面上的绝大多数 ORM 框架都实现了 JPA 规范,但是它们在 JPA 基础上也有各自的发展和修改,这样导致我们在使用 JPA 的时候,依旧无法无缝切换底层的 ORM 框架实现。而使用 Spring Data JPA 时,由于Spring Data JPA 帮助我们抹平了各个 ORM 框架的差异,从而可以让我们的上层业务无缝地切换 ORM 实现框架。

\

猜你喜欢

转载自juejin.im/post/7075187681399373855