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>
相关推荐
MaCa .BaKa8 小时前
44-校园二手交易系统(小程序)
java·spring boot·mysql·小程序·maven·intellij-idea·mybatis
java1234_小锋9 小时前
Java高频面试题:MyBatis如何实现动态数据源切换?
java·开发语言·mybatis
小旭952712 小时前
SpringBoot + 七牛云 + Quartz:图片存储与定时清理
java·spring boot·后端·mybatis
APguantou13 小时前
NCRE-三级数据库技术-第9章-安全管理
数据库·安全·sqlserver
夕除16 小时前
MVN--06
数据库·sql·mybatis
2501_9307077817 小时前
使用C#代码将 HTML 转换为 PDF、XPS 和 XML
xml·pdf
计算机学姐2 天前
基于SpringBoot的高校餐饮档口管理系统
java·vue.js·spring boot·后端·spring·intellij-idea·mybatis
身如柳絮随风扬2 天前
MyBatis-Plus与PageHelper分页方案对比
mybatis
罗山仔2 天前
【Vertx构建异步响应式reactive mybatis,mybatis-vertx-adaptor】
mybatis·orm·异步·reactive·响应式·webflux·vertx
java1234_小锋2 天前
Java高频面试题:如何编写一个MyBatis插件?
java·开发语言·mybatis