/** * Copyright (c) 2018 人人开源 All rights reserved. * * https://www.renren.io * * 版权所有,侵权必究! */ package com.iailab.framework.common.service.impl; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.enums.SqlMethod; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.OrderItem; import com.baomidou.mybatisplus.core.toolkit.Constants; import com.baomidou.mybatisplus.core.toolkit.ReflectionKit; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.toolkit.SqlHelper; import com.iailab.framework.common.constant.Constant; import com.iailab.framework.common.page.PageData; import com.iailab.framework.common.service.BaseService; import com.iailab.framework.common.util.object.BeanUtils; import org.apache.ibatis.binding.MapperMethod; import org.apache.ibatis.logging.Log; import org.apache.ibatis.logging.LogFactory; import org.apache.ibatis.session.SqlSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; import java.io.Serializable; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.function.BiConsumer; /** * 基础服务类,所有Service都要继承 * * @author Mark sunlightcs@gmail.com */ public abstract class BaseServiceImpl, T> implements BaseService { @Autowired protected M baseDao; protected Log log = LogFactory.getLog(getClass()); /** * 获取分页对象 * @param params 分页查询参数 * @param defaultOrderField 默认排序字段 * @param isAsc 排序方式 */ protected IPage getPage(Map params, String defaultOrderField, boolean isAsc) { //分页参数 long curPage = 1; long limit = 10; if(params.get(Constant.PAGE) != null){ curPage = Long.parseLong((String)params.get(Constant.PAGE)); } if(params.get(Constant.LIMIT) != null){ limit = Long.parseLong((String)params.get(Constant.LIMIT)); } //分页对象 Page page = new Page<>(curPage, limit); //分页参数 params.put(Constant.PAGE, page); //排序字段 String orderField = (String)params.get(Constant.ORDER_FIELD); String order = (String)params.get(Constant.ORDER); //前端字段排序 if(StringUtils.isNotBlank(orderField) && StringUtils.isNotBlank(order)){ if(Constant.ASC.equalsIgnoreCase(order)) { return page.addOrder(OrderItem.asc(orderField)); }else { return page.addOrder(OrderItem.desc(orderField)); } } //没有排序字段,则不排序 if(StringUtils.isBlank(defaultOrderField)){ return page; } //默认排序 if(isAsc) { page.addOrder(OrderItem.asc(defaultOrderField)); }else { page.addOrder(OrderItem.desc(defaultOrderField)); } return page; } protected PageData getPageData(List list, long total, Class target){ List targetList = BeanUtils.toBean(list, target); return new PageData<>(targetList, total); } protected PageData getPageData(IPage page, Class target){ return getPageData(page.getRecords(), page.getTotal(), target); } protected void paramsToLike(Map params, String... likes){ for (String like : likes){ String val = (String)params.get(like); if (StringUtils.isNotBlank(val)){ params.put(like, "%" + val + "%"); }else { params.put(like, null); } } } /** *

* 判断数据库操作是否成功 *

*

* 注意!! 该方法为 Integer 判断,不可传入 int 基本类型 *

* * @param result 数据库操作返回影响条数 * @return boolean */ protected static boolean retBool(Integer result) { return SqlHelper.retBool(result); } protected Class currentMapperClass() { return (Class) ReflectionKit.getSuperClassGenericType(this.getClass(), BaseServiceImpl.class, 0); } @Override public Class currentModelClass() { return (Class)ReflectionKit.getSuperClassGenericType(this.getClass(), BaseServiceImpl.class, 1); } protected String getSqlStatement(SqlMethod sqlMethod) { return SqlHelper.getSqlStatement(this.currentMapperClass(), sqlMethod); } @Override public boolean insert(T entity) { return BaseServiceImpl.retBool(baseDao.insert(entity)); } @Override @Transactional(rollbackFor = Exception.class) public boolean insertBatch(Collection entityList) { return insertBatch(entityList, 100); } /** * 批量插入 */ @Override @Transactional(rollbackFor = Exception.class) public boolean insertBatch(Collection entityList, int batchSize) { String sqlStatement = getSqlStatement(SqlMethod.INSERT_ONE); return executeBatch(entityList, batchSize, (sqlSession, entity) -> sqlSession.insert(sqlStatement, entity)); } /** * 执行批量操作 */ protected boolean executeBatch(Collection list, int batchSize, BiConsumer consumer) { return SqlHelper.executeBatch(this.currentModelClass(), this.log, list, batchSize, consumer); } @Override public boolean updateById(T entity) { return BaseServiceImpl.retBool(baseDao.updateById(entity)); } @Override public boolean update(T entity, Wrapper updateWrapper) { return BaseServiceImpl.retBool(baseDao.update(entity, updateWrapper)); } @Override @Transactional(rollbackFor = Exception.class) public boolean updateBatchById(Collection entityList) { return updateBatchById(entityList, 30); } @Override @Transactional(rollbackFor = Exception.class) public boolean updateBatchById(Collection entityList, int batchSize) { String sqlStatement = getSqlStatement(SqlMethod.UPDATE_BY_ID); return executeBatch(entityList, batchSize, (sqlSession, entity) -> { MapperMethod.ParamMap param = new MapperMethod.ParamMap<>(); param.put(Constants.ENTITY, entity); sqlSession.update(sqlStatement, param); }); } @Override public T selectById(Serializable id) { return baseDao.selectById(id); } @Override public boolean deleteById(Serializable id) { return SqlHelper.retBool(baseDao.deleteById(id)); } @Override public boolean deleteBatchIds(Collection idList) { return SqlHelper.retBool(baseDao.deleteBatchIds(idList)); } }