zoukankan      html  css  js  c++  java
  • mybatis入门(一):jdbc的缺点

    mybatis的基础内容
      1.mybatis的框架原理
      2.mybatis开发dao两种方法:
        a.原始dao开发方法(程序需要编写dao接口和dao实现类)
        b.mybatis的mapper接口(相当于dao接口)代理开发方法
      3.mybatis配置文件:SqlMapConfig.xml
      4.mybatis核心:
        mybatis输入映射
        mybatis输出映射
      5.mybatis的动态sql
      mybatis的高级知识
        1.高级结果集映射(一对一,一对多,多对多)
        2.mybatis延迟加载
      3.mybatis查询缓存(一级缓存,二级缓存)
      4.mybatis和spring进行整合
      5.mybatis的逆向工程

    mybatis基础入门:

      1.jdbc程序
        数据库驱动:
        mysql-connect-java-5.1.7-bin.jar:(mysql)
        ojdbcl4-10.2.0.1.0.jar

      jdbc程序代码问题:
        1.数据库连接,使用时创建,不使用立即释放,对数据库进行频繁连接开启和关闭,造成数据库资源浪费,
        影响了数据库的性能。
        解决方案:使用数据库的连接池管理数据库的连接。

        2.将sql语句硬编码到java代码中,如果sql语句的修改,需要重新编译java代码,不利于系统的维护
        解决方案:将sql语句配置到xml配置文件中,即使sql变化,不需要对java进行重新编译

        3.向preparedStatement中设置参数,对占位符位置和参数值,硬编码在代码中,不利于系统的维护。
        解决方案:将sql语句和参数值配置到xml中

        4.从resultSet中遍历结果集数据时,存在硬编码,将获取表的字段进行硬编码,不利于系统的维护。
        解决方案:将查询的结果自动的映射的java的对象

    public class JdbcTest {
        public static void main(String[] args) {
            //数据库连接
            Connection connection = null;
            //预编译的Statement,使用预编译的statement的可以提高性能,使用statement操作数据库
            PreparedStatement preparedStatement = null;
            //结果集
            ResultSet resultSet = null;
    
            try {
                //加载数据库驱动
                Class.forName("com.mysql.jdbc.Driver");
                //通过数据驱动管理类获取数据库链接
                connection =DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatisdata?characterEncoding=utf-8","root","root");
                //定义sql语句,其中?标识占位符
                String sql = "select * from user where id = ?";
                //获取预处理statement
                preparedStatement = connection.prepareStatement(sql);
                //设置参数,第一个参数为sql中参数的序号,第二个参数为设置的参数值
                preparedStatement.setInt(1,1);
                //向数据库发出sql查询,查询出数据集
                resultSet = preparedStatement.executeQuery();
                //遍历查询结果
                while(resultSet.next()){
                    System.out.println(resultSet.getString("id")+"->"+resultSet.getString("username")+"->"+resultSet.getString("sex"));
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                //释放资源
                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();
                    }
                }
            }
        }
    }
    

      

  • 相关阅读:
    eclipse-source not found
    eclipse-[org.apache.hadoop.util.Shell]
    Oracle—字段多行合并(LISTAGG)
    Selenium IDE 命令使用——断言
    Selenium IDE录制脚本——Chrome浏览器使用介绍
    Selenium家族谱(三生三世)
    python自动化测试之多线程生成BeautifulReport测试报告
    Python接口自动化之ExtentHTMLTestRunner测试报告
    [Java] Tomcat
    [刷题] 1002 写出这个数 (20分)
  • 原文地址:https://www.cnblogs.com/wnbahmbb/p/7337281.html
Copyright © 2011-2022 走看看