简体   繁体   English

如何在 JdbcTemplate 中创建 mySQL 存储过程

[英]How to create a mySQL stored procedure in a JdbcTemplate

Background背景

To work around the issue in MySql that certain statements are only permitted within a stored procedure I'm trying to create, run, then drop a stored procedure within sql submitted by a JdbcTemplate.为了解决 MySql 中的问题,即某些语句只允许在存储过程中使用,我正在尝试创建、运行,然后在 JdbcTemplate 提交的 sql 中删除存储过程。 A simplied example would be (this happens to be within spring boot):一个简单的例子是(这恰好在 spring boot 中):

@Service
public class StartupDatabaseCheck {
    private JdbcTemplate template;

    @Autowired
    public StartupDatabaseCheck(JdbcTemplate template){
        this.template = template;
    }

    @PostConstruct
    public void init() {
        log.info("Running custom fields table creation (if required)");
        try {
            String migrateSql = Resources.toString(Resources.getResource("migrateScript.sql"), Charsets.UTF_8);
            template.execute(migrateSql);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}

Where migrateScript.sql is migrateScript.sql 在哪里

DELIMITER //
CREATE PROCEDURE migrate()
BEGIN
    IF ((SELECT count(1)
         FROM INFORMATION_SCHEMA.COLUMNS
         WHERE table_name = 'custom_field_instance_data'
           and column_name='entity_id' and is_nullable = false) > 0)
    THEN
        alter table custom_field_instance_data MODIFY COLUMN entity_id char(32) null;
    END IF;
END //
DELIMITER ;

call migrate;

drop procedure migrate;

Running this within mySql workbench works fine, but submitted by the JdbcTemplate I get the error在 mySql 工作台中运行它工作正常,但由 JdbcTemplate 提交我收到错误

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 'CREATE PROCEDURE migrate_custom_fields()

As I understand it thats because those DELIMITER statements are not permitted by JdbcTemplate but just removing them as suggested in that link leads to other syntax errors据我了解,这是因为JdbcTemplate 不允许这些DELIMITER语句而只是按照该链接中的建议删除它们会导致其他语法错误

Question

How can a mySQL stored procedure be created (or statements usually only allowed with a stored procedure be executed) by a JdbcTemplate如何通过 JdbcTemplate 创建 mySQL 存储过程(或通常只允许执行存储过程的语句)

Notes笔记

The error without the deliminator statements is没有分隔符语句的错误是

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 'CREATE PROCEDURE migrate_custom_fields()

It seems that the driver is not taking the delimited queries into the account.If you want to create an stored procedure on fly using the jdbc.驱动程序似乎没有将分隔查询记入帐户。如果您想使用 jdbc 动态创建存储过程。 Using the following property and pass it as the connection parameter in the URL.使用以下属性并将其作为 URL 中的连接参数传递。

jdbc:mysql://localhost:3306/test?allowMultiQueries=true

The above property will allow ';'上述属性将允许';' delimited queries.定界查询。 You can find more on this at here Create MySQL stored procedure using JPA Hibernate您可以在此处找到更多关于使用 JPA Hibernate 创建 MySQL 存储过程的信息

The updated migrateScript.sql in this case would be在这种情况下更新的 migrateScript.sql 将是

drop procedure IF EXISTS migrate_custom_fields;

CREATE PROCEDURE migrate_custom_fields()
BEGIN
    IF ((SELECT count(1)
         FROM INFORMATION_SCHEMA.COLUMNS
         WHERE table_name = 'custom_field_instance_data'
           and column_name='entity_id' and is_nullable = false) > 0)
    THEN
        alter table custom_field_instance_data MODIFY COLUMN entity_id char(32) null;
    END IF;
END ;

call migrate_custom_fields;

drop procedure migrate_custom_fields;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM