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

  • 相关阅读:
    hdu 4710 Balls Rearrangement()
    hdu 4707 Pet(DFS水过)
    hdu 4706 Children's Day(模拟)
    hdu 4712 Hamming Distance(随机函数暴力)
    csu 1305 Substring (后缀数组)
    csu 1306 Manor(优先队列)
    csu 1312 榜单(模拟题)
    csu 1303 Decimal (数论题)
    网络爬虫
    Python处理微信利器——itchat
  • 原文地址:https://www.cnblogs.com/TimerHotel/p/springboot_matatis_08.html
Copyright © 2011-2022 走看看