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

com.baomidou.mybatisplus.extension.service.impl.ServiceImpl Maven / Gradle / Ivy

There is a newer version: 3.5.7
Show newest version
/*
 * Copyright (c) 2011-2016, hubin ([email protected]).
 * 

* 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.extension.service.impl; import java.io.Serializable; import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.Map; import org.apache.ibatis.binding.MapperMethod; import org.apache.ibatis.session.SqlSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.enums.SqlMethod; import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.TableInfo; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; import com.baomidou.mybatisplus.core.toolkit.ReflectionKit; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.core.toolkit.TableInfoHelper; import com.baomidou.mybatisplus.core.toolkit.sql.SqlHelper; import com.baomidou.mybatisplus.extension.service.IService; /** *

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

* * @author hubin * @since 2018-06-23 */ public class ServiceImpl, T> implements IService { @Autowired protected M baseMapper; /** *

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

*

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

* * @param result 数据库操作返回影响条数 * @return boolean */ protected static boolean retBool(Integer result) { return SqlHelper.retBool(result); } protected Class currentModelClass() { return ReflectionKit.getSuperClassGenricType(getClass(), 1); } /** *

* 批量操作 SqlSession *

*/ protected SqlSession sqlSessionBatch() { return SqlHelper.sqlSessionBatch(currentModelClass()); } /** * 获取SqlStatement * * @param sqlMethod * @return */ protected String sqlStatement(SqlMethod sqlMethod) { return SqlHelper.table(currentModelClass()).getSqlStatement(sqlMethod.getMethod()); } @Override public boolean save(T entity) { return ServiceImpl.retBool(baseMapper.insert(entity)); } @Transactional(rollbackFor = Exception.class) @Override public boolean saveBatch(Collection entityList) { return saveBatch(entityList, 30); } /** * 批量插入 * * @param entityList * @param batchSize * @return */ @Transactional(rollbackFor = Exception.class) @Override public boolean saveBatch(Collection entityList, int batchSize) { if (CollectionUtils.isEmpty(entityList)) { throw new IllegalArgumentException("Error: entityList must not be empty"); } try (SqlSession batchSqlSession = sqlSessionBatch()) { int i = 0; String sqlStatement = sqlStatement(SqlMethod.INSERT_ONE); Iterator iterator = entityList.iterator(); while (iterator.hasNext()) { batchSqlSession.insert(sqlStatement, iterator.next()); if (i >= 1 && i % batchSize == 0) { batchSqlSession.flushStatements(); } i++; } batchSqlSession.flushStatements(); } catch (Throwable e) { throw new MybatisPlusException("Error: Cannot execute saveBatch Method. Cause", e); } return true; } /** *

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

* * @param entity 实体对象 * @return boolean */ @Override public boolean saveOrUpdate(T entity) { if (null != entity) { Class cls = entity.getClass(); TableInfo tableInfo = TableInfoHelper.getTableInfo(cls); if (null != tableInfo && StringUtils.isNotEmpty(tableInfo.getKeyProperty())) { Object idVal = ReflectionKit.getMethodValue(cls, entity, tableInfo.getKeyProperty()); if (StringUtils.checkValNull(idVal)) { return save(entity); } else { /* * 更新成功直接返回,失败执行插入逻辑 */ return updateById(entity) || save(entity); } } else { throw new MybatisPlusException("Error: Can not execute. Could not find @TableId."); } } return false; } @Transactional(rollbackFor = Exception.class) @Override public boolean saveOrUpdateBatch(Collection entityList) { return saveOrUpdateBatch(entityList, 30); } @Transactional(rollbackFor = Exception.class) @Override public boolean saveOrUpdateBatch(Collection entityList, int batchSize) { if (CollectionUtils.isEmpty(entityList)) { throw new IllegalArgumentException("Error: entityList must not be empty"); } try (SqlSession batchSqlSession = sqlSessionBatch()) { Iterator iterator = entityList.iterator(); while (iterator.hasNext()) { saveOrUpdate(iterator.next()); } batchSqlSession.flushStatements(); } catch (Throwable e) { throw new MybatisPlusException("Error: Cannot execute saveOrUpdateBatch Method. Cause", e); } return true; } @Override public boolean removeById(Serializable id) { return SqlHelper.delBool(baseMapper.deleteById(id)); } @Override public boolean removeByMap(Map columnMap) { if (ObjectUtils.isEmpty(columnMap)) { throw new MybatisPlusException("removeByMap columnMap is empty."); } return SqlHelper.delBool(baseMapper.deleteByMap(columnMap)); } @Override public boolean remove(Wrapper wrapper) { return SqlHelper.delBool(baseMapper.delete(wrapper)); } @Override public boolean removeByIds(Collection idList) { return SqlHelper.delBool(baseMapper.deleteBatchIds(idList)); } @Override public boolean updateById(T entity) { return ServiceImpl.retBool(baseMapper.updateById(entity)); } @Override public boolean update(T entity, Wrapper wrapper) { return ServiceImpl.retBool(baseMapper.update(entity, wrapper)); } @Transactional(rollbackFor = Exception.class) @Override public boolean updateBatchById(Collection entityList) { return updateBatchById(entityList, 30); } @Transactional(rollbackFor = Exception.class) @Override public boolean updateBatchById(Collection entityList, int batchSize) { if (CollectionUtils.isEmpty(entityList)) { throw new IllegalArgumentException("Error: entityList must not be empty"); } try (SqlSession batchSqlSession = sqlSessionBatch()) { int i = 0; String sqlStatement = sqlStatement(SqlMethod.UPDATE_BY_ID); Iterator iterator = entityList.iterator(); while (iterator.hasNext()) { MapperMethod.ParamMap param = new MapperMethod.ParamMap<>(); param.put("et", iterator.next()); batchSqlSession.update(sqlStatement, param); if (i >= 1 && i % batchSize == 0) { batchSqlSession.flushStatements(); } i++; } batchSqlSession.flushStatements(); } catch (Throwable e) { throw new MybatisPlusException("Error: Cannot execute updateBatchById Method. Cause", e); } return true; } @Override public T getById(Serializable id) { return baseMapper.selectById(id); } @Override public Collection listByIds(Collection idList) { return baseMapper.selectBatchIds(idList); } @Override public Collection listByMap(Map columnMap) { return baseMapper.selectByMap(columnMap); } @Override public T getOne(Wrapper wrapper) { return SqlHelper.getObject(baseMapper.selectList(wrapper)); } @Override public Map getMap(Wrapper wrapper) { return SqlHelper.getObject(baseMapper.selectMaps(wrapper)); } @Override public Object getObj(Wrapper wrapper) { return SqlHelper.getObject(baseMapper.selectObjs(wrapper)); } @Override public int count(Wrapper wrapper) { return SqlHelper.retCount(baseMapper.selectCount(wrapper)); } @Override public List list(Wrapper wrapper) { return baseMapper.selectList(wrapper); } @Override public IPage page(IPage page, Wrapper wrapper) { wrapper = (Wrapper) SqlHelper.fillWrapper(page, wrapper); return baseMapper.selectPage(page, wrapper); } @Override public List> listMaps(Wrapper wrapper) { return baseMapper.selectMaps(wrapper); } @Override public List listObjs(Wrapper wrapper) { return baseMapper.selectObjs(wrapper); } @Override public IPage> pageMaps(IPage page, Wrapper wrapper) { wrapper = (Wrapper) SqlHelper.fillWrapper(page, wrapper); return baseMapper.selectMapsPage(page, wrapper); } }