【问题标题】:How to fetch data from multiple tables and put into an custom object? spring boot?如何从多个表中获取数据并放入自定义对象?弹簧靴?
【发布时间】:2021-08-15 21:08:09
【问题描述】:

JpaRepository

public interface SessionsStoreRepository extends JpaRepository<SessionsStore, Integer> {

SessionsStore findBySessionId(String sessionId);

//JPQL
@Query(value = "SELECT s.username,s.loginTime,s.logoutTime, e.firstName, e.lastName from SessionsStore s, Employee e "
        + " where e.username = s.username and s.isActive= 1")
List<ActiveSession> findUsers();           
}

ActiveSession

import java.time.LocalDateTime;
import lombok.Data;

@Data
public class ActiveSession {

    private String username;
    private String firstName;
    private String lastName;
    private LocalDateTime loginTime;
    private LocalDateTime logoutTime;
}

如何将@Query(...) 获取的数据存储在 ActiveSession 对象中或者还有其他方法吗?我尝试将数据存储到对象(即 List)中,但我使用 ftl 进行查看,除非对象具有 getter,否则它不允许您获取数据。

【问题讨论】:

    标签: spring-boot jpql freemarker


    【解决方案1】:

    您可以使用基于类的投影来实现这一点。

    更多信息here,或者有一个很好的例子说明如何使用它here

    【讨论】:

      猜你喜欢
      • 2018-03-05
      • 1970-01-01
      • 2019-05-03
      • 1970-01-01
      • 2023-02-15
      • 1970-01-01
      • 2017-12-22
      • 1970-01-01
      • 2018-12-01
      相关资源
      最近更新 更多