在查询用户的联系人时,我想使用特定于该用户的临时表,例如tmp_contact_{userId},如果userId为123,应该解析为tmp_contact_123
我继承了一个使用iBatis与MySql数据库互操作的java项目。 我已经阅读了一些 iBatis 文档,并且在 xml 中看到可替换参数为#{userId} 或#userId#。前者似乎被解释为准备好的语句的参数并产生问号,例如'create temporary table tmp_contact_?',而后者保留为文字,例如'创建临时表 tmp_contact_#userId#'。 是否有替代语法来生成“创建临时表 tmp_contact_123”?
最佳答案
您需要在 .xml 文件中进行以下更改:
<?xml version="1.0" encoding="utf-8" ?>
<sqlMap namespace="xxx" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<statements>
<select id="GetTempTableData" resultClass="Student" parameterClass="System.String">
CREATE TABLE ##TEMPTABLE (ID int, Name varchar(20))
INSERT INTO ##TEMPTABLE (ID, NAME) VALUES (1, 'RAVI')
INSERT INTO ##TEMPTABLE (ID, NAME) VALUES (2, 'KAPIL')
SELECT * FROM ##TEMPTABLE
</select>
</statements>
</sqlMap>
注意:您需要在选择语句中创建临时表。您需要使用双“##”在 IBATIS 中创建临时表。 “##”将替换为单个“#”,它将创建名为“#TEMPTABLE”的临时表。请记住它们是您需要搜索的临时表的一些限制。
希望对你有帮助
关于java - 我可以使用 iBatis 创建动态临时表吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27846995/