码迷,mamicode.com
首页 > 数据库 > 详细

带参sql$和#的区别

时间:2016-01-21 10:37:28      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

主要区别就是#带双引号,$不带

例如:#{id}代表‘id‘,${id}代表id

 

下面是Mybatis @Select注解方式的sql

@Select("select id,name from user where id=#{id}")
public User getUser(@Param("id")long id);

@Select("select id,name from user where id=${id}")
public User getUSer(@Param("id")long id);

如果id传入为1,则实际sql为

select id,name from user where id=‘1‘

select id,name from user where id=1

Mybaits方法有一种情况

@Select("select id,name from user where id=#{id}")
public User getUser(@Param("id") long id);

@Select("select id,name from user where id=#{id}")
public User getUser(long id);

以上两种都可以,因为传一个参数是可以省略@Param("")的,但是这种情况下不能使用${},

传两个参数以上时,必须要写@Param("")

 

带参sql$和#的区别

标签:

原文地址:http://www.cnblogs.com/angto64/p/5147273.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!