All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.freegeese.easymybatis.service.BaseService Maven / Gradle / Ivy

The newest version!
package com.github.freegeese.easymybatis.service;

import com.github.freegeese.easymybatis.domain.Pageable;
import com.github.freegeese.easymybatis.mapper.BaseMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Map;

/**
 * 基础Service
 *
 * 

通用基础服务,用于常用的增删改查方法 * 一般实体Service集成此类快速使用 * * @param 实体类型 * @param Mapper类型 * @author zhangguangyong * @since 1.0 */ public class BaseService> { @Autowired protected M mapper; protected final Class entityClass; public BaseService() { ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass(); this.entityClass = (Class) parameterizedType.getActualTypeArguments()[0]; } // 增 ================================================================================= public int insert(T entity) { return mapper.insert(entity); } public int insertSelective(T entity) { return mapper.insertSelective(entity); } public int insertBatch(List entities) { return mapper.insertBatch(entities); } public int insertBatchSelective(List entities) { return mapper.insertBatchSelective(entities); } public int insertBatch(List entities, int batchSize) { int size = entities.size(); int batchTimes = size / batchSize; int remaining = size % batchSize; int count = 0; for (int i = 0; i < batchTimes; i++) { count += mapper.insertBatch(entities.subList(i * batchSize, (i + 1) * batchSize)); } if (remaining > 0) { count += mapper.insertBatch(entities.subList(size - remaining, size)); } return count; } // 删 ================================================================================= public int deleteByPrimaryKey(Object id) { return mapper.deleteByPrimaryKey(id, entityClass); } public int deleteByPrimaryKeys(List ids) { return mapper.deleteByPrimaryKeys(ids, entityClass); } public int deleteByEntity(T record) { return mapper.deleteByEntity(record); } public int deleteByParameterMap(Map parameterMap) { return mapper.deleteByParameterMap(parameterMap, entityClass); } // 改 ================================================================================= public int updateByPrimaryKey(T entity) { return mapper.updateByPrimaryKey(entity); } public int updateByPrimaryKeySelective(T entity) { return mapper.updateByPrimaryKeySelective(entity); } public int updateBatchSelective(List entities) { return mapper.updateBatchSelective(entities); } public int updateBatchSelective(List entities, int batchSize) { int size = entities.size(); int batchTimes = size / batchSize; int remaining = size % batchSize; int count = 0; for (int i = 0; i < batchTimes; i++) { count += mapper.updateBatchSelective(entities.subList(i * batchSize, (i + 1) * batchSize)); } if (remaining > 0) { count += mapper.updateBatchSelective(entities.subList(size - remaining, size)); } return count; } // 查 ================================================================================= public T selectByPrimaryKey(Object id) { return mapper.selectByPrimaryKey(id, entityClass); } public List selectByPrimaryKeys(List ids) { return mapper.selectByPrimaryKeys(ids, entityClass); } public List selectByEntity(T entity) { return mapper.selectByEntity(entity); } public List selectByParameterMap(Map parameterMap) { return mapper.selectByParameterMap(parameterMap, entityClass); } public List selectAll() { return mapper.selectAll(entityClass); } // 分页 ================================================================================= public Pageable selectPage(Pageable page) { return copyPageInfo(page, PageHelper.offsetPage(page.getOffset(), page.getPageSize()).doSelectPageInfo(() -> mapper.selectAll(entityClass))); } public Pageable selectPageByEntity(Pageable page, T entity) { return copyPageInfo(page, PageHelper.offsetPage(page.getOffset(), page.getPageSize()).doSelectPageInfo(() -> mapper.selectByEntity(entity))); } public Pageable selectPageByParameterMap(Pageable page, Map parameterMap) { return copyPageInfo(page, PageHelper.offsetPage(page.getOffset(), page.getPageSize()).doSelectPageInfo(() -> mapper.selectByParameterMap(parameterMap, entityClass))); } private Pageable copyPageInfo(Pageable page, PageInfo pageInfo) { page.setTotalRecords(pageInfo.getTotal()); page.setContent(pageInfo.getList()); return page; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy