您的位置:首页 > 数据库

ssm项目使用Mybatis动态拼接sql语句,生成的sql中文全部显示为???的问题(配置文件sql语句中文解析问题)

2017-10-28 11:31 1291 查看

场景再现

有一个表,现在要查询表中一个字符型字段是否含有一些关键字。

例:表中有个字段 tag;现在要判断的是这个 tag 是否包含关键字,关键字的个数是不确定的,所以要用动态sql语句进行拼接,筛选出至少包含一个关键字的 tag

需求理解

tag 字段的值为 ‘abcdef admin root’

关键词集: {‘abc’, ‘admin’, ‘mysql’, ‘fuzz’}

结果: ‘abcdef admin root’满足筛选条件

分析tag 字段的值含有给定的关键词集中的 ‘abc’, ‘admin’

代码实现

相关的表结构如下:

create table `conferenceinfo` (
`id` bigint(20) not null auto_increment,
`cnname` varchar(128) default null,
`enname` varchar(128) default null,
`tag` varchar(250) default null,
`location` varchar(64) default null,
`sponsor` varchar(128) default null,
`startdate` date default null,
`enddate` date default null,
`deadline` date default null,
`acceptance` date default null,
`website` varchar(128) default null,
`isclassify` int(11) default '0',
primary key (`id`)
) engine=innodb auto_increment=1238 default charset=utf8


项目中使用mybatis自动扫描配置装载dao接口,配置文件中的 sql 语句如下:

<!-- 使用动态 sql 语句进行拼接, locate()函数查找 tag 是否包含关键字-->
<select id="queryLatestConcernedConferenceInfo" resultType="ConferenceInfo">
select id, cnName, enName, location, sponsor, startdate, enddate,
website, deadline, acceptance, tag
from ConferenceInfo
<where>
<![CDATA[ startdate >= #{time} and startdate <= #{endtime}]]>
<if test="#{tags}.size > 0">
and (
<!--
注意:采用的map传递sql参数,service层使用map.put("tags",list)把list放入map,
foreach标签中的collection的值要与map中放入list的key值一样
-->
<foreach collection="tags" item="t" separator=" ">
<![CDATA[ locate(#{t},tag) > 0 or ]]>
</foreach>
0 )
</if>
</where>

order by startdate
<if test=" #{offset}!=null and #{number} != null">
limit #{offset}, #{number}
</if>
</select>




直接在命令行下运行sql语句可以查找到数据,但项目中怎么都查不到任何数据,查看mysql执行日志后发现原来执行的sql语句如下:

select id, cnName, enName, location, sponsor, startdate, enddate,
website, deadline, acceptance, tag

from ConferenceInfo
WHERE startdate >= '2017-10-28 09:50:04.88'
and startdate <= '2017-10-30 09:50:04.88'
and ( locate(' Computing & Technology',tag) > 0
or locate('?????',tag) > 0
or 0
)
order by startdate
limit 0, 8


明明没有传入
?????
参数,而且第一个参数
Computing & Technology
就是查询参数,而第二个参数原本是一个中文字符串,现在变成了
?????
,大概知道是因为出现了中文乱码,但是项目中添加了字符编码过滤器,web层应该不会出现中文乱码,那么乱码可能就出现在数据库层了,搜了一下发现这个问题果然出现在数据库层 参考链接

查看下数据库连接配置,内容如下:

jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/pythondb
jdbc.user = root
jdbc.password = ********


问题就出现在
jdbc.url
上,需要为url指定字符集和编码后mybatis数据库操作就不会出现中文乱码问题,需要在url后面加上
?useUnicode=true&characterEncoding=utf-8


修改后的properties配置文件如下:

jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost
cd69
:3306/pythondb?useUnicode=true&characterEncoding=utf-8
jdbc.user = root
jdbc.password = ********




执行查询语句后再查看mysql执行日志,内容如下:

select id, cnName, enName, location, sponsor, startdate, enddate,
website, deadline, acceptance, tag
from ConferenceInfo
WHERE startdate >= '2017-10-28 10:02:55.099'
and startdate <= '2017-10-30 10:02:55.102'
and (   locate('计算机应用技术',tag) > 0 or
locate('计算机软件',tag) > 0 or
locate('计算机通信',tag) > 0 or
0 )
order by startdate
limit 0, 8
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