Sqlserver 模糊查询中文及在mybatis xml【非中文不匹配查询】N@P2问题

问题

sqlserver模糊查询或相等,两者都无法查询。

百度方案解释

Like 后的N是表示unicode字符。获取SQL Server数据库中Unicode类型的数据时,字符串常量必须以大写字母 N 开头,否则字符串将转换为数据库的默认代码页(字符集编码),这可能导致字符串内容发生变化,无法识别。

在SQL界面中

sql 复制代码
select * from table where column like N'%文档%'

但是在mybatis这样就不好写了。

注意点

一定不要使用mybatis的where标签,否则mybatis会识别为N@P2
N后面必须用 '' + #{queryColumn} 这种格式

模糊查询

sql 复制代码
select * from table where 
<if test="queryColumn != null and queryColumn != ''">
	column like N'%'+#{queryColumn}+'%'
</if>

相等查询

结合上面,我们发现N后面不能直接拼接#{queryColumn}参数,否则就会报错!

sql 复制代码
select * from table where 
<if test="queryColumn != null and queryColumn != ''">
	column = N''+#{queryColumn}
</if>
相关推荐
天若有情6735 小时前
SpringBoot4 + MyBatis 前后端分离实战|从零实现坏习惯管理系统,支持局域网手机访问CRUD
智能手机·mybatis
不能只会打代码7 小时前
Day 011 — Spring 全家桶深度拆解
spring boot·mybatis·spring aop·spring mvc·spring ioc
憧憬成为web高手20 小时前
皮卡丘靶场速通--sql 2
数据库·sql·mybatis
IT_Octopus1 天前
一次由 RestTemplate 引发的 Connection reset:被 XML 截胡的请求体
xml·java·spring boot
Dontla1 天前
网站爬虫控制策略介绍(robots.txt、sitemap.xml、x-robots-tag、noindex、nofollow)网站索引
xml·爬虫·dubbo
测试修炼手册2 天前
[测试技术] JUnit 6 入门与实战:断言、参数化与 Mockito
数据库·junit·sqlserver
Java面试题总结2 天前
mybatis插件
java·tomcat·mybatis
风起洛阳@不良使2 天前
spring中xml和注解开发的对比
xml·java·spring
心之语歌2 天前
SQL Server 按月分区实战:动态边界自动生成方案
sqlserver
Devin~Y3 天前
互联网大厂Java面试实战:Spring Boot、MyBatis、Redis、Kafka、JWT、Spring Cloud 与 AI 场景追问
java·spring boot·redis·spring cloud·kafka·mybatis·spring security