【SMM-Mybatis】${}与#{}

**${}相当于 Statement

#{}相当于PreparedStatement**

@Select(“select * from user where id = #{id}”)
User findById(@Param(“id”) long id);

@Select(“select * from user where name = #{name}”)
User findByName(@Param(“name”) String name);

@Select(“select * from user where email = #{email}”)
User findByEmail(@Param(“email”) String email);

// and more “findByXxx” method

可以只写这样一个方法:

@Select(“select * from user where ${column} = #{value}”) User findByColumn(@Param(“column”) String column, @Param(“value”) String value);

其中 ${column} 会被直接替换,而 #{value} 会被使用 ? 预处理。 因此你就可以像下面这样来达到上述功能:

User userOfId1 = userMapper.findByColumn(“id”, 1L);
User userOfNameKid = userMapper.findByColumn(“name”, “kid”);
User userOfEmail = userMapper.findByColumn(“email”, "[email protected]");

你可能感兴趣的:(SSM-Mybatisd,Mybatis)