1 package com.supwisdom.institute.backend.common.framework.repo;
3 import java.util.Calendar;
5 import java.util.Optional;
7 import org.springframework.data.domain.Page;
8 import org.springframework.data.domain.PageRequest;
9 import org.springframework.data.jpa.repository.JpaRepository;
10 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
11 import org.springframework.data.repository.NoRepositoryBean;
13 import com.supwisdom.institute.backend.common.core.transmit.user.UserContext;
14 import com.supwisdom.institute.backend.common.framework.entity.ABaseEntity;
15 import com.supwisdom.institute.backend.common.util.UUIDUtils;
18 public interface BaseJpaRepository<E extends ABaseEntity> extends JpaRepository<E, String>, JpaSpecificationExecutor<E> {
23 * 如果需要生成主键,需要由子类重写此方法根据需要的方式生成主键值。
24 * @param entity 要持久化的对象
26 public default String generateId() {
27 return UUIDUtils.create();
30 public default Page<E> selectPageList(boolean loadAll, int pageIndex, int pageSize, Map<String, Object> mapBean, Map<String, String> orderBy) {
34 pageSize = Integer.MAX_VALUE;
37 PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
39 Page<E> page = this.findAll(pageRequest);
44 public default E selectById(String id) {
47 Optional<E> entity = this.findById(id);
49 if (entity.isPresent()) {
52 } catch (RuntimeException e) {
53 System.out.println("RuntimeException:" + e.getMessage());
54 } catch (Exception e) {
55 System.out.println("Exception:" + e.getMessage());
61 public default E insert(E entity) {
63 if (entity.getId() == null || entity.getId().isEmpty()) {
64 entity.setId(generateId());
67 if (entity.getCompanyId() == null || entity.getCompanyId().isEmpty()) {
68 entity.setCompanyId("1");
71 if (entity.getDeleted() == null) {
72 entity.setDeleted(false);
74 if (entity.getAddAccount() == null) {
75 entity.setAddAccount(UserContext.getUsername());
77 if (entity.getAddTime() == null) {
78 entity.setAddTime(Calendar.getInstance().getTime());
81 E e = this.save(entity);
86 public default E update(E entity) {
88 if (entity.getEditAccount() == null) {
89 entity.setEditAccount(UserContext.getUsername());
91 if (entity.getEditTime() == null) {
92 entity.setEditTime(Calendar.getInstance().getTime());
95 E e = this.save(entity);
100 public default E remove(E entity) {
102 if (entity.getDeleted() == null) {
103 entity.setDeleted(true);
105 if (entity.getDeleteAccount() == null) {
106 entity.setDeleteAccount(UserContext.getUsername());
108 if (entity.getDeleteTime() == null) {
109 entity.setDeleteTime(Calendar.getInstance().getTime());
112 E e = this.save(entity);
117 public default void delete(String id) {