我有这 3 个独立的表,即学生、教师和主题。这里的独立是指这些表之间没有关系。
我想要所有这些表的数量。 SQL 查询看起来像 -
import numpy as np
import cv2 as cv
from sklearn.cluster import MeanShift, estimate_bandwidth
img = cv.imread(your_image)
# filter to reduce noise
img = cv.medianBlur(img, 3)
# flatten the image
flat_image = img.reshape((-1,3))
flat_image = np.float32(flat_image)
# meanshift
bandwidth = estimate_bandwidth(flat_image, quantile=.06, n_samples=3000)
ms = MeanShift(bandwidth, max_iter=800, bin_seeding=True)
ms.fit(flat_image)
labeled=ms.labels_
# get number of segments
segments = np.unique(labeled)
print('Number of segments: ', segments.shape[0])
# get the average color of each segment
total = np.zeros((segments.shape[0], 3), dtype=float)
count = np.zeros(total.shape, dtype=float)
for i, label in enumerate(labeled):
total[label] = total[label] + flat_image[i]
count[label] += 1
avg = total/count
avg = np.uint8(avg)
# cast the labeled image into the corresponding average color
res = avg[labeled]
result = res.reshape((img.shape))
# show the result
cv.imshow('result',result)
cv.waitKey(0)
cv.destroyAllWindows()
现在我想将此结果映射到 dto 。 DTO 看起来像
SELECT
(SELECT COUNT(*) FROM Student as ST,
(SELECT COUNT(*) FROM Teacher as TE,
(SELECT COUNT(*) FROM Subject as SU
存储库调用看起来像 -
public class CountDto{
Integer student;
Integer teacher;
Integer subject;
}
调用此函数时出现以下错误提示
@Query(value = "SELECT\r\n"
+ " (SELECT COUNT(*) FROM Student) as ST, \r\n"
+ " (SELECT COUNT(*) FROM Teacher) as TE,\r\n"
+ " (SELECT COUNT(*) FROM Subject) as SU", nativeQuery = true)
public CountDto getCount();
我应该如何将我的回复转换为所需的 DTO?
答案 0 :(得分:1)
您可以将 CountDto
声明为公共接口,它应该可以工作。就 Spring
而言,它被称为 Projection。或者您可以将 SqlResultSetMapping 或 ConstructorResult 与您的班级一起使用。
您不必通过任何实体类来实现它,即使在存储库文件中也可以像独立接口一样创建它:
public interface StudentRepository extends CrudRepository<Student, Long> {
@Query(value = "SELECT\r\n"
+ " (SELECT COUNT(*) FROM Student) as ST, \r\n"
+ " (SELECT COUNT(*) FROM Teacher) as TE,\r\n"
+ " (SELECT COUNT(*) FROM Subject) as SU", nativeQuery = true)
Counts getCount();
public static interface Counts {
Integer getST();
Integer getTE();
Integer getSU();
}
}
答案 1 :(得分:-1)
所以这是要做的答案- DTO 看起来像
public CountDto{
private Integer studentTotal;
private Integer teacherTota;
private Integer subjectTotal;
}
存储库调用 -
@Query(value = "SELECT\r\n"
+ " (SELECT COUNT(*) FROM Student) as ST, \r\n"
+ " (SELECT COUNT(*) FROM Teacher) as TE,\r\n"
+ " (SELECT COUNT(*) FROM Subject) as SU", nativeQuery = true)
public Map<String,Integer> getCount();
最后是serviceImpl--
public CountDto getCount{
CountDto CountValue = new CountDto();
Map<String,Integer> map = repository.getCount();
for (Map.Entry<String,Integer> entry : map.entrySet()) {
if(entry.getKey().equals("ST"))
CountValue.setStudentTotal( entry.getValue());
if(entry.getKey().equals("TE"))
CountValue.setTeacherTotal( entry.getValue());
if(entry.getKey().equals("SU"))
CountValue.setSubjectTotal( entry.getValue());
}
return CountValue ;
}
但解决方案对我来说似乎很复杂。需要任何更简单的方法。
答案 2 :(得分:-1)
在纯 Hibernate/JPA 用法中,这是一个简单的动态实例化查询(JPA 称之为“构造函数结果”):
select new CountDTO(
(SELECT COUNT(*) FROM Student) as ST,
...
)
这里对 Spring 一无所知,尽管有一个警告......在尝试有用时,它经常“妨碍”。不确定这里的情况...您是否尝试过直接 Hibernate/JPA?