diff --git a/opsli-core/src/main/java/org/opsli/core/base/concroller/BaseRestController.java b/opsli-core/src/main/java/org/opsli/core/base/concroller/BaseRestController.java new file mode 100644 index 00000000..6fd419ed --- /dev/null +++ b/opsli-core/src/main/java/org/opsli/core/base/concroller/BaseRestController.java @@ -0,0 +1,131 @@ +package org.opsli.core.base.concroller; + + +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.opsli.common.annotation.EnableHotData; +import org.opsli.common.exception.ServiceException; +import org.opsli.common.msg.CommonMsg; +import org.opsli.core.base.entity.BaseEntity; +import org.opsli.core.base.service.interfaces.CrudServiceInterface; +import org.opsli.core.cache.local.CacheUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.PostConstruct; +import java.lang.reflect.*; + +/** + * @BelongsProject: opsli-boot + * @BelongsPackage: org.opsli.common.base.concroller + * @Author: Parker + * @CreateTime: 2020-09-13 21:16 + * @Description: Controller 基类 + * + * 默认 范型引用 子类的Service , 为简单的CRUD做足准备 + * + */ +@Slf4j +@RestController +public abstract class BaseRestController >{ + + /** 开启热点数据状态 */ + protected boolean hotDataFlag = false; + + /** Entity Clazz 类 */ + protected Class entityClazz; + + @Autowired(required = false) + protected S IService; + + + /** + * 默认 直接设置 传入数据的 + * 根据id 从缓存 直接查询 数据对象 + * + * @param id + * @return + */ + @ModelAttribute + public T get(@RequestParam(required=false) String id) { + T entity = null; + if (StringUtils.isNotBlank(id)){ + // 如果开启缓存 先从缓存读 + if(hotDataFlag){ + entity = CacheUtil.get(id, entityClazz); + if(entity != null){ + return entity; + } + } + // 如果缓存没读到 则去数据库读 + entity = IService.get(id); + if(entity != null){ + // 如果开启缓存 将数据库查询对象 存如缓存 + if(hotDataFlag){ + CacheUtil.put(id, entity); + } + } + } + if (entity == null){ + try { + // 创建泛型对象 + entity = this.createModel(); + }catch (Exception e){ + log.error(CommonMsg.EXCEPTION_CONTROLLER_MODEL.toString()+" : {}",e.getMessage()); + throw new ServiceException(CommonMsg.EXCEPTION_CONTROLLER_MODEL); + } + } + return entity; + } + + + + + + // ================================================= + + @PostConstruct + public void init(){ + try { + this.entityClazz = this.getModelClass(); + Class serviceClazz = IService.getServiceClazz(); + // 判断是否开启热点数据 + if(serviceClazz != null){ + EnableHotData annotation = serviceClazz.getAnnotation(EnableHotData.class); + if(annotation != null){ + this.hotDataFlag = true; + } + } + + }catch (Exception e){ + log.error(e.getMessage(),e); + } + } + + + /** + * 创建泛型对象 + * @return + */ + private T createModel() { + try { + Class modelClazz = this.entityClazz; + return modelClazz.newInstance(); + } catch (Exception e) { + log.error(e.getMessage(),e); + } + return null; + } + + /** + * 获得 泛型 Clazz + * @return + */ + private Class getModelClass(){ + Class tClass = (Class)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0]; + return tClass; + } + +} diff --git a/opsli-common/src/main/java/org/opsli/common/base/entity/BaseEntity.java b/opsli-core/src/main/java/org/opsli/core/base/entity/BaseEntity.java similarity index 93% rename from opsli-common/src/main/java/org/opsli/common/base/entity/BaseEntity.java rename to opsli-core/src/main/java/org/opsli/core/base/entity/BaseEntity.java index 1ee3dd80..ad944756 100644 --- a/opsli-common/src/main/java/org/opsli/common/base/entity/BaseEntity.java +++ b/opsli-core/src/main/java/org/opsli/core/base/entity/BaseEntity.java @@ -1,4 +1,4 @@ -package org.opsli.common.base.entity; +package org.opsli.core.base.entity; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; @@ -6,6 +6,7 @@ import com.fasterxml.jackson.annotation.JsonFormat; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import lombok.EqualsAndHashCode; +import lombok.experimental.Accessors; import org.springframework.format.annotation.DateTimeFormat; import java.io.Serializable; @@ -20,6 +21,7 @@ import java.util.Date; * */ @Data +@Accessors(chain = true) @EqualsAndHashCode(callSuper = false) public abstract class BaseEntity implements Serializable { diff --git a/opsli-core/src/main/java/org/opsli/core/base/service/base/BaseService.java b/opsli-core/src/main/java/org/opsli/core/base/service/base/BaseService.java new file mode 100644 index 00000000..7bae81a0 --- /dev/null +++ b/opsli-core/src/main/java/org/opsli/core/base/service/base/BaseService.java @@ -0,0 +1,22 @@ +package org.opsli.core.base.service.base; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import org.opsli.core.base.entity.BaseEntity; +import org.opsli.core.base.service.interfaces.BaseServiceInterface; + +/** + * @BelongsProject: opsli-boot + * @BelongsPackage: org.opsli.common.base.service + * @Author: Parker + * @CreateTime: 2020-09-17 12:33 + * @Description: Opsli Base Service + */ +public abstract class BaseService , T extends BaseEntity> + extends ServiceImpl implements BaseServiceInterface { + + @Override + public Class getServiceClazz() { + return this.getClass(); + } +} diff --git a/opsli-core/src/main/java/org/opsli/core/base/service/impl/CrudServiceImpl.java b/opsli-core/src/main/java/org/opsli/core/base/service/impl/CrudServiceImpl.java new file mode 100644 index 00000000..bfecbccf --- /dev/null +++ b/opsli-core/src/main/java/org/opsli/core/base/service/impl/CrudServiceImpl.java @@ -0,0 +1,122 @@ +package org.opsli.core.base.service.impl; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.google.common.collect.Lists; +import lombok.extern.slf4j.Slf4j; +import org.opsli.core.base.entity.BaseEntity; +import org.opsli.core.base.service.base.BaseService; +import org.opsli.core.base.service.interfaces.CrudServiceInterface; + +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +/** + * @BelongsProject: opsli-boot + * @BelongsPackage: org.opsli.common.base.service.impl + * @Author: Parker + * @CreateTime: 2020-09-14 17:31 + * @Description: CurdServiceImpl 基类 - 实现类 + * + * 这里 可能觉得 增改 最次返回的也是实体对象 设计的有问题 + * + * 其实是为了 热点数据 切面用的 用来 增加或者清理数据 + * + * 没有 直接用一个save类的原因,可能是觉得 新增和修改分开一些会比较好 防止串了数据 + * + */ +@Slf4j +public abstract class CrudServiceImpl, T extends BaseEntity> + extends BaseService implements CrudServiceInterface { + + /** entity Class 类 */ + protected Class entityClazz; + + + @Override + public T get(String id) { + return baseMapper.selectById(id); + } + + @Override + public T get(T entity) { + if(entity == null) return null; + return baseMapper.selectById(entity.getId()); + } + + @Override + public List findList(T entity) { + //baseMapper.selectList() + return null; + } + + @Override + public T insert(T entity) { + if(entity == null) return null; + int count = baseMapper.insert(entity); + if(count > 0){ + return entity; + } + return null; + } + + @Override + public T update(T entity) { + if(entity == null) return null; + int count = baseMapper.updateById(entity); + if(count > 0){ + return entity; + } + return null; + } + + @Override + public int delete(String id) { + return baseMapper.deleteById(id); + } + + @Override + public int delete(T entity) { + if(entity == null) return 0; + return baseMapper.deleteById(entity.getId()); + } + + @Override + public int deleteAll(String[] ids) { + if(ids == null) return 0; + List idList = Arrays.asList(ids); + return baseMapper.deleteBatchIds(idList); + } + + @Override + public int deleteAll(Collection entitys) { + if(entitys == null || entitys.isEmpty()) return 0; + List idList = Lists.newArrayListWithCapacity(entitys.size()); + for (T entity : entitys) { + idList.add(entity.getId()); + } + return baseMapper.deleteBatchIds(idList); + } + + @Override + public int deleteByLogic(String id) { + return 0; + } + + @Override + public int deleteByLogic(T entity) { + return 0; + } + + @Override + public int deleteAllByLogic(String[] ids) { + return 0; + } + + @Override + public int deleteAllByLogic(Collection entities) { + return 0; + } + + +} diff --git a/opsli-core/src/main/java/org/opsli/core/base/service/interfaces/BaseServiceInterface.java b/opsli-core/src/main/java/org/opsli/core/base/service/interfaces/BaseServiceInterface.java new file mode 100644 index 00000000..e208f9f5 --- /dev/null +++ b/opsli-core/src/main/java/org/opsli/core/base/service/interfaces/BaseServiceInterface.java @@ -0,0 +1,20 @@ +package org.opsli.core.base.service.interfaces; + +import com.baomidou.mybatisplus.extension.service.IService; + +/** + * @BelongsProject: opsli-boot + * @BelongsPackage: org.opsli.common.base.service + * @Author: Parker + * @CreateTime: 2020-09-17 12:33 + * @Description: Opsli BaseServiceInterface + */ +public interface BaseServiceInterface extends IService { + + /** + * 获得当前Service Clazz + * @return + */ + Class getServiceClazz(); + +} diff --git a/opsli-core/src/main/java/org/opsli/core/base/service/interfaces/CrudServiceInterface.java b/opsli-core/src/main/java/org/opsli/core/base/service/interfaces/CrudServiceInterface.java new file mode 100644 index 00000000..e645084e --- /dev/null +++ b/opsli-core/src/main/java/org/opsli/core/base/service/interfaces/CrudServiceInterface.java @@ -0,0 +1,136 @@ +package org.opsli.core.base.service.interfaces; + + + +import java.util.Collection; +import java.util.List; + +public interface CrudServiceInterface extends BaseServiceInterface { + + + /** + * 获取单条数据 + * + * @param id + * @return + */ + T get(String id); + + /** + * 获取单条数据 + * + * @param entity + * @return + */ + T get(T entity); + + + /** + * 查询数据列表 + * + * @param entity + * @return + */ + List findList(T entity); + + + /** + * 插入数据 + * + * @param entity + * @return + */ + T insert(T entity); + + /** + * 更新数据 + * + * @param entity + * @return + */ + T update(T entity); + + + /** + * 删除数据(物理删除,从数据库中彻底删除) + * + * @param id + * @return + * @see int delete(T entity) + */ + int delete(String id); + + + /** + * 删除数据(物理删除,从数据库中彻底删除) + * + * @param entity + * @return + */ + int delete(T entity); + + + /** + * 批量物理删除 + * @param ids + * @return + */ + int deleteAll(String[] ids); + + /** + * 批量物理删除 + * @param entitys + * @return + */ + int deleteAll(Collection entitys); + + + /** + * 删除数据(逻辑删除,更新del_flag字段为1,在表包含字段del_flag时,可以调用此方法,将数据隐藏) + * + * @param id + * @return + * @see int delete(T entity) + */ + int deleteByLogic(String id); + + + /** + * 删除数据(逻辑删除,更新del_flag字段为1,在表包含字段del_flag时,可以调用此方法,将数据隐藏) + * + * @param entity + * @return + * @see int delete(T entity) + */ + int deleteByLogic(T entity); + + + /** + * 批量逻辑删除 + * @param ids + * @return + */ + int deleteAllByLogic(String[] ids); + + /** + * 批量逻辑删除 + * @param entitys + * @return + */ + int deleteAllByLogic(Collection entitys); + + /** + * 查询所有数据列表 + * + * @return + * @see List findAllList(T entity) + */ + //Page findPage(Page page, T t); + + +} + + + + +