我想设置两个不同的表。我已经尝试了第一个模型和相应的服务、控制器和存储。一切都很好,但当我使用相同的代码,只更改模型,并开发一组服务、控制器和存储时。它不能显示出我所期望的。
我的型号:
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.SecondaryTable;
import javax.persistence.Table;
@Data
@AllArgsConstructor
@NoArgsConstructor
@javax.persistence.Entity
@Table(schema = "order")
public class Order {
@Id
@GeneratedValue
private int id;
private String product_name;
private int quantity;}
mycontroller:
package com.javatechie.trymysql.contoller;
import com.javatechie.trymysql.Entity.Order;
import com.javatechie.trymysql.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class OrderController {
@Autowired
private OrderService service;
@PostMapping("/add/orders")
public List<Order> addOrders(@RequestBody List<Order> orders){
return service.saveOrders(orders);
}
}
服务:
package com.javatechie.trymysql.service;
import com.javatechie.trymysql.Entity.Order;
import com.javatechie.trymysql.repository.OrderRepository;
import com.javatechie.trymysql.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class OrderService {
@Autowired
private OrderRepository Repository;
public List<Order> saveOrders(List<Order> order) {
return Repository.saveAll(order);
}
}
存储库:
package com.javatechie.trymysql.repository;
import com.javatechie.trymysql.Entity.Order;
import com.javatechie.trymysql.Entity.Product;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface OrderRepository extends JpaRepository<Order,Integer> {
}
我的ide模式是:
com.javatechie.trymysql
-entity
--Product
--Order
-controller
--productcontroller
--ordercontroller
-service
--productservice
--orderservice
-repository
--productrespository
--orderreposiory
-productconfig
-Trymysqlapplication
邮差请求:
[{
"proudct_name":"sand",
"quantity":5
},{
"product_name":"fruit",
"quantity":2
}]
错误:
{
"timestamp": "2022-10-17T13:14:03.284+00:00",
"status": 500,
"error": "Internal Server Error",
"path": "/add/orders"
}
每次我运行服务器时,它都会显示以下日志:
Hibernate: select product0_.id as id1_1_0_, product0_.name as name2_1_0_, product0_.price as price3_1_0_, product0_.quantity as quantity4_1_0_ from product_tbl product0_ where product0_.id=?
Hibernate: select product0_.id as id1_1_0_, product0_.name as name2_1_0_, product0_.price as price3_1_0_, product0_.quantity as quantity4_1_0_ from product_tbl product0_ where product0_.id=?
但它仍然工作良好
稍后我添加第二个表,它只是向我显示:休眠:从订单order0_中选择order0.id作为id1_0_0_,order0.prproduct_name作为product_2_0_0_,order0_quantity作为quantity3_0_0_其中order0.id=?2022-10-17 21:21:11.916警告3828-[nio-9191-exec-1]o.h.engine.jdbc.spi.SqlExceptionHelper:SQL错误:1064,SQL状态:42000
我不知道为什么我的日志文件会自动生成这个sql,并在它不是模型的情况下得到错误;产品";?我不知道在尝试使用第二种型号(订单(时,我做错了哪一步
从注释中提取详细信息。
因为表名order
是MySQL中的一个保留关键字。因此,您必须将表名更改为另一个不是保留关键字的名称。更改为orders
解决了此问题。