Springboot应用程序创建mysql中已经存在的表

nwlqm0z1  于 2023-04-10  发布在  Spring
关注(0)|答案(3)|浏览(134)

我在mysql中有一个名为newdb的数据库,在该数据库中有2个表,Track和User。
当我运行我的springboot应用程序时,它创建了两个名为track和user的新表,应用程序使用这些表,而不是已经存在的两个表。
我希望它停止创建和使用表,并使用数据库中的2个预先存在的表。
我不知道是什么原因导致它创建这些表
下面是两个表的Model类
Track.java

  1. package net.codejava.song.model;
  2. import org.hibernate.annotations.NaturalId;
  3. import jakarta.persistence.Column;
  4. import jakarta.persistence.Entity;
  5. import jakarta.persistence.GeneratedValue;
  6. import jakarta.persistence.GenerationType;
  7. import jakarta.persistence.Id;
  8. import jakarta.persistence.Table;
  9. import java.sql.Timestamp;
  10. import java.time.LocalDateTime;
  11. @Entity
  12. @Table(name = "Track")
  13. public class Track {
  14. @Id
  15. @GeneratedValue(strategy = GenerationType.IDENTITY)
  16. private Integer id;
  17. @Column(name = "isrc", unique = true, nullable = false, length = 20)
  18. private String isrc;
  19. @Column(name = "name", nullable = false, length = 100)
  20. private String name;
  21. @Column(name = "duration_ms", nullable = false)
  22. private Long durationMs;
  23. @Column(name = "explicit", nullable = false)
  24. private Boolean explicit;
  25. @Column(name = "created_at", nullable = false, updatable = false, columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
  26. private LocalDateTime createdAt;
  27. public String getIsrc() {
  28. return isrc;
  29. }
  30. public void setIsrc(String isrc) {
  31. this.isrc = isrc;
  32. }
  33. public String getName() {
  34. return name;
  35. }
  36. public void setName(String name) {
  37. this.name = name;
  38. }
  39. public Long getDurationMs() {
  40. return durationMs;
  41. }
  42. public void setDurationMs(Long durationMs) {
  43. this.durationMs = durationMs;
  44. }
  45. public Boolean getExplicit() {
  46. return explicit;
  47. }
  48. public void setExplicit(Boolean explicit) {
  49. this.explicit = explicit;
  50. }
  51. // default constructor for JPA
  52. public Track() {
  53. this.createdAt = LocalDateTime.now();
  54. }
  55. // constructor with arguments
  56. public Track(String isrc, String name, Long durationMs, Boolean explicit) {
  57. this.isrc = isrc;
  58. this.name = name;
  59. this.durationMs = durationMs;
  60. this.explicit = explicit;
  61. }
  62. }

User.java

  1. @Entity
  2. @Table(name = "User")
  3. public class User {
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.IDENTITY)
  6. private Integer id;
  7. @Column(name = "username", unique = true, nullable = false, length = 50)
  8. private String username;
  9. @Column(name = "password", nullable = false, length = 255)
  10. private String password;
  11. // default constructor for JPA
  12. protected User() {}
  13. // constructor with arguments
  14. public User(String username, String password) {
  15. this.username = username;
  16. this.password = password;
  17. }
  18. public String getUsername() {
  19. return username;
  20. }
  21. public void setUsername(String username) {
  22. this.username = username;
  23. }
  24. public String getPassword() {
  25. return password;
  26. }
  27. public void setPassword(String password) {
  28. this.password = password;
  29. }
  30. // getters and setters
  31. }

这是我的application.properties文件
application.properties

  1. spring.datasource.url=jdbc:mysql://localhost:3306/newdb
  2. spring.datasource.username=root
  3. spring.datasource.password=password
  4. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  5. # JPA properties
  6. spring.jpa.show-sql=true
  7. spring.jpa.hibernate.ddl-auto=update

