如何在 JdbcTemplate 中创建 mySQL 存储过程
How to create a mySQL stored procedure in a JdbcTemplate
背景
要解决 MySql 中的问题,即某些语句只允许在我尝试创建的存储过程中使用,运行,然后在 sql 中删除存储过程由 JdbcTemplate 提交。一个简单的例子是(这恰好在 spring 引导内):
@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);
}
}
}
其中 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;
运行 mySql workbench 中的这个工作正常,但是由 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()
据我了解,那是因为那些 DELIMITER
语句 are not permitted by JdbcTemplate 但只是按照 link 中的建议删除它们会导致其他语法错误
问题
如何通过 JdbcTemplate
创建 mySQL 存储过程(或通常只允许使用存储过程执行的语句)
备注
没有分隔符语句的错误是
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()
驱动程序似乎没有将分隔查询带入 account.If 您想要使用 jdbc 即时创建存储过程。
使用以下 属性 并将其作为 URL.
中的连接参数传递
jdbc:mysql://localhost:3306/test?allowMultiQueries=true
上面的属性将允许';'分隔查询。
您可以在此处找到更多相关信息
在这种情况下更新后的 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;
背景
要解决 MySql 中的问题,即某些语句只允许在我尝试创建的存储过程中使用,运行,然后在 sql 中删除存储过程由 JdbcTemplate 提交。一个简单的例子是(这恰好在 spring 引导内):
@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);
}
}
}
其中 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;
运行 mySql workbench 中的这个工作正常,但是由 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()
据我了解,那是因为那些 DELIMITER
语句 are not permitted by JdbcTemplate 但只是按照 link 中的建议删除它们会导致其他语法错误
问题
如何通过 JdbcTemplate
创建 mySQL 存储过程(或通常只允许使用存储过程执行的语句)备注
没有分隔符语句的错误是
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()
驱动程序似乎没有将分隔查询带入 account.If 您想要使用 jdbc 即时创建存储过程。 使用以下 属性 并将其作为 URL.
中的连接参数传递jdbc:mysql://localhost:3306/test?allowMultiQueries=true
上面的属性将允许';'分隔查询。
您可以在此处找到更多相关信息
在这种情况下更新后的 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;