【问题标题】:Spring boot not able to run data.sql to initialize DBSpring Boot 无法运行 data.sql 来初始化 DB
【发布时间】:2020-04-07 06:21:53
【问题描述】:

build.gradle

plugins {
    id 'org.springframework.boot' version '2.2.2.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
}

group = 'com.spdemo'
version = '1.0'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    runtimeOnly 'com.h2database:h2'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude module: 'junit'
    }
    testImplementation 'org.junit.jupiter:junit-jupiter:5.4.0'
}

test {
    useJUnitPlatform()
}

schema.sql

drop table IF EXISTS officers;
create TABLE officers (
  id         INT         NOT NULL AUTO_INCREMENT,
  rank       VARCHAR(20) NOT NULL,
  first_name VARCHAR(50) NOT NULL,
  last_name  VARCHAR(20) NOT NULL,
  PRIMARY KEY (id)
);

数据.sql

INSERT INTO officers(rank, first_name, last_name) VALUES('CAPTAIN', 'James', 'Kirk');
INSERT INTO officers(rank, first_name, last_name) VALUES('CAPTAIN', 'Jean-Luc', 'Picard');
INSERT INTO officers(rank, first_name, last_name) VALUES('CAPTAIN', 'Benjamin', 'Sisko');
INSERT INTO officers(rank, first_name, last_name) VALUES('CAPTAIN', 'Kathryn', 'Janeway');
INSERT INTO officers(rank, first_name, last_name) VALUES('CAPTAIN', 'Jonathan', 'Archer');

JdbcOfficerDAOTest.java

@SpringBootTest
@ExtendWith(SpringExtension.class)
// @Transactional // tx for each test rolls back by default
public class JdbcOfficerDAOTest {
    // @Qualifier("jdbcOfficerDAO")
    private Logger logger = LoggerFactory.getLogger(JdbcOfficerDAOTest.class);

    @Autowired
    private OfficerDAO dao;

    @Test
    public void save() {
        Officer officer = new Officer(Rank.LIEUTENANT, "Nyota", "Uhuru");
        officer = dao.save(officer);
        assertNotNull(officer.getId());
        // assertTrue(!(officer == null));
        logger.info("id is: " + officer.getId());
        logger.info("I'm here");
    }

    @Test
    public void findByIdThatExists() {
        Optional<Officer> officer = dao.findById(1);
        logger.info("here is the count: " + dao.count());
        assertTrue(officer.isPresent());
        // assertEquals(1, officer.get().getId().intValue());
    }
}

当我运行上面的测试用例时,第一个 save() 测试总是通过,这意味着表已经创建,但第二个总是失败,意味着 data.sql 没有被执行。 dao.count() 总是返回 0。 我怎样才能解决这个问题?

【问题讨论】:

    标签: java spring spring-boot spring-data-jpa h2


    【解决方案1】:
    spring.jpa.hibernate.ddl-auto=none
    

    我在 application.properties 中添加了这一行,它解决了这个问题。 默认设置阻止 spring boot 运行 data.sql

     DDL mode. This is actually a shortcut for the "hibernate.hbm2ddl.auto" property.
     Defaults to "create-drop" when using an embedded database and no 
     schema manager was detected. Otherwise, defaults to "none". 
    
    none         Disable DDL handling.
    validate     Validate the schema, make no changes to the database.
    update       Update the schema if necessary.
    create       Create the schema and destroy previous data.
    create-drop  Create and then destroy the schema at the end of the session.
    

    【讨论】:

      【解决方案2】:

      您可以通过此属性指定要运行/执行的 SQL 文件:

      spring.datasource.data=classpath:sql_script1.sql,classpath:sql_script2.sql
      

      【讨论】:

      • 我试过了。运行测试时数据仍未加载。
      • 日志有错误吗?您可以尝试将所有语句放在一个文件中吗?
      猜你喜欢
      • 2019-11-29
      • 2020-07-08
      • 2021-09-29
      • 1970-01-01
      • 2018-12-31
      • 2017-12-18
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      相关资源
      最近更新 更多