这是我的pom.xml
pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>3.0.5</version>
  9. <relativePath /> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>net.codejava.song</groupId>
  12. <artifactId>RestAPI-MetaData</artifactId>
  13. <version>1.0</version>
  14. <name>RestAPI-MetaData</name>
  15. <description>A REST-API for storing metadata of music tracks to a Relational Database, fetching the metadata from a mock endpoint.</description>
  16. <properties>
  17. <java.version>17</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.apache.httpcomponents.client5</groupId>
  22. <artifactId>httpclient5</artifactId>
  23. <version>5.1.3</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.mock-server</groupId>
  27. <artifactId>mockserver-netty</artifactId>
  28. <version>3.10.8</version>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.mock-server</groupId>
  32. <artifactId>mockserver-client-java</artifactId>
  33. <version>3.10.8</version>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-starter-data-jpa</artifactId>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.springframework.boot</groupId>
  41. <artifactId>spring-boot-starter-hateoas</artifactId>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.springframework.boot</groupId>
  45. <artifactId>spring-boot-starter-web</artifactId>
  46. </dependency>
  47. <dependency>
  48. <groupId>org.springframework.data</groupId>
  49. <artifactId>spring-data-jpa</artifactId>
  50. <version>3.0.4</version>
  51. </dependency>
  52. <dependency>
  53. <groupId>org.springframework.boot</groupId>
  54. <artifactId>spring-boot-devtools</artifactId>
  55. <scope>runtime</scope>
  56. <optional>true</optional>
  57. </dependency>
  58. <dependency>
  59. <groupId>com.h2database</groupId>
  60. <artifactId>h2</artifactId>
  61. <scope>runtime</scope>
  62. </dependency>
  63. <dependency>
  64. <groupId>junit</groupId>
  65. <artifactId>junit</artifactId>
  66. <version>4.13.2</version>
  67. <scope>test</scope>
  68. </dependency>
  69. <dependency>
  70. <groupId>org.springframework.boot</groupId>
  71. <artifactId>spring-boot-starter-test</artifactId>
  72. <scope>test</scope>
  73. </dependency>
  74. <dependency>
  75. <groupId>com.github.tomakehurst</groupId>
  76. <artifactId>wiremock-jre8</artifactId>
  77. <version>2.27.2</version>
  78. <scope>test</scope>
  79. </dependency>
  80. <!-- Spring dependencies -->
  81. <dependency>
  82. <groupId>org.springframework.boot</groupId>
  83. <artifactId>spring-boot-starter-security</artifactId>
  84. </dependency>
  85. <!-- MySQL dependency -->
  86. <dependency>
  87. <groupId>mysql</groupId>
  88. <artifactId>mysql-connector-java</artifactId>
  89. <version>8.0.32</version>
  90. </dependency>
  91. <!-- JSON Web Token (JWT) dependency -->
  92. <dependency>
  93. <groupId>io.jsonwebtoken</groupId>
  94. <artifactId>jjwt-api</artifactId>
  95. <version>0.11.5</version>
  96. </dependency>
  97. <!-- https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt-impl -->
  98. <dependency>
  99. <groupId>io.jsonwebtoken</groupId>
  100. <artifactId>jjwt-impl</artifactId>
  101. <version>0.11.5</version>
  102. <scope>runtime</scope>
  103. </dependency>
  104. <dependency>
  105. <groupId>io.jsonwebtoken</groupId>
  106. <artifactId>jjwt-jackson</artifactId>
  107. <version>0.11.5</version>
  108. <scope>runtime</scope>
  109. </dependency>
  110. <!-- Lombok dependency -->
  111. <dependency>
  112. <groupId>org.projectlombok</groupId>
  113. <artifactId>lombok</artifactId>
  114. <version>1.18.26</version>
  115. <scope>provided</scope>
  116. </dependency>
  117. </dependencies>
  118. <build>
  119. <plugins>
  120. <plugin>
  121. <groupId>org.springframework.boot</groupId>
  122. <artifactId>spring-boot-maven-plugin</artifactId>
  123. </plugin>
  124. </plugins>
  125. </build>
  126. </project>

正在创建的表的日志输出

  1. Hibernate: drop table if exists track
  2. Hibernate: drop table if exists user
  3. Hibernate: create table track (id integer not null auto_increment, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP not null, duration_ms bigint not null, explicit bit not null, isrc varchar(20) not null, name varchar(100) not null, primary key (id)) engine=InnoDB
  4. Hibernate: create table user (id integer not null auto_increment, password varchar(255) not null, username varchar(50) not null, primary key (id)) engine=InnoDB
  5. Hibernate: alter table track add constraint UK_eeljh0fjup9osa3cp6h9wqtgv unique (isrc)
  6. Hibernate: alter table user add constraint UK_sb8bbouer5wak8vyiiy4pf2bx unique (username)
ars1skjm

