我正在尝试在表上实施分组依据,并希望从存储库中实施

时间:2019-06-23 10:38:24

标签: spring-boot spring-data-jpa jpql

我有一个表tbl_rating,我正在尝试获取该表中现有用户的平均o = rating。 这是我的桌子。

CREATE TABLE `tbl_user` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  `email` varchar(255) NOT NULL,
  `contact` varchar(255) NOT NULL,
  `status` enum('active','inactive') NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `email` (`email`),
  UNIQUE KEY `contact` (`contact`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1

这是tbl_rating

CREATE TABLE `tbl_rating` (
  `id` bigint(255) NOT NULL AUTO_INCREMENT,
  `user_id` bigint(20) NOT NULL,
  `point` int(1) NOT NULL,
  `status` enum('active','inactive') NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1

这是我正在尝试从存储库中实现的查询。

SELECT r.user_id, u.name, AVG(r.point) AS average
FROM tbl_rating r LEFT JOIN tbl_user u ON u.id=r.user_id
GROUP BY r.user_id;

这是我的控制器

@RequestMapping(value = "/user/rating/avg/individual", method = RequestMethod.POST)
    public JsonNode getAvgRatingOfIndividual(HttpServletRequest httpServletRequest) throws IOException {

        JSONArray response = new JSONArray();

        List<Rating> ratings = new ArrayList<>();

        try {
            ratings = ratingService.getAvgRatingOfIndividual();

            if (ratings.size() > 0) {

            } else {
                response = new JSONArray();
            }
        } catch (Exception e) {
            return objectMapper.readTree(utility.createResponse(500, KeyWord.ERROR, e.toString()).toString());
        }

        return objectMapper.readTree(response.toString());
    }

以下是我遇到的服务和存储库 服务

public List<Rating> getAvgRatingOfIndividual() {
        return ratingRepository.??
    }

存储库

@Repository
public interface RatingRepository extends JpaRepository<Rating, Long> {
}

评分模型

@Entity
@Table(name = "tbl_rating")
public class Rating {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    long id;

    @ManyToOne
    @JoinColumn(name = "user_id")
    User user;

    @Column(name = "point")
    int point;

    @Column(name = "status")
    String status;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public int getPoint() {
        return point;
    }

    public void setPoint(int point) {
        this.point = point;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

}

用户模型

@Entity
@Table(name = "tbl_user")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    long id;

    @Column(name = "name")
    String name;

    @Column(name = "email")
    String email;

    @Column(name = "contact")
    String contact;

    @Column(name = "status")
    String status;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getContact() {
        return contact;
    }

    public void setContact(String contact) {
        this.contact = contact;
    }

}

如果能在Spring Boot存储库中编写本机查询方面获得任何帮助,我将不胜感激

1 个答案:

答案 0 :(得分:4)

您可以在DTO中使用它,并将其隐式填充到存储库中。

package com.test.dto;

@Value
public class RatingDTO{
  private Long userId;
  private String name;
  private Double average;
}

在存储库类中:

//RatingRepository.java
@Repository
public interface RatingRepository extends JpaRepository<Rating, Long> {
  @Query("SELECT new com.test.dto.RatingDTO(R.user.id, R.user.name, AVG(R.point)) FROM Rating R GROUP BY R.user.id")
  List<RatingDTO> getAveragePoints();
}