zoukankan      html  css  js  c++  java
  • mybatis 中 #{} 和 ${} 的区别

    mybatis 中 #{} 和 ${} 的区别:

    1、相同:都可以用来获取传入的参数值

    2、不同:    #{}:是以预编译的形式,将参数设置到sql语句中;jdbc中的PreparedStatement,可以防止sql注入,sql语句以?号的形式

           ${}:取出的值直接拼装在sql语句中;会有安全问题;

    例如:

    <select id="getEmpByIdAndName" resultType="com.mybatis.bean.Emp" databaseId="mysql">
            select * from emp where id = #{id} and  name = #{name}
        </select>

    SQL : Preparing: select * from emp where id = ? and name = ? 

    如果用${}:

    <select id="getEmpByIdAndName" resultType="com.mybatis.bean.Emp" databaseId="mysql">
            select * from emp where id = ${id} and  name = #{name}
        </select>

    SQL :  Preparing: select * from emp where id = 101 and name = ?

    大多数的时候,我们都用#{},而${}的用在jdbc不支持占位符的地方:

    例如:

    根据传入的分表的下标来查询数据,emp分为三个表,传入index来查询不同的表

    select * from emp_${index}

    用来排序order by ${字段}

    select * from emp order by  ${field}
  • 相关阅读:
    cf D. Vessels
    cf C. Hamburgers
    zoj 3758 Singles' Day
    zoj 3777 Problem Arrangement
    zoj 3778 Talented Chef
    hdu 5087 Revenge of LIS II
    zoj 3785 What day is that day?
    zoj 3787 Access System
    判断给定图是否存在合法拓扑排序
    树-堆结构练习——合并果子之哈夫曼树
  • 原文地址:https://www.cnblogs.com/tdyang/p/12720602.html
Copyright © 2011-2022 走看看