我如何使用MyBatis创建alter table语句? MySQLSyntaxErrorException:您的SQL语法有错误

问题描述 投票:0回答:2

我有以下内容

@Mapper
public interface StatMapper {
    @Update("alter table stats reorganize partition #{pFirst},#{pSecond} into ( "
            + "partition #{pSecond} values less than (#{dSecond}) "
            + ");")
    public void updateFirstPartition(@Param("pFirst")String pFirst, @Param("pSecond")String pSecond, @Param("dSecond")LocalDate dSecond);

它出现以下错误

2019-09-30 21:58:23.067调试13728 --- [restartedMain] c.s.s.m.StatMapper.updateFirstPartition:==>准备:alter table stats重新组织分区?,? into(分区?值小于(?));2019-09-30 21:58:23.093调试13728 --- [restartedMain] cssmStatMapper.updateFirstPartition:==>参数:p20180901(String),p20181001(String),p20181001(String),2018-10-01(日期)引起原因:org.springframework.jdbc.BadSqlGrammarException:###更新数据库时出错。原因:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法有错误。检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在'p20180901','p20181001'附近使用(分区'p20181001'的值小于('2018-10-'在第1行)

如何使用MyBatis发布此alter table语句?

该语句应该看起来像这样(p0p1p20180901p20181001替代:]

alter table stats reorganize partition p0,p1 into (
    partition p1 values less than ('2018-10-01')
);

我具有以下@Mapper公共接口StatMapper {@Update(“ alter table stats将分区#{pFirst},#{pSecond}重新组织为(” +“分区#{pSecond}的值小于(## ... >

java spring mybatis spring-mybatis
2个回答
1
投票

${}是文本替换,#{}java.sql.PreparedStatement中的占位符(有关详细信息,请参见FAQ)。因此,通过您的代码,MyBatis生成了如下的准备好的语句...

PreparedStatement ps = connection.prepareStatement(
  "alter table stats reorganize partition ?,? into (partition ? values less than (?))");

0
投票

我用${pFirst},${pSecond}而不是#{pFirst},#{pSecond}解决了。

© www.soinside.com 2019 - 2024. All rights reserved.