【发布时间】:2019-04-06 10:42:18
【问题描述】:
我有一个自定义对象
@Component
public class SaleReport {
private Date date;
private String customerName;
private String phoneNo;
private String productName;
private Integer quantity;
private Double totalAmount;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getPhoneNo() {
return phoneNo;
}
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public Integer getQuantity() {
return quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
public Double getTotalAmount() {
return totalAmount;
}
public void setTotalAmount(Double totalAmount) {
this.totalAmount = totalAmount;
}
}
我需要使用JpaRepository<Sale, Integer> 返回此对象的列表
SaleRepository 如下:
@Repository
public interface SaleRepository extends JpaRepository<Sale, Integer> {
@Query(value = "SELECT B.order_date AS date, E.customer_name AS customerName, " +
"E.phone_no AS phoneNo, D.product_name AS productName, C.quantity AS quantity, " +
"C.total_price AS totalAmount " +
"FROM SALE A " +
"INNER JOIN A.quotation_id B " +
"INNER JOIN B.quotation_id C " +
"INNER JOIN C.product_id D " +
"INNER JOIN B.customer_id E " +
"WHERE (B.order_date BETWEEN :from_date AND :to_date)", nativeQuery = true)
public List<SaleReport> getSaleReportByDate(@Param("from_date")String fromDate, @Param("to_date")String toDate);
}
这是我的控制器方法:
@GetMapping("api/report/sale/{from_date}/{to_date}")
public List<SaleReport> getSaleReportByDate(@PathVariable("from_date") String fromDate, @PathVariable("to_date") String toDate){
return saleRepository.getSaleReportByDate(fromDate, toDate);
}
当我运行代码时,它显示了这个错误:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'a'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_161]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_161]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_161]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_161]
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.47.jar:5.1.47]
at com.mysql.jdbc.Util.getInstance(Util.java:408) ~[mysql-connector-java-5.1.47.jar:5.1.47]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:944) ~[mysql-connector-java-5.1.47.jar:5.1.47]
我的数据库名称没问题。当我在其他方法中请求时,它可以完美运行。 请给我一个理想的解决方案。
【问题讨论】:
-
此异常是由于应用程序找不到数据库名称。通过手动检查确保您有名为“a”的数据库。
-
为什么查询正在搜索名为“a”的数据库..?这里是我的数据库信息
spring.datasource.url=jdbc:mysql://localhost:3366/andropos_db spring.datasource.username=root spring.datasource.password=1525 -
FROM SALE A " + "INNER JOIN A.quotation_id B " + 看到这个。你正在尝试只对 'a' 数据库做一些事情
-
我已将
SALE A更改为仅SALE。在这种情况下,查询正在搜索名为“sale”的数据库。现在呢? -
SaleReport 用“组件”注释。我认为它应该用“实体”注释?
标签: java mysql spring spring-boot jpql