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对象(输出映射

      

      

  • 相关阅读:
    简介.Net对象序列化.txt
    如何在Web页面退出前提示用户保存数据?
    如何将图片存储到数据库中
    页面回车键响应,onkeydown事件
    用C#创建Windows服务(Windows Services)
    解决“Visual Studio 要求设计器使用文件中的第一个类。移动类代码使之成为文件中的第一个类,然后尝试重新加载设计器。”方法
    动态创建htm元素并添加到document中
    如何在Asp.net的Header中添加/title/Meta tages/CSS
    无法打开项目文件,“d:\web\webapp.csproj”,此安装不支持该项目类型
    用Intelligencia.UrlRewriter实现URL重写
  • 原文地址:https://www.cnblogs.com/joan-HTY/p/9012693.html
Copyright © 2011-2022 走看看