We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.NoRepositoryBean; import org.springframework.data.repository.query.Param; import org.springframework.transaction.annotation.Transactional; import java.time.Instant; import java.util.List; import java.util.Optional; /** * GenericTokenRepository 共同接口 * * @param <T> UserToken/TeacherToken 这两个实体公用一个GenericTokenRepository */ @NoRepositoryBean public interface GenericTokenRepository<T extends GenericToken> extends JpaRepository<T, Long> { /** * 根据 token 查找 UserToken * * @param token * @return */ Optional<T> findByToken(String token); /** * 查找操作 * @param userId * @param expiresInBorder * @return */ @Query(value = "select t from #{#entityName} t where t.userId = :userId and t.expiresIn > :expiresInBorder and t.deactivatedAt is null") List<T> findValidTokens(@Param("userId") Long userId, @Param("expiresInBorder") Long expiresInBorder); /** * 更新操作 * @param ids * @param reason * @param deactivatedAt */ @Modifying @Query(value = "update #{#entityName} set deactivationReason = :deactivationReason, deactivatedAt = :deactivatedAt where id in :ids and deactivatedAt is null") @Transactional void expireTokensWithReason(@Param("ids") Iterable<Long> ids, @Param("deactivationReason") DeactivationReason reason, @Param("deactivatedAt") Instant deactivatedAt); }
public interface UserTokenRepository extends GenericTokenRepository<UserToken> { } public interface TeacherTokenRepository extends GenericTokenRepository<TeacherToken> { }
import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; import javax.persistence.*; import java.time.Instant; import java.util.HashSet; import java.util.List; import java.util.Objects; import java.util.Set; @Getter @Setter @EqualsAndHashCode(of = {"id"}, callSuper = true) @MappedSuperclass @EntityListeners(AuditingEntityListener.class) public abstract class GenericToken { @Id @Override @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private Instant createdAt; private Instant updatedAt; private Integer version; private Long refreshTokenId; private String serviceName; private String token; private Long expiresIn; private Instant deactivatedAt; private Long userId; private String uuid; }
@Getter @Setter @Entity @Table(name = "tpuser_tokens") @AttributeOverride(name = "userId", column = @Column(name = "teacher_id")) public class TeacherToken extends GenericToken{ } @Getter @Setter @Entity @Table(name = "user_tokens") public class UserToken extends GenericToken { }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
公共GenericTokenRepository
GenericTokenRepository的两个子类
GenericToken父类实体
TeacherToken 和 UserToken 代表两个表,其中teacher的字段有一个不一样
使用的时候,不同的业务逻辑调用各自的Repository 即可
The text was updated successfully, but these errors were encountered: