(一)Mybatis之JDBC介绍

前言:为什么在学mybatis之前要先了解JDBC呢?因为mybatis是以ORM模型为中心思想的框架,而所有的ORM模型都是基于JDBC进行封装的,不同的ORM模型对JDBC封装的强度是不一样的。


JDBC

  相信有一部分读者是通过JDBC连接数据库的,但是JDBC之定义了接口规范,具体实现是交由给各个数据库厂商实现的,因为每个数据库都有其特殊性,这些是Java规范没有办法确定的,所以JDBC是一种典型的桥接模式(桥接模式,简单地说就是,将抽象部分与它的实现部分分离开来,将继承关系转化成关联关系,降低类与类之间的耦合度,减少系统中的类的代码,使他们都可以独立变化,具体可以看https://www.cnblogs.com/chenssy/p/3317866.html )。

  JDBC基本连接类:

 

public class JdbcExample{

    private Connection getConnection(){

        Connection connection = null;  //初始化Connection

        try{

            Class.forName("com.mysql.Driver");  //注册驱动

            String url = "jdbc:mysql://localhost:3306/mybatis";

            String user="root";

            String password="";

            connection = DriverManager.getConnection(url,user,password);  //注册数据库信息

        }catch(ClassNotFoundException|SQLException ex){

            Logger.getConnection(JdbcExample.class.getName()).log(Level.SEVERE,null,ex);

            return null;

        }

        return connection;

    }

  JDBC实现类:

public User getUser(int id){

        Connection connection = getConnection();  //获取连接信息

        PreparedStatement ps = null;  //操作数据库

        ResultSet rs = null;  //接收返回的结果

        try{

            ps = connection.prepareStatement("select id, userName from user_info where id =?");  

            ps.setInt(1,id);

            rs = ps.executeQuery();

            while(rs.next()){

                int roleId = rs.getInt("id");

                String userName = rs.getString("userName");

                User user = new User(id,userName);

                return user;

            }

        }catch(SQLException ex){

            Logger.getConnection(JdbcExample.class.getName()).log(Level.SEVERE,null,ex);

        }finally{

            this.close(rs,ps,connection);

        }

        return null;

    }
  //关闭数据库的连接
private void close(ResultSet rs, Statement stmt, Connection connection){ try{ if(rs!=null&&!rs.isClosed()){ rs.close(); } }catch(SQLException ex){ Logger.getConnection(JdbcExample.class.getName()).log(Level.SEVERE,null,ex); } try{ if(stmt!=null&&!stmt.isClosed()){ stmt.close(); } }catch(SQLException ex){ Logger.getConnection(JdbcExample.class.getName()).log(Level.SEVERE,null,ex); } try{ if(connection!=null&&!connection.isClosed()){ connection.close(); } }catch(SQLException ex){ Logger.getConnection(JdbcExample.class.getName()).log(Level.SEVERE,null,ex); } }   
  //测试
public static void main(String[] args){ JdbcExample example = new JdbcExample(); User user = example.getUser(1); System.err.println("userName=>+"+user.getName()); } }

         从代码中我们可以看出,整个过程大致分为几部分:

1、 使用JDBC编程连接数据库,注册驱动和数据库信息。

2、 操作Connection以打开Statement对象。

3、 通过Statementzhixing SQL,将结果返回到ResultSet对象。

4、 使用ResultSet读取数据库信息,然后通过代码转换为具体的POJO对象。

5、 关闭数据库相关资源。

         JDBC虽然比ODBC(用C语言实现可关联数据库工具)的平台独立性要好,但是它还是存在一些弊端。其一,工作量相对较大。我们需要先连接,然后处理JDBC底层事务,处理数据类型。我们还需要操作Connection对象、Statement对象和ResultSet对象去拿到数据。其二,我们要对JDBC编程可能产生的异常进行捕捉处理并正确关闭资源。

猜你喜欢

转载自www.cnblogs.com/NYfor2018/p/9088254.html
今日推荐