使用Spring+SpringMVC+MyBatis查询用户名是否存在时,返回时不要返回 *
1、因为如果这样写,当用户可用时会报一个null的错误
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 <!-- 根据name查询用户存在与否 --> 2 <select id="findName" resultType="int" parameterType="String" > 3 select * from user where name=#{name} 4 </select>
$正确写法,能解决这个问题
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 <select id="findName" resultType="int" parameterType="String" > 2 select IFNULL(sum(id),0) from user where name=#{name} 3 </select>
注意:IFNULL(sum(id),0)
作用:当未查询到数据时(即用户名可用),会返回0,而不会报上面那个错。