zoukankan      html  css  js  c++  java
  • #和$的区别

    MyBatis启用了预编译功能

    #{}:在预编译过程中,会把#{}部分用一个占位符?代替,执行时,将入参替换编译好的sql中的占位符“?”,能够很大程度防止sql注入

    ${}:在预编译过程中,${}会直接参与sql编译,直接显示数据,无法防止Sql注入,一般用于传入表名或order by动态参数

    MyBatis是如何做到SQL预编译的呢?

    其实在框架底层,是JDBC中的PreparedStatement类在起作用

    Statement代码

    public void insertStatement(){
        Connection conn=null;
        Statement statement=null;        
        try {
            conn=JDBCTools.getConnection2();
            statement=conn.createStatement();
            statement.executeUpdate("insert into t_student(name,age,email) values('zs',18,'zs@173.com')");//执行sql语句
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            statement.close();
            conn.close();
        }        
    }

    PreparedStatement代码

    public void insertPreparedStatement(){
        Connection conn=null;
        PreparedStatement preStatement=null;//创建PreparedStatement对象
        try {
            conn=JDBCTools.getConnection2();
            String sql="insert into t_student(name,age,email) values(?,?,?)";
            preStatement=conn.prepareStatement(sql);
            preStatement.setString(1, "klkl");
            preStatement.setInt(2, 12);
            preStatement.setString(3, "kkk@jjj");
            preStatement.executeUpdate();//执行sql
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            statement.close();
            conn.close();
        }
    }
  • 相关阅读:
    delphi 鼠标拖动
    Tesseract-ocr 工具使用记录
    在dcef3当中执行js代码并获得返回值
    idhttp提交post带参数并带上cookie
    新制作加热块
    java 调用oracle 分页存储过程 返回游标数据集
    JDBC链接
    ------------浪潮之巅读后感---------------
    价值观作业
    --------关于C语言的问卷调查-----------
  • 原文地址:https://www.cnblogs.com/yifanSJ/p/9095926.html
Copyright © 2011-2022 走看看