zoukankan      html  css  js  c++  java
  • mybatis(二):缘由

    本是Apache的一个开源项目iBatis

    2010年,iBatis由Apache Software Foundation(软件基金会)迁移到了Google Code(代码托管平台),并改名为MyBatis

    实质上Mybatis是对ibatis进行一些改进

    原生态JDBC代码

    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    
    try {
        //1、加载数据库驱动
        Class.forName("com.mysql.jdbc.Driver");
        
        //2、通过驱动管理类获取数据库链接
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8","root", "root");
        
        //3、定义sql语句 ?表示占位符
        String sql = "select * from user where username = ?";
        
        //4、获取预处理statement
        preparedStatement = connection.prepareStatement(sql);
        
        //5、设置参数,第一个参数为sql语句中参数的序号(从1开始),第二个参数为设置的参数值
        preparedStatement.setString(1, "admin");
        
        //6、向数据库发出sql执行查询,查询出结果集
        resultSet = preparedStatement.executeQuery();
        
        //7、遍历查询结果集
        while (resultSet.next()) {
            System.out.println(resultSet.getString("id"));
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        //8、释放资源
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    

    问题

    数据库连接频繁开启和关闭

    存在硬编码

  • 相关阅读:
    Gym 101466(完整)
    HDU 3639 Hawk-and-Chicken (强连通缩点+DFS)
    hdu3394--Railway(点的双连通分量)
    hdu2732 Leapin' Lizards 最大流+拆点
    hdu2609 How many 字典树+最小表示法
    hdu2594 Simpsons’ Hidden Talents LCS--扩展KMP
    hdu2509 Be the Winner 博弈
    hdu2461 Rectangles 线段树--扫描线
    hdu2389 Rain on your Parade 二分图匹配--HK算法
    hdu2328 Corporate Identity 扩展KMP
  • 原文地址:https://www.cnblogs.com/angelica-duhurica/p/11111688.html
Copyright © 2011-2022 走看看