【问题标题】:Creating a table programmatically using MyBatis and MySql使用 MyBatis 和 MySql 以编程方式创建表
【发布时间】:2015-08-28 12:17:29
【问题描述】:

我想创建一种方法来动态创建表,只需将表名作为变量传递。 我已经定义了我的 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 接口映射器很简单:

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

但是当我调用我的界面时

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).

知道为什么吗?

【问题讨论】:

    标签: java mysql database mybatis spring-mybatis


    【解决方案1】:

    试试

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

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

    【讨论】:

    • 嗨,谢谢你的建议,但后来我得到: org.mybatis.spring.MyBatisSystemException: 嵌套异常是 org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ' org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:75) 的“class java.lang.String”中的 tableName'
    • 请尝试更新答案;如果它不起作用,简单的binding 应该可以工作。
    【解决方案2】:

    在DAO中,使用注解@Param void createTableIfNotExist(@Param("uuid") String uuid);

    在 MAPPER 中,使用 $

    <update id="createTableIfNotExist" parameterType="java.lang.String">
      CREATE TABLE IF NOT EXISTS `table_${uuid}` 
      (
            `id` bigint(18) NOT NULL,
            `info` varchar(18) NOT NULL,
            PRIMARY KEY (`id`)
      )
      ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='this table is generated by java code.'
    </update>
    

    &lt;bind&gt; 也可以在 MAPPER 中使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-05
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-19
      • 1970-01-01
      相关资源
      最近更新 更多