MyBatis学习笔记-注解SQL多个参数查询异常处理

MyBatis 注解SQL查询,单个参数无需处理可以正常返回结果

@Select("SELECT * FROM USER WHERE ACCOUNT = #{account}")
User readUserByAccount(String account) throws DataAccessException;

多个参数查询不做处理会报异常

@Select("SELECT * FROM USER WHERE ACCOUNT = #{account} AND PASSWORD = #{password}")
User readUserByAccountAndPassword(String account, String password) throws DataAccessException;

Caused by: org.apache.ibatis.binding.BindingException: Parameter 'account' not found. Available parameters are [1, 0, param1, param2]

解决方法一:参数前面添加Param注解

@Select("SELECT * FROM USER WHERE ACCOUNT = #{account} AND PASSWORD = #{password}")
User readUserByAccountAndPassword(@Param("account") String account, @Param("password") String password) throws DataAccessException;

解决方法二:使用#{0}、#{1}
@Select("SELECT * FROM USER WHERE ACCOUNT = #{0} AND PASSWORD = #{1}")
User readUserByAccountAndPassword(String account, String password) throws DataAccessException;
 
解决方法三:使用Map,key与{}里面参数名称保持一致
@Select("SELECT * FROM USER WHERE ACCOUNT = #{account} AND PASSWORD = #{password}")

User readUserByAccountAndPassword(Map params) throws DataAccessException;





你可能感兴趣的:(J2EE,Batis,MyBatis,SQL,多参数,注解)