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);
43 if (pageIndex == -1) {
49 pageSize = Integer.MAX_VALUE;
52 Sort sort = convertToSort(orderBy);
54 PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
56 pageRequest = PageRequest.of(pageIndex, pageSize, sort);
59 Page<E> page = this.findAll(spec, pageRequest);
67 public default E selectById(String id) {
70 Optional<E> entity = this.findById(id);
72 if (entity.isPresent()) {
75 } catch (RuntimeException e) {
76 System.out.println("RuntimeException:" + e.getMessage());
77 } catch (Exception e) {
78 System.out.println("Exception:" + e.getMessage());
84 public default E insert(E entity) {
86 if (entity.getId() == null || entity.getId().isEmpty()) {
87 entity.setId(generateId());
90 if (entity.getCompanyId() == null || entity.getCompanyId().isEmpty()) {
91 entity.setCompanyId("1");
94 if (entity.getDeleted() == null) {
95 entity.setDeleted(false);
97 if (entity.getAddAccount() == null) {
98 entity.setAddAccount(UserContext.getUsername());
100 if (entity.getAddTime() == null) {
101 entity.setAddTime(Calendar.getInstance().getTime());
104 E e = this.save(entity);
109 public default E update(E entity) {
111 if (entity.getEditAccount() == null) {
112 entity.setEditAccount(UserContext.getUsername());
114 if (entity.getEditTime() == null) {
115 entity.setEditTime(Calendar.getInstance().getTime());
118 E e = this.save(entity);
123 public default E remove(E entity) {
125 if (entity.getDeleted() == null) {
126 entity.setDeleted(true);
128 if (entity.getDeleteAccount() == null) {
129 entity.setDeleteAccount(UserContext.getUsername());
131 if (entity.getDeleteTime() == null) {
132 entity.setDeleteTime(Calendar.getInstance().getTime());
135 E e = this.save(entity);
140 public default void delete(String id) {