簡體   English   中英

使用MyBatis和MySql以編程方式創建表

[英]Creating a table programmatically using MyBatis and MySql

我想創建一種方法來動態創建表,只需將表名作為變量傳遞即可。 我已經定義了我的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).

知道為什么嗎?

嘗試

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

#{name}用於PreparedStatement中的參數 (請參閱Parameters中的 字符串替換 )。

在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>

<bind>也可以在MAPPER中使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM