如何编写一个显示仅分配给登录用户的列表的 SQL 查询?
Posted
技术标签:
【中文标题】如何编写一个显示仅分配给登录用户的列表的 SQL 查询?【英文标题】:How do I write a SQL Query that Displays a List assigned only to only the logged in User? 【发布时间】:2021-12-13 18:50:10 【问题描述】:我有一组具有不同角色类型的用户营销人员、管理员、总经理。注册客户帐户时,有一列获取分配给客户的营销人员的名称。我在用户和客户之间创建了@ManyToOne 和@OneToMany 关系。当用户类型营销人员登录软件时,我希望营销人员只看到注册到已登录营销人员名称的客户。这似乎很复杂,因为首先我需要使用用户名获取登录的营销人员详细信息,然后获取名字和姓氏,然后显示分配给该名称的客户列表。
这是客户实体
@Entity
@JsonIgnoreProperties("hibernateLazyInitializer","handler")
public class Customer implements Serializable
/**
*
*/
private static final long serialVersionUID = 8348682056500740593L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String userName;
private String password;
private String firstName ;
private String lastName;
private String gender;
private String Address;
private String maritalStatus;
private String category;
private String motherMaidenName;
private String idType;
private String idNumber;
private String phoneNumber;
private String email;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date dateOfBirth;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date registrationDate;
@JsonIgnoreProperties("hibernateLazyInitializer", "handler")
@ManyToOne(targetEntity = User.class,
fetch = FetchType.LAZY )
@JoinColumn(name="marketer_id")
private User marketer ;
@JsonBackReference
@OneToMany(mappedBy="customer_id",cascade = CascadeType.ALL, targetEntity=Investment.class)
private List <Investment> investment;
这是用户实体
@Entity
public class User
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String firstName ;
private String lastName;
private String username;
private String password;
private String Gender;
private String phoneNumber;
private String email;
private String branch;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date createdDate;
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(
name = "users_roles",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "role_id")
)
private Set<UserRole> userRole = new HashSet<>();
@Enumerated(EnumType.STRING)
private UserStatus status;
@JsonBackReference
@OneToMany(mappedBy="marketer",cascade = CascadeType.ALL, targetEntity=Customer.class)
private List <Customer> customer;
这是 用户存储库,其中包含 JWT 身份验证管理器正在使用的 FindByUsername 方法
@Repository
public interface UserAccountRepository extends JpaRepository <User, Long>
Optional<User> findById(Long id);
User findByUsername(String username);
更新:我从查询中得到空响应,我做错了什么?这是更新后的 Customer 控制器类
@CrossOrigin(origins = "http://localhost:3000")
@RestController
public class CustomerController
@Autowired
CustomerAccountService customerRepo;
@Autowired
UserAccountService userRepo;
@GetMapping(value="marketers/customers")
public List<Customer> getLlistByMarketerName()
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
User loggedInUser =userRepo.findByUserName(authentication.getName());
System.out.println("logged in user:"+ loggedInUser);
return customerRepo.findByMarketer(loggedInUser);
我打印了 loggedInUser 变量,但它显示为 null。
【问题讨论】:
【参考方案1】:您的 CustomerRepository 应该可以轻松处理这个问题:
public interface CustomerRepository extends JpaRepository<Customer, Long>
List<Customer> findByMarketer(User marketer);
如果您想更明确并显示查询:
public interface CustomerRepository extends JpaRepository<Customer, Long>
@Query("select customer from Customer customer join customer.marketer marketer where marketer = :marketer")
List<Customer> findByMarketer(User marketer);
传递的用户(营销人员)是从 UserAccountRepository 获取的当前用户。
【讨论】:
我得到的是空响应以上是关于如何编写一个显示仅分配给登录用户的列表的 SQL 查询?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Drupal 7 中创建一个仅显示基于当前用户字段的过滤列表的视图
iOS 应用程序中的 Twitter 提要 - 我如何仅从我自己的帐户获取列表提要 [无需应用用户登录 Twitter]
Laravel如何通过ajax按名称或ID搜索在前端仅显示用户帐户的附加患者