mybatis 中文做参数报错

Posted Flink菜鸟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis 中文做参数报错相关的知识,希望对你有一定的参考价值。

  一个简单的查询,如果参数中有中文。如下:

<select id="xxxx" resultType="hashmap">
    select * from talbe_a a where  a.kpeople = ${name}
</select>

报错:

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.sql.SQLSyntaxErrorException: ORA-00904: "萧亚轩": 标识符无效
...

Caused by: java.sql.SQLSyntaxErrorException: ORA-00904: "萧亚轩": 标识符无效
....

mybatis sql日志:

18:06:36,192 DEBUG JdbcTransaction:47 - Openning JDBC Connection
18:06:36,830 DEBUG PooledDataSource:47 - Created connection 1459361227.
18:06:36,843 DEBUG queryrealactormediatop:47 - ooo Using Connection [[email protected]]
18:06:36,843 DEBUG queryrealactormediatop:47 - ==>  Preparing: select * from table_a a where v1.kpeople = 萧亚轩 
18:06:36,961 DEBUG queryrealactormediatop:47 - ==> Parameters: 

注意:“萧亚轩”两边没有单引号,不是“?”的占位符,参数列表里面是没有值。

修改其实很简单:

v1:换$ 为#

<select id="xxxx" resultType="hashmap">
    select * from talbe_a a where  a.kpeople = #{name}
</select>

v2:添加 ‘’ 单引号

<select id="xxxx" resultType="hashmap">
    select * from talbe_a a where  a.kpeople = ‘${name}‘
</select>

 

#只会当成个字符串

$就不一定是字符串,或者其他类型

 





以上是关于mybatis 中文做参数报错的主要内容,如果未能解决你的问题,请参考以下文章

MyBatis怎么防止SQL注入

spring整合mybatis时运行测试代码报错,是怎么回事?

mybatis仅传入一个String类型参数报错

Failed to convert property value of type ‘java.lang.String‘ to required type ‘int‘ for property(代码片段

Mybatis单个参数报错: There is no getter for property named

Mybatis单个参数报错: There is no getter for property named