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>
相关推荐
小马爱打代码5 小时前
MyBatis:反射模块详解
mybatis
Klong.k9 小时前
Mybatis-plus中Save()方法的注意点
mybatis
小北方城市网10 小时前
SpringBoot 集成 RabbitMQ 实战(消息队列):实现异步通信与系统解耦
java·spring boot·后端·spring·rabbitmq·mybatis·java-rabbitmq
BD_Marathon10 小时前
动态SQL(四) choose、when、otherwise
mybatis
阿杰 AJie11 小时前
MyBatis-Plus 比较运算符
java·数据库·mybatis
皙然12 小时前
MyBatis 执行流程源码级深度解析:从 Mapper 接口到 SQL 执行的全链路逻辑
数据库·sql·mybatis
这儿有个昵称12 小时前
Java大厂面试实录:从Spring MVC到微服务的技术深度探讨
java·redis·spring·mybatis·spring security·spring mvc·oauth2
一直都在57212 小时前
Spring3整合MyBatis实现分页查询和搜索
mybatis
色空大师1 天前
mybatis动态sql
sql·mybatis·foreach·where·sql动态语法
BD_Marathon1 天前
自定义映射resultMap——通过collection解决一对多的映射关系(九)
mybatis