【问题标题】:Spring boot and h2. Can't change db name弹簧靴和 h2。无法更改数据库名称
【发布时间】:2017-09-20 15:19:41
【问题描述】:

我有这样的配置文件:

spring.datasource:
    driverClassName: org.h2.Driver
    url: jdbc:h2:mem:mydb;MODE=MySQL

server:
  port: 9001

我的主

@SpringBootApplication
public class SpringJmsApplication implements CommandLineRunner {

   public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringJmsApplication.class, args);

    }

    @Resource
    DataSource dataSource;
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public void run(String... strings) throws Exception {

        log.info("Creating tables" + jdbcTemplate.toString());
        log.info("<<<<<<<<<<<<<<<<<<<<<<<<" + dataSource.getConnection().getCatalog()+">>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

        jdbcTemplate.execute("DROP TABLE customers IF EXISTS");
        jdbcTemplate.execute("CREATE TABLE xxx(" +
                "id SERIAL, first_name VARCHAR(255), last_name VARCHAR(255))");
}
}

在我的控制台http://localhost:9001/console/ 中,我使用 url 参数 jdbc:h2:mem:mydb 进入我的数据库并且找不到我的表,但是使用参数 jdbc:h2:mem:testdb 我看到了我的 xxx 表。如何解决这个问题?

dataSource.getConnection().getCatalog()

总是返回“TESTDB”

【问题讨论】:

标签: java spring spring-boot jdbc h2


【解决方案1】:

您必须在 build.gradle 文件中提供 spring-boot-starter-jdbc 依赖项。

替换:

compile group: 'org.springframework', name: 'spring-jdbc', version: '4.3.11.RELEASE

与:

compile group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc'

大概是spring-boot-starter-jdbc处理这个属性处理。

【讨论】:

  • dependencies { compile("org.springframework.boot:spring-boot-starter-web") 编译组:'org.springframework',名称:'spring-jms',版本:'4.3。 11.RELEASE' 编译组:'org.apache.activemq',名称:'activemq-core',版本:'5.7.0' 编译组:'org.apache.activemq',名称:'activemq-broker',版本:'5.15.0' testCompile 组:'org.apache.activemq.junit',名称:'activemq-junit',版本:'5.15.0' testCompile('org.springframework.boot:spring-boot-starter-test ') testCompile('com.jayway.jsonpath:json-path') }
  • @ttt,你一定错过了什么?这里没有h2依赖
  • 对不起,这是另一个项目
  • compile("org.springframework.boot:spring-boot-starter-web") compile("org.springframework.boot:spring-boot-devtools") 编译组:'org.springframework' ,名称:'spring-jdbc',版本:'4.3.11.RELEASE' 编译组:'com.h2database',名称:'h2',版本:'1.4.196' 编译组:'org.springframework.batch' ,名称:'spring-batch-core',版本:'3.0.8.RELEASE' 编译组:'org.springframework.boot',名称:'spring-boot-starter-web',版本:'1.5.7。 RELEASE' 编译组:'org.springframework.boot',名称:'spring-boot-starter-thymeleaf',版本:'1.5.7.RELEASE'
  • @ttt,请参阅我更新的问题。另外,编辑您的问题并添加build.gradle 文件,以便其他人可以找到它
【解决方案2】:

将此添加到您的 applicaton.properties

spring.datasource.url=jdbc:h2:mem:testdb

【讨论】:

    猜你喜欢
    • 2019-05-03
    • 2016-12-26
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    • 1970-01-01
    • 1970-01-01
    • 2018-02-14
    相关资源
    最近更新 更多