zoukankan      html  css  js  c++  java
  • 8、SpringBoot+Mybatis整合------参数取值方式

    前言:

    我们知道,在mybatis中,参数取值方式有两种:

    #{ } 和 ${ }

    下面,我们来探讨下#{ }与${ }不同。


    一、#{ }

    例:

    select * from student where name=#{name}

    编译后执行的sql语句:

    select * from student where name=?

    说明:

                #{ }实现的是JDBC 中preparedStatement中的占位符。

    #{ }适合sql语句中的参数传值,构建sql语句#{ }是不可以的。

    例如:

    select * from #{tablename} ;

    编译后的sql语句为:

    select * from ?

    这在sql中是不允许的,所以要用${ 拼接}

    #{ }试用的场景

    1.where语句里的判断:

              a=#{a},a>#{a},a in {#{a}},a like #{a}........

              2.set语句:

                           set a=#{a}

             3.插入语句中:

                          values(#{a},.......)

             4.其他大部分适合${ }进行拼接

    二、${ }

    例:

    select * from student where name=${name}

    编译后执行的sql语句:

    select * from student where name=name

    说明:

    ${ }取参方式是简单的字符串拼接,不适合进行参数传值,不然会有sql语句注入的危险。

    它更加适合的是构建sql语句。


                                                                                                      2018-07-03

  • 相关阅读:
    按钮字体颜色的设置
    异常
    数据存储
    SQLiteOpenHelper
    MVC
    在单线程模型中 Message、Handler、Message Queue、Looper 之间的关系
    ListView 的优化方案
    fragment生命周期及优点
    ANR
    Android系统架构
  • 原文地址:https://www.cnblogs.com/TimerHotel/p/springboot_matatis_08.html
Copyright © 2011-2022 走看看