Mybatis 模糊查询 中文问题

IDE编码:GBK ,换成UTF-8也不行!


@Test
public void testSearchStudents() {
logger.info("查询学生(带条件)");
Map<String,Object> map=new HashMap<String,Object>();
//map.put("gradeId", 2);
map.put("name", "王"); // 中文

//map.put("name", "a");  英文

//map.put("age", 17);
List<Student> studentList=studentMapper.searchStudents(map);
for(Student student:studentList){
logger.info(student);
}
}


<select id="searchStudents" parameterType="Map" resultMap="StudentResult">
select * from t_student 
<where>
<if test="gradeId!=null">
gradeId=#{gradeId}
</if>
<if test="name!=null">
<!-- and name like '%${name}%' -->
and name like CONCAT('%','${name}','%' )
</if>
<if test="age!=nulll">
and age=#{age}
</if>
</where>
</select>


假设模糊查询name=英文,可以打印学生信息;但是模糊查询name=中文,查询不到信息。

我的解决方案是:原来是数据库编码的问题,简单的方法是:在.properties配置文件中的jdbc.url值后面跟上?useUnicode=true&characterEncoding=utf8

注意:前提环境:Mybatis没有和SpringMvc整合,整合后不需要这样设置,可以有其他更便捷的方法。(纯粹个人理解,希望可以得到更多人的指教)

原文地址:https://www.cnblogs.com/LEARN4J/p/5332357.html