java template 0.2

/*
java service 服务接口
 */
package tx.txservice.service;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.github.pagehelper.PageInfo;

import java.util.List;

public interface ITemplateService<TDTO, TVO, TQUERY> extends IService<TDTO> {
 default Long getTotal(List<?> pList)
  /*****
   * Author:狂客
   * QQ:214109721
   * @Description:获取信息总数
   * @param pList 集合
   * @return:信息总数
   *****/
  {
    return new PageInfo(pList).getTotal();
  }
  
  //前端更多
  public void setFrontMore();

  //后端更多
  public void setBgMore();

  //格式化查询对象返回QueryWrapper
  public QueryWrapper<TDTO> getQueryWrapper(TQUERY pQuery);
 
 //分页查询列表
 public List<TDTO> getListPage(TQUERY pQuery);
  
  //查询列表
  public List<TDTO> getList(QueryWrapper<TDTO> queryWrapper);

  //查询列表
  public List<TDTO> getList(TQUERY pQuery);

  //DTO格式化
  public TDTO formatDTO(TDTO pItem);

  public default List<TDTO> formatDTO(List<TDTO> pList)
  /*****
   * Author:狂客
   * QQ:214109721
   * @Description:列表DTO格式化
   * @param pList 集合
   * @return:无
   *****/
  {
    pList.forEach(vItem -> {
      formatDTO(vItem);
    });
    return pList;
  }

  //列表格式化
  public List<TVO> formatVO(List<TDTO> pList, Class<TVO> pClass);

  //单条VO格式化
  public default TVO formatVO(TDTO pItem, Class<TVO> pClass) {
    return formatVO(Convert.convert(pClass, pItem));
  }

  //单条VO格式化
  public TVO formatVO(TVO pItem);

  //查询单条数据
  public TDTO getOne(TQUERY pQuery);

  //查询单条数据
  public TDTO getOne(QueryWrapper<TDTO> pQuery);

  //根据ID查询单条数据
  public TDTO getOneById(Long pId);

  //id解密
  public Long idDecode(String pSid);

  //id加密
  public String idEncode(Long pId);

  //删除
  public boolean delete(TQUERY pQuery);

  //编辑
  public boolean edit(TQUERY pQuery);

  //编辑
  public boolean edit(TDTO pItem, TQUERY pQuery);

  //添加
  public boolean add(TDTO pItem);
}
/*
template service impl 服务实现类
*/
package tx.txservice.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.crypto.symmetric.AES;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import tx.txservice.service.TemplateService;

import java.util.List;

@Data
public class TemplateServiceImpl<TMAPPER extends BaseMapper<TDTO>, TDTO, TVO, TQUERY  extends BaseQuery> extends ServiceImpl<TMAPPER, TDTO> implements TemplateService<TDTO, TVO, TQUERY> {
 //前端更多
  protected Boolean frontMore = false;
  //后端更多
  protected Boolean bgMore = false;
  //加密类
  @Autowired
  protected AES encodeAES;
 
 @Override
  public void setFrontMore() {
    frontMore = true;
    return;
  }

  @Override
  public void setBgMore() {
    bgMore = true;
    return;
  }
  
  @Override
  public List<TVO> formatVO(List<TDTO> pList, Class<TVO> pClass)
  /*****
   * Author:狂客
   * QQ:214109721
   * @Description:列表VO格式化
   * @param pList 集合
   * @param pClass VO class
   * @return:无
   *****/
  {
    List<TVO> vList = Convert.toList(pClass, pList);
    Boolean vFrontMore = frontMore;
    Boolean vBgMore = bgMore;
    vList.forEach(vItem -> {
      frontMore = vFrontMore;
      bgMore = vBgMore;
      formatVO(vItem);
    });
    return vList;
  }

  @Override
  public List<TDTO> getList(QueryWrapper<TDTO> queryWrapper) {
    //throw new RuntimeException("getList 2 未初始化");
    List<TDTO> vList = list(queryWrapper);
    for (TDTO vItem : vList) {
      formatDTO(vItem);
    }
    return vList;
  }
  
  @Override
  public List<TDTO> getListPage(TQUERY pQuery){
    //开启分页
    PageDomain pageDomain = TableSupport.buildPageRequest();
    Integer pageNum = pageDomain.getPageNum();
    Integer pageSize = pageDomain.getPageSize();
    Boolean reasonable = pageDomain.getReasonable();
    PageHelper.startPage(pageNum, pageSize, null).setReasonable(reasonable);
    List<TDTO> vList = getList(getQueryWrapper(pQuery));
    //清理分页
    PageHelper.clearPage();
    return vList;
  }

  @Override
  public List<TDTO> getList(TQUERY pQuery) {
    //throw new RuntimeException("getList 1 未初始化");
    return getList(getQueryWrapper(pQuery));
  }

  /**
   * 需要自己实现功能Begin
   **/

  //格式化查询对象返回QueryWrapper
 @Override
  public QueryWrapper<TDTO> getQueryWrapper(TQUERY pQuery) {
    QueryWrapper<TDTO> vQueryWrap = new QueryWrapper<>();
    if (ObjectUtil.isNotNull(pQuery.getFields())) {
      vQueryWrap.select(pQuery.getFields().toArray(new String[pQuery.getFields().size()]));
    }
    if (ObjectUtil.isNotNull(pQuery.getIgnoreDel())) {
      vQueryWrap.eq("is_del", 0);
    }
    return vQueryWrap;
  }

