Spring boot 使用spring jpa搜索登录用户的记录
我是spring boot的新手,正在尝试创建一个用于跟踪费用的web应用程序。我使用了spring安全性来实现登录机制。以下是费用和用户的实体 用户实体:Spring boot 使用spring jpa搜索登录用户的记录,spring-boot,spring-data-jpa,Spring Boot,Spring Data Jpa,我是spring boot的新手,正在尝试创建一个用于跟踪费用的web应用程序。我使用了spring安全性来实现登录机制。以下是费用和用户的实体 用户实体: @Entity @Data @Table(name="user") public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "user_id") private int id; @C
@Entity
@Data
@Table(name="user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "user_id")
private int id;
@Column(name = "email")
@Email(message = "*Please provide a valid Email")
@NotEmpty(message = "*Please provide an email")
private String email;
@Column(name = "password")
@Length(min = 5, message = "*Your password must have at least 5 characters")
@NotEmpty(message = "*Please provide your password")
private String password;
@Column(name = "name")
@NotEmpty(message = "*Please provide your name")
private String name;
@Column(name = "last_name")
@NotEmpty(message = "*Please provide your last name")
private String lastName;
@Column(name = "active")
private int active;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles;
}
费用实体具有用户引用
费用存储库:
@Repository("expenseRepository")
public interface ExpenseRepository extends JpaRepository<Expense,Long> {
@Query(value="SELECT sum(e.amount) from Expense e")
public Double totalExpenses();
@Query(value="select c.name as category, sum(e.amount) as amount from Category c, Expense e where c.id=e.category.id group by c.name")
public List<ExpenseByCategory> getExpensesPerCategory();
@Query(value="select e.paymentType as paymentType, sum(e.amount) as amount from Expense e group by e.paymentType")
public List<ExpenseByPaymentType> getExpensesByPaymentType();
}
@Repository(“expenseRepository”)
公共接口费用存储库扩展了JpaRepository{
@查询(value=“从费用e中选择金额(e.金额”)
公共双重开支();
@查询(value=“选择c.name作为类别,sum(e.amount)作为c类中的金额,费用e,其中c.id=e.category.id按c.name分组”)
公共列表getExpensesPerCategory();
@查询(value=“选择e.paymentType作为paymentType,按e.paymentType从费用e组中选择sum(e.amount)作为金额”)
公共列表getExpensesByPaymentType();
}
我只想根据当前登录用户的id返回其费用记录。我无法构造@Query来执行此操作并提取当前登录用户的详细信息
请帮助试试这个方法
import java.security.Principal;
public ReturnType methodName(Principal principal){
System.out.println(principal);
System.out.println(principal.getName());
}
你也可以用这个
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
System.out.println(authentication.getName());
这回答了你的问题吗?
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
System.out.println(authentication.getName());