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.domain.Sort;
10 import org.springframework.data.jpa.domain.Specification;
11 import org.springframework.data.jpa.repository.JpaRepository;
12 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
13 import org.springframework.data.repository.NoRepositoryBean;
15 import com.supwisdom.institute.backend.common.core.transmit.user.UserContext;
16 import com.supwisdom.institute.backend.common.framework.entity.ABaseEntity;
17 import com.supwisdom.institute.backend.common.util.UUIDUtils;
20 public interface BaseJpaRepository<E extends ABaseEntity> extends JpaRepository<E, String>, JpaSpecificationExecutor<E> {
25 * 如果需要生成主键,需要由子类重写此方法根据需要的方式生成主键值。
26 * @param entity 要持久化的对象
28 public default String generateId() {
29 return UUIDUtils.create();
32 public default Specification<E> convertToSpec(Map<String, Object> mapBean) {
35 public default Sort convertToSort(Map<String, String> orderBy) {
39 public default Page<E> selectPageList(boolean loadAll, int pageIndex, int pageSize, Map<String, Object> mapBean, Map<String, String> orderBy) {
41 Specification<E> spec = convertToSpec(mapBean);
45 pageSize = Integer.MAX_VALUE;
48 Sort sort = convertToSort(orderBy);
50 PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
52 pageRequest = PageRequest.of(pageIndex, pageSize, sort);
55 Page<E> page = this.findAll(spec, pageRequest);
63 public default E selectById(String id) {
66 Optional<E> entity = this.findById(id);
68 if (entity.isPresent()) {
71 } catch (RuntimeException e) {
72 System.out.println("RuntimeException:" + e.getMessage());
73 } catch (Exception e) {
74 System.out.println("Exception:" + e.getMessage());
80 public default E insert(E entity) {
82 if (entity.getId() == null || entity.getId().isEmpty()) {
83 entity.setId(generateId());
86 if (entity.getCompanyId() == null || entity.getCompanyId().isEmpty()) {
87 entity.setCompanyId("1");
90 if (entity.getDeleted() == null) {
91 entity.setDeleted(false);
93 if (entity.getAddAccount() == null) {
94 entity.setAddAccount(UserContext.getUsername());
96 if (entity.getAddTime() == null) {
97 entity.setAddTime(Calendar.getInstance().getTime());
100 E e = this.save(entity);
105 public default E update(E entity) {
107 if (entity.getEditAccount() == null) {
108 entity.setEditAccount(UserContext.getUsername());
110 if (entity.getEditTime() == null) {
111 entity.setEditTime(Calendar.getInstance().getTime());
114 E e = this.save(entity);
119 public default E remove(E entity) {
121 if (entity.getDeleted() == null) {
122 entity.setDeleted(true);
124 if (entity.getDeleteAccount() == null) {
125 entity.setDeleteAccount(UserContext.getUsername());
127 if (entity.getDeleteTime() == null) {
128 entity.setDeleteTime(Calendar.getInstance().getTime());
131 E e = this.save(entity);
136 public default void delete(String id) {