当前位置: 首页 > news >正文

SpringBoot MyBatis连接数据库设置了encoding=utf-8还是不能用中文来查询

properties的MySQL连接时已经指定了字符编码格式:

url: jdbc:mysql://localhost:3306/sky_take_out?useUnicode=true&characterEncoding=utf-8

使用MyBatis查询,带有中文参数,查询出的内容为空。
执行的语句为:

    <select id="selectByCondition" resultType="Employee">select *from employeewhere 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 employeewhere 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)

http://www.mrgr.cn/news/43887.html

相关文章:

  • 感知机及其实践
  • uname命令:系统信息
  • Zig FFI与第三方C库的集成与使用
  • 【梯级水电站调度优化】基于线性递减策略优化粒子群算法
  • 思维导图工具,轻松搞定复杂问题!
  • dockerpull
  • 为什么说函数是Python中的一等公民
  • MySQL查询优化
  • 港股大跌敲响警钟
  • 用Python Turtle绘制天安门技术讲解
  • 一项研究表明,只需一滴干血,新的检测技术或许可以在几分钟内发现癌症
  • 脉冲神经网络(SNN)论文阅读(六)-----ECCV-2024 脉冲驱动的SNN目标检测框架:SpikeYOLO
  • Python - Windows下安装pip
  • 频繁full gc问题排查及解决
  • 用IMX6UL开发板编写按键输入实验
  • MSF捆绑文件
  • 超简单 Flux 换背景工作流:结合最新 ControlNet 深度 Depth 模型
  • 二分搜索算法
  • 跨境电商独立站轮询收款问题
  • VMware桥接模式无法连接网络