[英]Creating a table programmatically using MyBatis and MySql
I want to create a method to dynamically create tables just passing the table name as a variable. 我想创建一种方法来动态创建表,只需将表名作为变量传递即可。 I have defined my xml mapper 我已经定义了我的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>
And my Java Interface Mapper is simply: 我的Java接口映射器很简单:
public interface TableCreatorMapper {
public void createNewTableIfNotExists(String tableName);
}
but when I call my interface 但是当我调用界面时
tableCreatorMapper.createNewTableIfNotExists("test");
I get the following exception: 我得到以下异常:
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
If I instead change the query adding the ``for the table name: 如果我改为更改查询,则为表名添加``:
CREATE TABLE IF NOT EXISTS `#{tableName}`(
`ID` varchar(20) NOT NULL,
PRIMARY KEY (`ID`)
)
ENGINE=InnoDB
I get 我懂了
### 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).
Any idea why? 知道为什么吗?
try 尝试
CREATE TABLE IF NOT EXISTS ${_parameter}
(
`ID` varchar(20) NOT NULL,
PRIMARY KEY (`ID`)
)
ENGINE=InnoDB
#{name} is for parameters in PreparedStatement (see String Substitution in Parameters ). #{name}用于PreparedStatement中的参数 (请参阅Parameters中的 字符串替换 )。
in DAO, use annotation @Param
void createTableIfNotExist(@Param("uuid") String uuid);
在DAO中,使用注释@Param
void createTableIfNotExist(@Param("uuid") String uuid);
in MAPPER, use $
在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>
could be used in MAPPER too. <bind>
也可以在MAPPER中使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.