背景:mybatis查询的时候,发现当where条件中有中文的时候,mapper接口返回的结果集为空,但是将sql语句拿到客户端工具中执行时,可以查询到结果集
问题现象描述:
1.对mapper接口进行测试时,发现返回结果集大小为0:
@Test
public void getRecordsByFuzzySearch() throws Exception {
List<RecordModel> recLst = trecordMapper.getRecordsByFuzzySearch("测试");
Assert.assertTrue(recLst.size() > 0);
}
console日志显示查询返回结果集大小为0
将sql语句拿到客户端工具(Sequel Pro或者navicat)上执行,可以查询到结果:
问题解决过程:
1.查询mysql的字符设置:发现charater_set_server为latin1
2.修改my.cnf配置,在[mysqld]节点下增加配置character-set-server=utf8:
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
# port = .....
# server_id = .....
socket = /var/lib/mysql/mysql.sock
character-set-server=utf8
3.重启mysql:
4.修改生效后的mysql字符设置:
5.再次执行方法,可以看到,返回结果集大小为1