ars1skjm1#

  1. spring.datasource.url=jdbc:mysql://localhost:3306/newdb
  2. spring.datasource.username=root
  3. spring.datasource.password=password
  4. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  5. # JPA properties
  6. spring.jpa.show-sql=true
  7. spring.jpa.hibernate.ddl-auto=none

将此添加到application.properties文件中

7cwmlq89

7cwmlq892#

你遇到的问题是2(imho)fold。
1.你使用的MySQL表名区分大小写。
1.您正在使用JPA来管理模式,永远不要(除了快速原型)将其用于生产表。
为了避免第一个问题,你需要将你的表名包含在backtics `中,使它们不区分大小写(或者在MySQL数据库端禁用区分大小写)。

  1. @Table(name="`User`")

或者可以将属性添加到application.properties中,以自动引用表和列的名称。

  1. spring.jpa.properties.hibernate.globally_quoted_identifiers=true
  2. spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true

这应该也可以。
相反,如果你想在MySQL端禁用区分大小写的表/列,请阅读this answer
最后,建议不要像JPA那样使用模式管理。至少不要在生产环境中使用,所以最好完全禁用它,使用Flyway或Liquibase来管理模式。

  1. spring.jpa.hibernate.ddl-auto=none

PRO BONUS您的依赖项是一团糟,请清理它们,否则它们将来会回来困扰您。

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-data-jpa</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-hateoas</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-web</artifactId>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-security</artifactId>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-devtools</artifactId>
  21. <scope>runtime</scope>
  22. <optional>true</optional>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.apache.httpcomponents.client5</groupId>
  26. <artifactId>httpclient5</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>com.h2database</groupId>
  30. <artifactId>h2</artifactId>
  31. <scope>runtime</scope>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-test</artifactId>
  36. <scope>test</scope>
  37. </dependency>
  38. <dependency>
  39. <groupId>com.github.tomakehurst</groupId>
  40. <artifactId>wiremock-jre8</artifactId>
  41. <version>2.27.2</version>
  42. <scope>test</scope>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.mock-server</groupId>
  46. <artifactId>mockserver-netty</artifactId>
  47. <version>3.10.8</version>
  48. <scope>test</scope>
  49. </dependency>
  50. <dependency>
  51. <groupId>org.mock-server</groupId>
  52. <artifactId>mockserver-client-java</artifactId>
  53. <version>3.10.8</version>
  54. <scope>test</scope>
  55. </dependency>
  56. <!-- MySQL dependency -->
  57. <dependency>
  58. <groupId>mysql</groupId>
  59. <artifactId>mysql-connector-java</artifactId>
  60. </dependency>
  61. <!-- JSON Web Token (JWT) dependency -->
  62. <dependency>
  63. <groupId>io.jsonwebtoken</groupId>
  64. <artifactId>jjwt-api</artifactId>
  65. <version>0.11.5</version>
  66. </dependency>
  67. <!-- https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt-impl -->
  68. <dependency>
  69. <groupId>io.jsonwebtoken</groupId>
  70. <artifactId>jjwt-impl</artifactId>
  71. <version>0.11.5</version>
  72. <scope>runtime</scope>
  73. </dependency>
  74. <dependency>
  75. <groupId>io.jsonwebtoken</groupId>
  76. <artifactId>jjwt-jackson</artifactId>
  77. <version>0.11.5</version>
  78. <scope>runtime</scope>
  79. </dependency>
  80. <!-- Lombok dependency -->
  81. <dependency>
  82. <groupId>org.projectlombok</groupId>
  83. <artifactId>lombok</artifactId>
  84. <scope>provided</scope>
  85. </dependency>
  86. </dependencies>
  87. <build>
  88. <plugins>
  89. <plugin>
  90. <groupId>org.springframework.boot</groupId>
  91. <artifactId>spring-boot-maven-plugin</artifactId>
  92. <configuration>
  93. <excludes>
  94. <exclude>
  95. <groupId>org.projectlombok</groupId>
  96. <artifactId>lombok</artifactId>
  97. </exclude>
  98. </excludes>
  99. </configuration>
  100. </plugin>
  101. </plugins>
  102. </build>
展开查看全部
yzckvree

yzckvree3#

而不是

  1. spring.jpa.hibernate.ddl-auto=update

使用

  1. spring.jpa.hibernate.ddl-auto=none

您还需要使用tics(`)对表名进行转义,使其区分大小写。

  1. @Table(name = "`User`")

  1. @Table(name = "`Track`")

相关问题