Spring boot H2数据库应用程序

时间:2017-11-22 12:27:44

标签: spring spring-boot h2

我创建了一个包含以下代码的示例项目。即使我没有在data.sql中提供表创建语句,它也在创建表。如何阻止它。示例代码位于

下方 你能告诉我我做错了什么吗?我删除了下面的import语句,因为帖子不允许在这里输入这么多代码。

package com.example.demo;
// Model class 
@Entity
@Table(name="reservation")
public class Reservation {
 @Id
 private Long id;
 @Column(name="user_id")
 private Long userId;    
 @Column(name="party_size")
 private int partySize;
 @Column(name="restaurant_id")
 private Long restaurantId;
 @Column(name="date")
 private LocalDateTime dt;
 public Reservation() {}
 public Reservation(Long id,  Long userId, int partySize) {
  this.id = id;
  this.userId = userId;
  this.partySize = partySize;
 }
 public Long getId() {
  return id;
 }
 public void setId(Long id) {
  this.id = id;
 }
 public Long getUserId() {
  return userId;
 }
 public void setUserId(Long userId) {
  this.userId = userId;
 }
 public int getPartySize() {
  return partySize;
 }
 public void setPartySize(int partySize) {
  this.partySize = partySize;
 }
  public Long getRestaurantId() {
  return restaurantId;
 }
 public void setRestaurantId(Long restaurantId) {
  this.restaurantId = restaurantId;
 }
 public LocalDateTime getDt() {
  return dt;
 }
 public void setDt(LocalDateTime dt) {
  this.dt = dt;
 }
}

package com.example.demo;

@SpringBootApplication
public class ReservationApp {
 public static void main(String[] args) {
  SpringApplication.run(ReservationApp.class, args);
 }
}

package com.example.demo;
@RestController
@RequestMapping("/v1")
public class ReservationController {

 @Autowired
 private ReservationService reservationService;

 // ------------ Retrieve all reservations ------------
 @RequestMapping(value = "/reservations", method = RequestMethod.GET)
 public List getAllReservations() {

  return reservationService.getAllReservations();

 }

package com.example.demo;
import org.springframework.data.repository.CrudRepository;
public interface ReservationRepository extends CrudRepository<Reservation,String> {
 }

 package com.example.demo;

@Service 
public class ReservationService {

 @Autowired
 private ReservationRepository reservationRepository;

 // Retrieve all rows from table and populate list with objects
 public List getAllReservations() {
  List reservations = new ArrayList<>();
  reservationRepository.findAll().forEach(reservations::add);
  return reservations;
 }
 }

3 个答案:

答案 0 :(得分:2)

尝试删除spring boot hibernate配置

spring.jpa.hibernate.ddl-auto = update

哪个能够从实体创建/更新数据库模式

答案 1 :(得分:0)

要禁用自动DDL生成,请在false中将以下属性设置为application.properties

spring.jpa.generate-ddl = false

有关详细信息和细粒度控制,请参阅documentation

答案 2 :(得分:0)

在application.properties中将ddl生成设置为none:

spring.jpa.hibernate.ddl-auto=none