首页 > 代码库 > mybatis 中文做参数报错

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 [oracle.jdbc.driver.T4CConnection@56fc15cb]
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 中文做参数报错