我需要通过 HashMap 中的参数传递数据库名称。
我的Mybatis XML是
<select id="getById" parameterType="hashmap" resultMap="result">
SELECT * FROM #{db}.CONTACT WHERE CONTACT_NAME = #{name}
</select>
我的 Java 调用是
hm.put("db","abc");
hm.put("name","def");
Contact c=contactDAO.selectById(hm);
但是我得到以下错误
The error occurred while setting parameters
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your
SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax
to use near ''abc'.CONTACT WHERE CONTACT_NAME = 'def'' at line 1
mybatis 可以不传数据库名吗?
最佳答案
您需要使用 ${}
而不是 #{}
进行直接 String
替换。
<select id="getById" parameterType="hashmap" resultMap="result">
SELECT * FROM ${db}.CONTACT WHERE CONTACT_NAME = #{name}
</select>
关于java - 在mybatis中传递数据库名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27689042/