项目场景:
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession. ### The error may exist in com/bjsxt/mapper/UserMapper.xml问题描述:
在使用mybatis框架,通过id查询用户时,出现异常;
</select>
<!-- 根据用户id查询用户 参数类型可以不写-->
<select id="selectUsersById" parameterType="int" resultType="com.bjsxt.pojo.Users">
/* #{}相当于jdbc中的问号,直接在{}中给定绑定参数的名称;如参数只有一个叫什么都可以,但要是有多个就需要具体参数名称了;*/
select * from users where userid = #{suibian}
</select>
原因分析:
这样的异常:是映射文件出现了错误,大多数都是resultType的配置出现了错误,但是今天我这个错误优点奇葩;
解决方案:
将代码中的注释删掉就解决了;
异常代码
</select>
<!-- 根据用户id查询用户 参数类型可以不写-->
<select id="selectUsersById" parameterType="int" resultType="com.bjsxt.pojo.Users">
/* #{}相当于jdbc中的问号,直接在{}中给定绑定参数的名称;如参数只有一个叫什么都可以,但要是有多个就需要具体参数名称了;*/
select * from users where userid = #{suibian}
</select>
消除异常后代码:
</select>
<!-- 根据用户id查询用户 参数类型可以不写-->
<select id="selectUsersById" parameterType="int" resultType="com.bjsxt.pojo.Users">
select * from users where userid = #{suibian}
</select>
解决后我也是无语了;
在这里再加一句,不论是/* */注释,还是选中Ctrl+/注释都是会报相同的异常的;
在这里建议就不要加注释了,如果真的要加还是放到select标签外面吧!