Java 在内存数据库中的H2和Spring boot@Entity类之间存储和检索JSON类型的数据
我在spring boot应用程序中的H2内存数据库中创建了两个表,如下所示:Java 在内存数据库中的H2和Spring boot@Entity类之间存储和检索JSON类型的数据,java,json,spring-boot,hibernate,spring-data-jpa,Java,Json,Spring Boot,Hibernate,Spring Data Jpa,我在spring boot应用程序中的H2内存数据库中创建了两个表,如下所示: 餐厅餐桌 书桌 我现在要问的第一个问题是,如何使用SQL命令(特别是JSON字段“TABLES”)将数据插入到Restaurant表中,该字段实际以JSON格式存储另一个BOOKTABLE表中的数据? 这个问题正确吗 INSERT INTO RESTAURANT VALUES (2, 'Saraya', '2021-05-25', '[{"tableId": 1, "seatingCap
INSERT INTO RESTAURANT VALUES (2, 'Saraya', '2021-05-25', '[{"tableId": 1, "seatingCapacity": 12, "isAvailable": true}]');
注意:我希望餐厅表能够存储多个表,因此JSON字符串中包含数组。
接下来,我在Spring Boot项目中定义了这两个@Entity类:
@RestController
@RequestMapping("/v1/restaurants")
public class RestaurantController {
private final RestaurantService restaurantService;
public RestaurantController(RestaurantService restaurantService) {
this.restaurantService = restaurantService;
}
@GetMapping(produces = "application/json")
public List<Restaurant> getAllExistingRestaurants() {
return restaurantService.getAllRestaurants();
}
@GetMapping(value = "/{id}", produces = "application/json")
public Restaurant getRestaurantById(@PathVariable("id") Long reservationId) {
return restaurantService.getRestaurant(reservationId);
}
@PostMapping(consumes = "application/json")
private long addRestaurant(@RequestBody Restaurant restaurant) throws IOException, ClassNotFoundException {
restaurantService.AddNewRestaurant(restaurant);
return restaurant.getId();
}
}
package com.assignment.restaurantmanagement.service;
import com.assignment.restaurantmanagement.model.BookTable;
import com.assignment.restaurantmanagement.model.Restaurant;
import com.assignment.restaurantmanagement.repository.RestaurantRepository;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@Service
public class RestaurantService {
private final RestaurantRepository restaurantRepository;
private final TableService tableService;
public RestaurantService(RestaurantRepository restaurantRepository, TableService tableService) {
this.restaurantRepository = restaurantRepository;
this.tableService = tableService;
}
// Retrieve all restaurants
public List<Restaurant> getAllRestaurants() {
List<Restaurant> restaurants = new ArrayList<>();
restaurantRepository.findAll().forEach(restaurants::add);
return restaurants;
}
// Retrieves a restaurant based on restaurant id
public Restaurant getRestaurant(Long id) {
Optional<Restaurant> restaurant = restaurantRepository.findById(id);
return restaurant.get();
}
// Adds a new restaurant
public void AddNewRestaurant(Restaurant restaurant) throws IOException, ClassNotFoundException
{
//*Some code here*
}
}
public interface RestaurantRepository extends CrudRepository<Restaurant, Long> {
}
public interface TableRepository extends CrudRepository<BookTable, Long> {
}
@RestController
@请求映射(“/v1/restaurants”)
公共类餐厅控制器{
私人最终餐厅服务餐厅服务;
公共餐厅控制员(餐厅服务餐厅服务){
this.restaurantService=restaurantService;
}
@GetMapping(products=“application/json”)
公共列表GetAllexistingTaurants(){
return restaurantService.getAllRestaurants();
}
@GetMapping(value=“/{id}”,products=“application/json”)
公共餐厅getRestaurantById(@PathVariable(“id”)长预订id){
返回restaurantService.getRestaurant(预订ID);
}
@后期映射(consumes=“application/json”)
private long addRestaurant(@RequestBody Restaurant Restaurant)抛出IOException,ClassNotFoundException{
餐厅服务。新增餐厅(餐厅);
return restaurant.getId();
}
}
正如您在上面所看到的,目前我正在尝试3个REST API:GetAll()、GetByID()和Post()。下面是服务类代码:
RestaurantService.java
@RestController
@RequestMapping("/v1/restaurants")
public class RestaurantController {
private final RestaurantService restaurantService;
public RestaurantController(RestaurantService restaurantService) {
this.restaurantService = restaurantService;
}
@GetMapping(produces = "application/json")
public List<Restaurant> getAllExistingRestaurants() {
return restaurantService.getAllRestaurants();
}
@GetMapping(value = "/{id}", produces = "application/json")
public Restaurant getRestaurantById(@PathVariable("id") Long reservationId) {
return restaurantService.getRestaurant(reservationId);
}
@PostMapping(consumes = "application/json")
private long addRestaurant(@RequestBody Restaurant restaurant) throws IOException, ClassNotFoundException {
restaurantService.AddNewRestaurant(restaurant);
return restaurant.getId();
}
}
package com.assignment.restaurantmanagement.service;
import com.assignment.restaurantmanagement.model.BookTable;
import com.assignment.restaurantmanagement.model.Restaurant;
import com.assignment.restaurantmanagement.repository.RestaurantRepository;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@Service
public class RestaurantService {
private final RestaurantRepository restaurantRepository;
private final TableService tableService;
public RestaurantService(RestaurantRepository restaurantRepository, TableService tableService) {
this.restaurantRepository = restaurantRepository;
this.tableService = tableService;
}
// Retrieve all restaurants
public List<Restaurant> getAllRestaurants() {
List<Restaurant> restaurants = new ArrayList<>();
restaurantRepository.findAll().forEach(restaurants::add);
return restaurants;
}
// Retrieves a restaurant based on restaurant id
public Restaurant getRestaurant(Long id) {
Optional<Restaurant> restaurant = restaurantRepository.findById(id);
return restaurant.get();
}
// Adds a new restaurant
public void AddNewRestaurant(Restaurant restaurant) throws IOException, ClassNotFoundException
{
//*Some code here*
}
}
public interface RestaurantRepository extends CrudRepository<Restaurant, Long> {
}
public interface TableRepository extends CrudRepository<BookTable, Long> {
}
package com.assignment.restaurantmanagement.service;
导入com.assignment.restaurantmanagement.model.BookTable;
导入com.assignment.restaurantmanagement.model.Restaurant;
导入com.assignment.restaurantmanagement.repository.restaurantpository;
导入com.fasterxml.jackson.core.JsonProcessingException;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Service;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Optional;
@服务
公营餐厅服务{
私人最终餐厅;
私有最终表服务表服务;
公共餐厅服务{
this.restaurantRepository=restaurantRepository;
this.tableService=表服务;
}
//检索所有餐厅
公共列表getAllRestaurants(){
列表餐厅=新建ArrayList();
restaurantRepository.findAll().forEach(restaurants::add);
返回餐厅;
}
//根据餐厅id检索餐厅
公共餐厅(长id){
可选餐厅=restaurantRepository.findById(id);
返回餐厅;
}
//增加一家新餐厅
public void AddNewRestaurant(Restaurant Restaurant)抛出IOException,ClassNotFoundException
{
//*这里有一些代码*
}
}
下面是为每个实体定义的存储库类:
RestaurantRepository.java
@RestController
@RequestMapping("/v1/restaurants")
public class RestaurantController {
private final RestaurantService restaurantService;
public RestaurantController(RestaurantService restaurantService) {
this.restaurantService = restaurantService;
}
@GetMapping(produces = "application/json")
public List<Restaurant> getAllExistingRestaurants() {
return restaurantService.getAllRestaurants();
}
@GetMapping(value = "/{id}", produces = "application/json")
public Restaurant getRestaurantById(@PathVariable("id") Long reservationId) {
return restaurantService.getRestaurant(reservationId);
}
@PostMapping(consumes = "application/json")
private long addRestaurant(@RequestBody Restaurant restaurant) throws IOException, ClassNotFoundException {
restaurantService.AddNewRestaurant(restaurant);
return restaurant.getId();
}
}
package com.assignment.restaurantmanagement.service;
import com.assignment.restaurantmanagement.model.BookTable;
import com.assignment.restaurantmanagement.model.Restaurant;
import com.assignment.restaurantmanagement.repository.RestaurantRepository;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@Service
public class RestaurantService {
private final RestaurantRepository restaurantRepository;
private final TableService tableService;
public RestaurantService(RestaurantRepository restaurantRepository, TableService tableService) {
this.restaurantRepository = restaurantRepository;
this.tableService = tableService;
}
// Retrieve all restaurants
public List<Restaurant> getAllRestaurants() {
List<Restaurant> restaurants = new ArrayList<>();
restaurantRepository.findAll().forEach(restaurants::add);
return restaurants;
}
// Retrieves a restaurant based on restaurant id
public Restaurant getRestaurant(Long id) {
Optional<Restaurant> restaurant = restaurantRepository.findById(id);
return restaurant.get();
}
// Adds a new restaurant
public void AddNewRestaurant(Restaurant restaurant) throws IOException, ClassNotFoundException
{
//*Some code here*
}
}
public interface RestaurantRepository extends CrudRepository<Restaurant, Long> {
}
public interface TableRepository extends CrudRepository<BookTable, Long> {
}
public interface RestaurantRepository扩展了crudepository{
}
TableRepository.java
@RestController
@RequestMapping("/v1/restaurants")
public class RestaurantController {
private final RestaurantService restaurantService;
public RestaurantController(RestaurantService restaurantService) {
this.restaurantService = restaurantService;
}
@GetMapping(produces = "application/json")
public List<Restaurant> getAllExistingRestaurants() {
return restaurantService.getAllRestaurants();
}
@GetMapping(value = "/{id}", produces = "application/json")
public Restaurant getRestaurantById(@PathVariable("id") Long reservationId) {
return restaurantService.getRestaurant(reservationId);
}
@PostMapping(consumes = "application/json")
private long addRestaurant(@RequestBody Restaurant restaurant) throws IOException, ClassNotFoundException {
restaurantService.AddNewRestaurant(restaurant);
return restaurant.getId();
}
}
package com.assignment.restaurantmanagement.service;
import com.assignment.restaurantmanagement.model.BookTable;
import com.assignment.restaurantmanagement.model.Restaurant;
import com.assignment.restaurantmanagement.repository.RestaurantRepository;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@Service
public class RestaurantService {
private final RestaurantRepository restaurantRepository;
private final TableService tableService;
public RestaurantService(RestaurantRepository restaurantRepository, TableService tableService) {
this.restaurantRepository = restaurantRepository;
this.tableService = tableService;
}
// Retrieve all restaurants
public List<Restaurant> getAllRestaurants() {
List<Restaurant> restaurants = new ArrayList<>();
restaurantRepository.findAll().forEach(restaurants::add);
return restaurants;
}
// Retrieves a restaurant based on restaurant id
public Restaurant getRestaurant(Long id) {
Optional<Restaurant> restaurant = restaurantRepository.findById(id);
return restaurant.get();
}
// Adds a new restaurant
public void AddNewRestaurant(Restaurant restaurant) throws IOException, ClassNotFoundException
{
//*Some code here*
}
}
public interface RestaurantRepository extends CrudRepository<Restaurant, Long> {
}
public interface TableRepository extends CrudRepository<BookTable, Long> {
}
public interface TableRepository扩展了crudepository{
}
我还有以下问题: