【发布时间】:2017-11-22 12:27:44
【问题描述】:
我使用以下代码创建了一个示例项目。即使我没有在 data.sql 中提供表创建语句,它也在创建表。如何阻止它。示例代码如下所示
你能告诉我我做错了什么吗?我已经删除了下面的导入语句,因为帖子不允许在这里放这么多代码。
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;
}
}
【问题讨论】:
-
每次启动应用程序时都会创建表吗?如果是这样,那是正常的,因为它是 H2 db
-
但我不希望他们创建它们,我想为此添加 create 语句。
标签: spring spring-boot h2