问题描述:在MyBatis中使用动态Sql后,对数据库进行条件查询,返回结果条数为0;但在mysql中直接执行语句能查询出来结果
<mapper namespace="com.wow.dao.IUserDao">
<!-- 配置 查询结果的列名和实体类的属性名的对应关系 -->
<resultMap id="userMap" type="com.wow.domain.User">
<!-- 主键字段的对应 -->
<id property="userId" column="id"></id>
<!--非主键字段的对应-->
<result property="userName" column="username"></result>
<result property="userAddress" column="address"></result>
<result property="userSex" column="sex"></result>
<result property="userBirthday" column="birthday"></result>
</resultMap>
<!-- 根据条件查询用户 -->
<select id="findUserByCondition" resultMap="userMap" parameterType="com.wow.domain.User">
select * from user
<where>
<if test="userName != null">
username=#{userName}
</if>
</where>
</select>
</mapper>
解决思路:首先检查了sql语句是否有语法问题;然后检查了resultmap配置的列名与参数是否对应正确,是否有拼写错误或者映射问题,可以给根据mybatislog中的sql语句来判断;然后检查是否能执行别的语句;
解决方案:最终经过排查,发现产生的原因为中英文编码问题导致的,在jdbcConfig.properties文件中的连接url内加入的编码格式出现问题,需加入如下语句,
?useUnicode=true&characterEncoding=UTF-8
注意这里的&在xml文件中使用时,需要改为&;但在properties文件中不需要,可以直接使用,这是我的问题。
ps:麻了,莫名其妙的bug,费心费时,但愿天下不在有bug!不然我迟早气出病