mybatis 中的#{} 和 ${}

mybatis 中的#{} ${}

#{} 在mybatis中,底层是用PreparedStatement方法,调用pojo类的set() 来赋值的。MyBatis在处理#{ }时,它会将sql中的#{ }替换为?
优点:防止SQL注入。
缺点:执行效率低。
${} 在mybatis中。底层是用字符串拼接实现的。
优点:执行效率高
缺点:不能防止SQL注入。

注意:${} 比 #{} 快很多,当数据量很高时,SQL查询使用 #{} 会导致数据库cpu飙高、查询超时。因此要慎重使用 #{}

扩展:spring el表达式的#{} ${}

${} 一般是类@PropertySource(value=“classpath:user.properties”) 加上成员变量@Value(“name”),配合user.properties配置文件使用

这个很常用,就不举例了。

#{} 一般是一个pojo类注入另一个pojo类的参数,这里要注意被注入的类的注解

@Component
@Data
public class Pet {
    @Value("猫")
    private String species;
    @Value("22")
    private Integer age;
}
@Component
@Data
@NoArgsConstructor
@AllArgsConstructor
//@ConfigurationProperties(prefix = "user")
//@PropertySource(value="classpath:user.properties")
public class User {

    @Value("#{pet.age}")
    private Integer id;
    @Value("#{pet.species}")
    private String name;
    private Integer age;
    private String email;
    private String username;
    private String password;
    private Date birthday;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值