Skip to content
New issue

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

entityName动态entity的使用例子 #41

Open
zhangzhenhuajack opened this issue Dec 10, 2021 · 0 comments
Open

entityName动态entity的使用例子 #41

zhangzhenhuajack opened this issue Dec 10, 2021 · 0 comments

Comments

@zhangzhenhuajack
Copy link
Owner

zhangzhenhuajack commented Dec 10, 2021

公共GenericTokenRepository

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);

}

GenericTokenRepository的两个子类

public interface UserTokenRepository extends GenericTokenRepository<UserToken> {
}
public interface TeacherTokenRepository extends GenericTokenRepository<TeacherToken> {
}

GenericToken父类实体

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;
}

TeacherToken 和 UserToken 代表两个表,其中teacher的字段有一个不一样

@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 {
}

使用的时候,不同的业务逻辑调用各自的Repository 即可

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant