/** * Copyright (c) 2011-2016, hubin (jobob@qq.com). *

* Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at *

* http://www.apache.org/licenses/LICENSE-2.0 *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package com.baomidou.mybatisplus.service.impl; import java.io.Serializable; import java.util.List; import java.util.Map; 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 com.baomidou.mybatisplus.entity.TableInfo; import com.baomidou.mybatisplus.enums.IdType; import com.baomidou.mybatisplus.exceptions.MybatisPlusException; import com.baomidou.mybatisplus.mapper.BaseMapper; import com.baomidou.mybatisplus.mapper.SqlHelper; import com.baomidou.mybatisplus.mapper.Wrapper; import com.baomidou.mybatisplus.plugins.Page; import com.baomidou.mybatisplus.service.IService; import com.baomidou.mybatisplus.toolkit.CollectionUtils; import com.baomidou.mybatisplus.toolkit.MapUtils; import com.baomidou.mybatisplus.toolkit.ReflectionKit; import com.baomidou.mybatisplus.toolkit.StringUtils; import com.baomidou.mybatisplus.toolkit.TableInfoHelper; /** *

* IService 实现类( 泛型:M 是 mapper 对象,T 是实体 , PK 是主键泛型 ) *

* * @author hubin * @Date 2016-04-20 */ public class ServiceImpl, T> implements IService { private static final Log logger = LogFactory.getLog(ServiceImpl.class); @Autowired protected M baseMapper; @SuppressWarnings("unchecked") protected Class currentModleClass() { return ReflectionKit.getSuperClassGenricType(getClass(), 1); } /** *

* 批量操作 SqlSession *

*/ protected SqlSession sqlSessionBatch() { return SqlHelper.sqlSessionBatch(currentModleClass()); } /** *

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

* * @param result * 数据库操作返回影响条数 * @return boolean */ public boolean retBool(int result) { return result >= 1; } /** *

* TableId 注解存在更新记录,否插入一条记录 *

* * @param entity * 实体对象 * @return boolean */ public boolean insertOrUpdate(T entity) { if (null != entity) { Class cls = entity.getClass(); TableInfo tableInfo = TableInfoHelper.getTableInfo(cls); if (null != tableInfo) { Object idVal = ReflectionKit.getMethodValue(cls, entity, tableInfo.getKeyProperty()); if (StringUtils.checkValNull(idVal)) { return insert(entity); } else { /* 特殊处理 INPUT 主键策略逻辑 */ if (IdType.INPUT == tableInfo.getIdType()) { T entityValue = selectById((Serializable) idVal); if (null != entityValue) { return updateById(entity); } else { return insert(entity); } } return updateById(entity); } } else { throw new MybatisPlusException("Error: Can not execute. Could not find @TableId."); } } return false; } public boolean insert(T entity) { return retBool(baseMapper.insert(entity)); } public boolean insertBatch(List entityList) { return insertBatch(entityList, 30); } public boolean insertOrUpdateBatch(List entityList) { return insertOrUpdateBatch(entityList, 30); } public boolean insertOrUpdateBatch(List entityList, int batchSize) { if (CollectionUtils.isEmpty(entityList)) { throw new IllegalArgumentException("Error: entityList must not be empty"); } try { SqlSession batchSqlSession = sqlSessionBatch(); int size = entityList.size(); for (int i = 0; i < size; i++) { insertOrUpdate(entityList.get(i)); if (i % batchSize == 0) { batchSqlSession.flushStatements(); } } batchSqlSession.flushStatements(); } catch (Exception e) { logger.warn("Error: Cannot execute insertOrUpdateBatch Method. Cause:" + e); return false; } return true; } /** * 批量插入 * * @param entityList * @param batchSize * @return */ public boolean insertBatch(List entityList, int batchSize) { if (CollectionUtils.isEmpty(entityList)) { throw new IllegalArgumentException("Error: entityList must not be empty"); } SqlSession batchSqlSession = sqlSessionBatch(); try { int size = entityList.size(); for (int i = 0; i < size; i++) { baseMapper.insert(entityList.get(i)); if (i % batchSize == 0) { batchSqlSession.flushStatements(); } } batchSqlSession.flushStatements(); } catch (Exception e) { logger.warn("Error: Cannot execute insertBatch Method. Cause:" + e); return false; } return true; } public boolean deleteById(Serializable id) { return retBool(baseMapper.deleteById(id)); } public boolean deleteByMap(Map columnMap) { if (MapUtils.isEmpty(columnMap)) { throw new MybatisPlusException("deleteByMap columnMap is empty."); } return retBool(baseMapper.deleteByMap(columnMap)); } public boolean delete(Wrapper wrapper) { return retBool(baseMapper.delete(wrapper)); } public boolean deleteBatchIds(List idList) { return retBool(baseMapper.deleteBatchIds(idList)); } public boolean updateById(T entity) { return retBool(baseMapper.updateById(entity)); } public boolean update(T entity, Wrapper wrapper) { return retBool(baseMapper.update(entity, wrapper)); } public boolean updateBatchById(List entityList) { if (CollectionUtils.isEmpty(entityList)) { throw new IllegalArgumentException("Error: entityList must not be empty"); } SqlSession batchSqlSession = sqlSessionBatch(); try { int size = entityList.size(); for (int i = 0; i < size; i++) { baseMapper.updateById(entityList.get(i)); if (i % 30 == 0) { batchSqlSession.flushStatements(); } } batchSqlSession.flushStatements(); } catch (Exception e) { logger.warn("Error: Cannot execute insertBatch Method. Cause:" + e); return false; } return true; } public T selectById(Serializable id) { return baseMapper.selectById(id); } public List selectBatchIds(List idList) { return baseMapper.selectBatchIds(idList); } public List selectByMap(Map columnMap) { return baseMapper.selectByMap(columnMap); } public T selectOne(Wrapper wrapper) { List list = baseMapper.selectList(wrapper); if (CollectionUtils.isNotEmpty(list)) { int size = list.size(); if (size > 1) { logger.warn(String.format("Warn: selectOne Method There are %s results.", size)); } return list.get(0); } return null; } public int selectCount(Wrapper wrapper) { return baseMapper.selectCount(wrapper); } public List selectList(Wrapper wrapper) { return baseMapper.selectList(wrapper); } public Page selectPage(Page page) { page.setRecords(baseMapper.selectPage(page, null)); return page; } public Page selectPage(Page page, Wrapper wrapper) { if (null != wrapper) { wrapper.orderBy(page.getOrderByField(), page.isAsc()); } page.setRecords(baseMapper.selectPage(page, wrapper)); return page; } }