带参sql$和#的区别(注解)

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

例如:#{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("")

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