spring3.0 aop 获取 ibatis 执行的语句_Mybatis 源码分析:执行器

一、Jdbc的执行过程

7b301966cfa28b18b226ad50ac5bddd4.png

一个简单的入门Demo:

public static final String URL = "jdbc:mysql://127.0.0.1:3306/mybatis?serverTimezone=GMT";public static final String USERNAME = "root";public static final String PASSWORD = "root";@Testpublic void streamTest() throws SQLException, ClassNotFoundException {
        Class.forName("com.mysql.cj.jdbc.Driver");    Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);    String sql = "select id, order_no, user_name from order_info where id = 1";    Statement statement = connection.createStatement();    ResultSet rs = statement.executeQuery(sql);    while(rs.next()){
            System.out.println("user_name:" + rs.getString(3));    }    rs.close();    statement.close();    connection.close();}

二、Jdbc的三种执行器

我们平时最常使用的是PreparedStatment 执行器,因为他能够防止sql注入。在Statement每次执行都是一条条的静态Sql,而PreparedStatement 是一条sql和若干组参数。这样在多次执行的时候,数据的体量较小,所以理论上来性能会更高。当然Statement也有他的优势,就是在一个Statement中,可以执行不同的静态sql。

7ad7cdc9616f0537ca91d8ebb4c674eb.png

@Testpublic void preparedStatementTest() throws SQLException {
        Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);    String sql = "select * from order_info where id = ?";    PreparedStatement ps = connection.prepareStatement(sql);    ps.setInt(1, 1);    ps.execute();     printResultSet(ps);    ps.setInt(1, 2);    ps.execute();    printResultSet(ps);    ps.close();}private void printResultSet(PreparedStatement ps) throws SQLException {
        ResultSet rs = ps.getResultSet();    while (rs.next()){
            System.out.println(rs.getString(3));    }    rs.close();}

三、Mybatis 的执行过程

e0ea4edcfc1dbef2be41bbce07ba934f.png

1、SQL会话SqlSession

org.apache.ibatis.session.SqlSession 接口提供了基本Api,包含了select、s

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值