完善Base模版

v1.4.1
Parker 5 years ago
parent ab3b990841
commit 1693c5ed56

@ -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 <T extends BaseEntity, S extends CrudServiceInterface<T>>{
/** 开启热点数据状态 */
protected boolean hotDataFlag = false;
/** Entity Clazz 类 */
protected Class<T> 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<T> modelClazz = this.entityClazz;
return modelClazz.newInstance();
} catch (Exception e) {
log.error(e.getMessage(),e);
}
return null;
}
/**
* Clazz
* @return
*/
private Class<T> getModelClass(){
Class<T> tClass = (Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
return tClass;
}
}

@ -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.IdType;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
@ -6,6 +6,7 @@ import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.springframework.format.annotation.DateTimeFormat; import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable; import java.io.Serializable;
@ -20,6 +21,7 @@ import java.util.Date;
* *
*/ */
@Data @Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false) @EqualsAndHashCode(callSuper = false)
public abstract class BaseEntity implements Serializable { public abstract class BaseEntity implements Serializable {

@ -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 <M extends BaseMapper<T>, T extends BaseEntity>
extends ServiceImpl<M, T> implements BaseServiceInterface<T> {
@Override
public Class<?> getServiceClazz() {
return this.getClass();
}
}

@ -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<M extends BaseMapper<T>, T extends BaseEntity>
extends BaseService<M, T> implements CrudServiceInterface<T> {
/** entity Class 类 */
protected Class<T> 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<T> 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<String> idList = Arrays.asList(ids);
return baseMapper.deleteBatchIds(idList);
}
@Override
public int deleteAll(Collection<T> entitys) {
if(entitys == null || entitys.isEmpty()) return 0;
List<String> 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<T> entities) {
return 0;
}
}

@ -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<T> extends IService<T> {
/**
* Service Clazz
* @return
*/
Class<?> getServiceClazz();
}

@ -0,0 +1,136 @@
package org.opsli.core.base.service.interfaces;
import java.util.Collection;
import java.util.List;
public interface CrudServiceInterface<T> extends BaseServiceInterface<T> {
/**
*
*
* @param id
* @return
*/
T get(String id);
/**
*
*
* @param entity
* @return
*/
T get(T entity);
/**
*
*
* @param entity
* @return
*/
List<T> 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<T> entitys);
/**
* del_flag1,del_flag
*
* @param id
* @return
* @see int delete(T entity)
*/
int deleteByLogic(String id);
/**
* del_flag1,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<T> entitys);
/**
*
*
* @return
* @see List<T> findAllList(T entity)
*/
//Page findPage(Page page, T t);
}
Loading…
Cancel
Save