java - 使用 MyBatis 和 MySql 以编程方式创建表

标签 java mysql database mybatis spring-mybatis

我想创建一种方法来动态创建表,只需将表名作为变量传递即可。 我已经定义了我的 xml 映射器

<mapper namespace="com.mappers.TableCreatorMapper">
    <cache />
    <insert id="createNewTableIfNotExists" parameterType="String" > 
        CREATE TABLE IF NOT EXISTS #{tableName} 
        (
        `ID` varchar(20) NOT NULL,
        PRIMARY KEY (`ID`)
        ) 
        ENGINE=InnoDB
    </insert>
</mapper>

我的 Java Interface Mapper 很简单:

public interface TableCreatorMapper {
     public void createNewTableIfNotExists(String tableName);
}

但是当我调用我的接口(interface)时

tableCreatorMapper.createNewTableIfNotExists("test");

我得到以下异常:

org.springframework.jdbc.BadSqlGrammarException: 
### Error updating database.  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 ''test' 
        (
        `ID` varchar(20) NOT NULL,
        PRIMARY KEY (`ID`)
' at line 1
### The error may involve com.mappers.TableCreatorMapper.createNewTableIfNotExists-Inline
### The error occurred while setting parameters
### SQL: CREATE TABLE IF NOT EXISTS ?          (         `ID` varchar(20) NOT NULL,         PRIMARY KEY (`ID`)         )    ENGINE=InnoDB
### 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 ''test' 
        (
        `ID` varchar(20) NOT NULL,
        PRIMARY KEY (`ID`)
' at line 1
; bad SQL grammar []; nested exception is 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 ''test' 
        (
        `ID` varchar(20) NOT NULL,
        PRIMARY KEY (`ID`)
' at line 1
    at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:231)
    at org.sp

如果我改为更改查询,为表名添加``:

 CREATE TABLE IF NOT EXISTS `#{tableName}`(
        `ID` varchar(20) NOT NULL,
        PRIMARY KEY (`ID`)
        ) 
        ENGINE=InnoDB

我明白了

### The error occurred while setting parameters
### SQL: CREATE TABLE IF NOT EXISTS `?`(         `ID` varchar(20) NOT NULL,         PRIMARY KEY (`ID`)         )    ENGINE=InnoDB
### Cause: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
; SQL []; Parameter index out of range (1 > number of parameters, which is 0).; nested exception is java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).

知道为什么吗?

最佳答案

尝试

CREATE TABLE IF NOT EXISTS ${_parameter} 
        (
        `ID` varchar(20) NOT NULL,
        PRIMARY KEY (`ID`)
        ) 
        ENGINE=InnoDB

#{name} 用于 PreparedStatement 中的参数(参见 Parameters 中的字符串替换)。

关于java - 使用 MyBatis 和 MySql 以编程方式创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32270897/

相关文章:

java - 如何删除工具栏上方的空间?

java - 将 Android 应用程序嵌入到其他 Android 应用程序中(Overlay + Events)

database - 检查 Postgresql 是否正在监听

java - 在 Java 中,如何从 Resteasy 验证返回有意义的 JSON 格式错误?

java - 如何将单个或多个附件附加到 CouchbaseLite 文档 - Android?

php 计数函数未在 mysql_fetch_assoc 上显示正确的计数

mysql - 将 mysql 查询转换为 sql server 2000 的 mssql 查询

Mysql触发器创建错误

database - 数据库查询优化器和并行性中的左深度连接树

java - Android数据库md5改变,为什么改变相同的大小?