在Spring Boot中创建数据库表后运行自定义脚本 [英] Run custom script after database tables are created in Spring Boot

查看:202
本文介绍了在Spring Boot中创建数据库表后运行自定义脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中使用带有gradle的Spring Boot, ─主$ b $│├──java
│└──资源
│└──import.sql
├──脚本
│└──custom_script.sql
└──test
├──java
│├──持久化
││└──UserRepositoryTest.java
│└──TestConfiguration.java
└──资源
└──import.sql

位于 custom_script.sql 我有一些我的应用程序需要的sql脚本,例如在插入之前触发等等。可以配置应用程序,例如使用应用程序属性文件,使用hibernate或其他方式来加载该脚本,并在创建表之后创建我的触发器?



表我已经定义为JPA实体。因此,我预期的步骤顺序是:
$ b $ ol <

  • 从JPA实体创建表

  • 从脚本/ custom_script.sql

  • resources / import.sql加载插入脚本

  • 运行application /测试(这取决于是否决定运行主应用程序或测试)

    为了测试目的,我试着创建 TestConfiguration .java 接口在这里我使用 @Sql 类似的附加内容:

      @Sql(src / scripts / custom_script.sql)
    public interface TestConfiguration {
    }

    接下来我想用这个接口进行测试,需要这样的脚本:

      @Transactional 
    @SpringBootTest
    @RunWith(SpringRunner.class)
    public class UserRepositoryTest实现TestConfiguration {

    }

    问题在于:


    1. 我的解决方案没有工作

    2. 它仅包含测试用例,而不是应用程序正常启动时的情况

    您能帮我解决吗?谢谢。

    >

    https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/context/jdbc/Sql.html



    然而,您可以尝试以下操作。
    通过将脚本文件夹放在src / test / resources下将脚本添加到classpath中。然后将 @SQL 设置为

    c $ c>在您的UserRepositoryTest类上添加Annotation,并将路径更改为:

    @SQL(classpath:scripts / custom_script.sql) $ b

    如果你想在其他测试中使用它,请创建一个抽象测试类和所需的注释。



    如果您需要真正的数据库迁移,请查看Flyway: https://flywaydb.org/

    p>

    I am using Spring Boot with gradle in my project with following project structure:

    src/
    ├── main
    │   ├── java
    │   └── resources
    │       └── import.sql
    ├── scripts
    │   └── custom_script.sql
    └── test
        ├── java
        │   ├── persistent
        │   │   └── UserRepositoryTest.java
        │   └── TestConfiguration.java
        └── resources
            └── import.sql
    

    in custom_script.sql I have some sql scripts which my application requires, e.g. triggers before inserts and so on. Is possible to configure application, for example with application properties file, with hibernate, or some another way to load that script and create my triggers after tables will be created?

    Tables I have defined as JPA Entities. So my expected steps order is:

    1. Create tables from JPA Entities
    2. Load triggers from scripts/custom_script.sql
    3. Load insert scripts from resources/import.sql
    4. Run application / tests (it depends if I decide to run main app or tests)

    For test purpose I tried create TestConfiguration.java interface where I use @Sql anotation like that:

    @Sql("src/scripts/custom_script.sql")
    public interface TestConfiguration {
    }
    

    and next I wanted to use this interface for tests which requires this script like that:

    @Transactional
    @SpringBootTest
    @RunWith(SpringRunner.class)
    public class UserRepositoryTest implements TestConfiguration {
    
    }
    

    Problem is that :

    1. my solution didn't work
    2. it's cover only Test case, not case when application starts normally

    Can you help me with that please? Thanks.

    解决方案

    @SQL only works on Tests:

    https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/context/jdbc/Sql.html

    However you could try the following. Add the script to your classpath by putting your scripts folder under src/test/resources

    Then set the @SQLAnnotation on your UserRepositoryTest Class and change the path to:

    @SQL(classpath:scripts/custom_script.sql)

    If you want to use it on other Tests please create an abstract testclass with the needed annotations.

    If you need real Database migration have a look at Flyway: https://flywaydb.org/

    这篇关于在Spring Boot中创建数据库表后运行自定义脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