  //DTO格式化
  @Override
  public TDTO formatDTO(TDTO pItem) {
    if (ObjectUtil.isNull(pItem)) {
      return pItem;
    }
    throw new RuntimeException("formatDTO 未初始化");
    //return pItem;
  }

  @Override
  public TVO formatVO(TVO pItem) {
    if (ObjectUtil.isNull(pItem)) {
      return pItem;
    }

    frontMore = false; //必须要写到自己代码formatVO最后里
    bgMore = false;//必须要写到自己代码formatVO最后里
    throw new RuntimeException("formatVO 未初始化");
    //return null;
  }

  @Override
  public boolean delete(TQUERY pQuery) {
    return remove(getQueryWrapper(pQuery));
  }

  @Override
  public boolean edit(TQUERY pQuery) {
    Boolean vRet = update(getQueryWrapper(pQuery));
    if (!vRet) {
      throw new ServiceException("操作失败,请重试");
    }
    return vRet;
  }

  @Override
  public boolean edit(TDTO pItem, TQUERY pQuery) {
    Boolean vRet = update(pItem, getQueryWrapper(pQuery));
    if (!vRet) {
      throw new ServiceException("操作失败,请重试");
    }
    return vRet;
  }

  @Override
  public boolean add(TDTO pItem) {
    Boolean vRet = save(pItem);
    if (!vRet) {
      throw new ServiceException("操作失败,请重试");
    }
    return vRet;
  }

  /**
   * 需要自己实现功能End
   **/

  @Override
  public TDTO getOneById(Long pId) {
    /**
     建议这里通过 TQUERY 里设置个primaryId 来作为筛选条件 统一调用 getOne(QueryWrapper<TDTO> pQuery)
     TQUERY vQuery = new TQUERY();
     vQuery.setPrimaryId(1);
     return getOne(vQuery);
     **/
    return formatDTO(getById(pId));
  }

  @Override
  public TDTO getOne(TQUERY pQuery) {
    return getOne(getQueryWrapper(pQuery));
  }

  @Override
  public TDTO getOne(QueryWrapper<TDTO> pQuery) {
    return formatDTO(super.getOne(pQuery));
  }

  @Override
  public Long idDecode(String pSid) {
    Long vId = new Long(encodeAES.decryptStr(pSid));
    return vId;
  }

  @Override
  public String idEncode(Long pId) {
    return encodeAES.encryptHex(Convert.toStr(pId));
  }
}
/*
接口继承
*/
package tx.txservice.service;

public interface CpnCdKeyService extends TemplateService<CdKeyDTO, CdKeyVO, CdKeyDTO.CQuery> {

}

/*
实现类继承
*/
package tx.txservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tx.txservice.domain.KuangKeDTO;
import tx.txservice.domain.vo.KuangKeVO;
import tx.txservice.mapper.KuangKeMapper;
import tx.txservice.service.KuangKeService;
import tx.txservice.service.KuangKeQuery;

import java.util.List;

@Service
public class CpnCdKeyImpl extends TemplateServiceImpl<CpnCdKeyMapper, CdKeyDTO, CdKeyVO, , CdKeyDTO.CQuery> implements CpnCdKeyService {
  //格式化查询对象返回QueryWrapper
  @Override
  public QueryWrapper<CpnCdKeyDTO> getQueryWrapper(CpnCdKeyQuery pQuery) {
    //throw new RuntimeException("getQueryWrapper 未初始化");
    QueryWrapper<CpnCdKeyDTO> vQuery = super.getQueryWrapper(pQuery);
    return vQuery;
  }

  //DTO格式化
  @Override
  public CpnCdKeyDTO formatDTO(CpnCdKeyDTO pItem) {
    if (ObjectUtil.isNull(pItem)) {
      return pItem;
    }
    //throw new RuntimeException("formatDTO 未初始化");
    return pItem;
  }

  @Override
  public CpnCdKeyVO formatVO(CpnCdKeyVO pItem) {
    if (ObjectUtil.isNull(pItem)) {
      return pItem;
    }

    frontMore = false; //必须要写到自己代码formatVO最后里
    bgMore = false;//必须要写到自己代码formatVO最后里
    return pItem;
  }

}

//TQUERY基类
@Data
public class BaseQuery {
  /**
   * 创建实例
   *
   * @param pClass 对象类字节
   * @return 无
   */
  public static <T> T getInstance(Class<T> pClass) {
    try {
      return pClass.newInstance();
    } catch (InstantiationException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    }
    return null;
  }

  //主键Id
  protected Long primaryId;
  //忽略删除
  protected Boolean ignoreDel;
  //查询字段
  protected List<String> fields;
  //主键id集合
  protected List<Long> primaryIds;
  //主键sid集合
  protected List<String> primarySids;
}

全部都需要设置别名 避免bean重名. mapper domain service

posted @ 2022-10-23 11:02  狂客  阅读(19)  评论(0编辑  收藏  举报