JdbcTemplateにMySQLのストアドプロシージャを作成する方法

リチャード・チンクル:

バックグラウンド

特定のステートメントは、唯一の私は、実行を作成しようとしているストアドプロシージャ内で許可されていることをMySQLでの問題を回避するには、その後、JdbcTemplateから提出されたSQL内のストアドプロシージャをドロップします。簡体例は、(これは春ブーツ内であることを起こる)のようになります。

@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の中にこれを実行しているが、JdbcTemplate Iから提出されました

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()

私はそれらのために、それはthatsの理解ではDELIMITER文がJdbcTemplateによって許可されていないが、他の構文エラーへリンクしているリードで提案されているようちょうどそれらを取り除きます

質問

MySQLのストアドプロシージャは、JdbcTemplateで作成された(またはステートメントは、通常は実行され、ストアドプロシージャを許可)することができますどのように

ノート

deliminator文のないエラーがあります

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()
user06062019:

ドライバーがあなたがJDBCを使用してその場でストアドプロシージャを作成したいaccount.Ifに区切られたクエリを取っていないようです。次のプロパティを使用してURLに接続パラメータとして渡します。

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

上記のプロパティができるようになります「;」クエリを区切り。あなたはここで、この上でより多くを見つけることができJPAのHibernateを使用してMySQLのストアドプロシージャを作成します。

この場合、更新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;

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=236755&siteId=1