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>
相关推荐
IT_Octopus8 小时前
java 实体属性 Map 解决 mybatis-plus wrapper selectone 查mysql json类型为null 问题
java·mysql·mybatis
Dolphin_Home9 小时前
MyBatis 核心属性详解笔记(由浅入深)
笔记·mybatis
susu10830189119 小时前
maven-3.9.12的conf配置settings.xml
xml·java·maven
一直都在5729 小时前
MyBatis入门:CRUD、参数处理与防 SQL 注入
java·sql·mybatis
while(1){yan}18 小时前
MyBatis Generator
数据库·spring boot·java-ee·mybatis
memgLIFE1 天前
mybatis数据库查询
数据库·oracle·mybatis
drebander1 天前
MyBatis-Plus saveBatch 在异步线程中事务未提交问题排查与修复
数据库·mybatis
super_lzb1 天前
mybatis拦截器ResultSetHandler详解
java·spring·mybatis·springboot
七夜zippoe1 天前
ORM框架下的SQL优化 N+1问题识别与解决方案
自动化·mybatis·jpa·n+1·batch fetching
Yu_iChan1 天前
苍穹外卖Day09 地址簿模块
java·数据库·mybatis