Spring Data JPA - 将列名和值作为参数传递 [英] Spring Data JPA - Pass column name and value as parameters

查看:108
本文介绍了Spring Data JPA - 将列名和值作为参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道如何将列名及其值传递给 Spring Data JPA 中的 @Query 注释.

I just wanted to know how to pass column name and its value to @Query annotation in Spring Data JPA.

基本上列名将是静态的,我们过去常常将每一列作为一个元素放在 Entity 类中.但在这里我想要一些不同的东西,这里的列名将是动态的,我将此值作为参数传递给存储库中定义的方法.

Basically column names will be static and we used to put every column as a element in Entity class. But here I want something different, here column name will be dynamic I will be passing this value as Parameter to the method defined in repository.

表格 - 日历

- id、PersonName、1、2、3......31

Columns - id, PersonName, 1, 2, 3......31

上面是表结构,1,2,3,.....31 是代表日历日的列名,我们在这些列中有值.我正在使用 Spring Data JPA 从数据库中获取数据.在这里,我只想获取特定日期的人名.下面给出存储库中定义的函数.

Above is the table structure, 1,2,3,.....31 are the column names which represents calendar days and we have values in that columns. I'm using Spring Data JPA to fetch data from DB. Here I just wanted to fetch person name for a particular day. Below given the function defined in repository.

@Query("select c from Calendar c where :calendarDay=:value")
List<Calendar> getPersonName(@Param("calendarDay") String calendarDay, @Param("value") String value);

这对我不起作用.任何帮助将不胜感激.

This is not working for me. Any help would be appreciated.

推荐答案

看一看 sping 数据规范.您可以在那里找到您的解决方案!
阅读文档您可以看到,如果 Calendar 是您的域(我会尝试为我的域找到一个不同的名称,有一个 Calendar 类已经在 J​​ava SE 中),然后你可以使用类似上面的东西,

Take a look at sping data Specifications. You can find your solution there!
Reading the docs you can see that if Calendar is your domain (I would try to find a different name for my domain, there is a Calendar class in Java SE already), then you could use something like the above,

@Repository
public interface CalendarRepository extends JpaRepository<Calendar, Integer>, JpaSpecificationExecutor<Calendar> {
}


public class CalendarSpecification implements Specification<Calendar> {

    private String randomColumnName; // A varchar column.
    private String valueToSearchFor;

    public CalendarSpecification(String randomColumnName, String valueToSearchFor) {
        this.randomColumnName = randomColumnName;
        this.valueToSearchFor = valueToSearchFor;
    }

    @Override
    public Predicate toPredicate(Root<Calendar> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
        return builder.and(builder.equal(root.<String>get(this.randomColumnName), this.valueToSearchFor));
    }
}


@Service
public class CalendarService {

    @Autowired
    private CalendarRepository calendarRepository;

    public List<Calendar> findCustom(String randomColumnName, String valueToSearchFor) {
        CalendarSpecification cs = new CalendarSpecification(randomColumnName, valueToSearchFor);
        return calendarRepository.find(cs);
        // Or using lambda expression - without the need of CalendarSpecification class.
//      return calendarRepository.find((Root<ProductCategory> root, CriteriaQuery<?> query, CriteriaBuilder builder) -> {
//          return builder.and(builder.equal(root.<String>get(randomColumnName), valueToSearchFor));
//      });
    }
}

这篇关于Spring Data JPA - 将列名和值作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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