string substitution

by default, using the #{} syntax will cause mybatis to generate preparedstatement properties and set the values safely against the preparedstatement parameters (e.g. ?). while this is safer, faster and almost always preferred, sometimes you just want to directly inject a string unmodified into the sql statement. for example, for order by,you might use something like this:

order by ${columnname}

here mybatis won't modify or escape the string.

note it's not safe to accept input from a user and supply it to a statement unmodified in this way. this leads to potential sql injection attacks and therefore you should either disallow user input in these fields, or always perform your own escapes and checks.


1. 使用#{}格式的语法在mybatis中使用preparement语句来安全的设置值,执行sql类似下面的:

preparedstatement ps =conn.preparestatement(sql);



2. 不过有时你只是想直接在 sql 语句中插入一个不改变的字符串。比如,像 order by,你可以这样来使用:

order by ${columnname}

此时mybatis 不会修改或转义字符串。


statement st =conn.createstatement();

resultset rs= st.executequery(sql);

这种方式的缺点是: 以这种方式接受从用户输出的内容并提供给语句中不变的字符串是不安全的,会导致潜在的 sql 注入攻击,因此要么不允许用户输入这些字段,要么自行转义并检验。




  • mybatis中#{}和${}的区别
  • mybatis与ibatis的区别
  • 《深入理解mybatis原理》mybatis缓存机制的
  • hibernate和mybatis思想,区别,优缺点
  • mybatis和ibatis区别
  • mybatis decimal 救命啊!mybatis整合问题
  • hibernate跟mybatis
  • mybatis与ibatis比较
  • mybatis缓存和hibernate缓存的理解问题和区
  • hibernate与mybatis 的比较

     收录时间:2016-11-12 15:29 来源:post  作者:匿名
    上一篇:mybatis与ibatis的区别  (电脑版  手机版)
    Copyright by;All rights reserved. 联系我们