Spring Boot:使用JPA希望从表中获取唯一值 [英] Spring Boot : Using JPA want to get the unique value from table

查看:158
本文介绍了Spring Boot:使用JPA希望从表中获取唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子

CatID |分类名称| scatID | subCategoryName 2个用户1 x 2用户2年 2用户3 z 2用户4 a 2位用户5 b

CatID | CategoryName | scatID | subCategoryName 2 User 1 x 2 User 2 y 2 User 3 z 2 User 4 a 2 User 5 b

我能够使用SpringBoot获取JSON formate中的所有值.

I am able to get all the value in JSON formate using SpringBoot.

我的要求: 我想获得唯一的CategoryName属性,但在当前情况下,我将使所有用户获得5次访问.

My Requirement : I want to get the unique CategoryName attribute but in current scenario I am getting all User coming 5 times.

我正在寻找解决方案.请任何人都可以帮助克服使用Spring Boot JPA的实现.

I am looking for solution. Please any one can help to get over using Spring Boot JPA implementation.

推荐答案

您可以在存储库界面中使用Query批注. 例如,下面的代码段返回所有不同的categoryName.

You can use the Query annotation in your repository interface. For example, below snippet code return all distinct categoryName.

声明一个简单的bean类:

Declare a simple bean class:

package com.example.test;

public class CategoryNameClass {
    private String CategoryName;

    public CategoryNameClass(String CategoryName) {
        this.CategoryName = CategoryName;
    }

    public String getCategoryName() {
        return CategoryName;
    }

    public void setCategoryName(String categoryName) {
        CategoryName = categoryName;
    }
}

然后使用以下查询:

public interface ARepo extends JpaRepository<A, String> {

    @Query("SELECT DISTINCT new com.example.test.CategoryNameClass(a.categoryName) FROM A a ")
    List<CategoryNameClass> findAllCategoryName();
}

这篇关于Spring Boot:使用JPA希望从表中获取唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