zoukankan      html  css  js  c++  java
  • mybatis学习笔记

    jdbc程序

    public static void main(String[] args) {
            Connection connection = null;//数据库连接
            PreparedStatement preparedStatement = null;//预编译的Statement
            ResultSet resultSet = null;//结果集
            try {
                // 加载驱动
                Class.forName("com.mysql.jdbc.Driver");
                // 创建连接
                connection = (Connection) DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/mybatis_day01?characterEncoding=utf-8", "root", "176166");
                // 定义sql语句
                String sql = "select * from user where username=?";
                preparedStatement = (PreparedStatement) connection.prepareStatement(sql);
                preparedStatement.setString(1, "joan");
                resultSet = preparedStatement.executeQuery();
                // 遍历结果集
                while (resultSet.next()) {
                    System.out.println(resultSet.getString("id") + "" + resultSet.getString("username"));
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if(resultSet!=null) {
                    try {
                        resultSet.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if(preparedStatement!=null) {
                    try {
                        preparedStatement.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if(connection!=null) {
                    try {
                        connection.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

            }

        }

    1 使用jdbc原生问题

      1 数据库频繁的链接开启和关闭,造成数据库资源浪费,影响数据库性能

      设想:使用数据库连接池管理数据库连接

      2 sql语句为硬编码到java代码中,不利于维护

      设想:将sql语句配置到xml文件中
      3 向preparedStatement中设置参数,设置占位符号位置,以及对应的参数值属于硬编码。
      设想:将sql语句及占位符和全部参数配置在xml中

      4 从resultSet中遍历结果集数据时存在硬编码
      设想:将查询结果集,自动映射成java对象

    2 mybatis 框架

     mybatis是一个持久层的框架。让程序将主要精力放在sql上,通过mybatis提供的映射方式,自由灵活生成(半自动化,大部分需要程序员编写sql)满足需要的sql语句。向prepaerdStatement中的输入参数自动进行输入映射,将结果集映射成java对象(输出映射

      

      

  • 相关阅读:
    利用Powershell和ceye.io实现Windows账户密码回传
    Local Response Normalization作用——对局部神经元的活动创建竞争机制,使得其中响应比较大的值变得相对更大,并抑制其他反馈较小的神经元,增强了模型的泛化能力
    python 读取二进制文件 转为16进制输出
    滴滴持续扩招私车 倒逼官方就范
    滴滴专车——司机提现流程
    滴滴专车司机升降级标准流程
    北京Uber优步司机奖励政策(9月21日~9月27日)
    滴滴快车奖励政策,高峰奖励,翻倍奖励,按成交率,指派单数分级(9月12日~9月18日)
    北京Uber优步司机奖励政策(9月14日~9月20日)
    天津市人民优步Uber司机奖励政策(9月14日~9月20日)
  • 原文地址:https://www.cnblogs.com/joan-HTY/p/9012693.html
Copyright © 2011-2022 走看看