properties的MySQL连接时已经指定了字符编码格式:
url: jdbc:mysql://localhost:3306/sky_take_out?useUnicode=true&characterEncoding=utf-8
使用MyBatis查询,带有中文参数,查询出的内容为空。
执行的语句为:
<select id="selectByCondition" resultType="Employee">
select *
from employee
where status = #{status} and name like #{name} and phone like #{phone}
</select>
在控制台打印出SQL的执行过程,发现传递的参数变成了乱码:
==> Preparing: select * from employee where status = ? and name like ? and phone like ?
==> Parameters: 1(Integer), %??%(String), %1%(String)
进一步排查问题发现,在MyBatis中直接执行不传参的语句,可以正确执行,并能在控制台打印:
<select id="selectByCondition1" resultType="Employee">
select *
from employee
where status =1 and name like '%程%' and phone like '%1%'
</select>
==> Preparing: select * from employee where status =1 and name like '%程%' and phone like '%1%'
出现这个问题是因为idea中文件的编码格式不是utf-8
解决
将设置中的编码格式都改成utf-8
可以正确执行参数的传递和查询结果
==> Preparing: select * from employee where status = ? and name like ? and phone like ?
==> Parameters: 1(Integer), %程%(String), %1%(String)