ServiceImpl.java 7.41 KB
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
/**
* Copyright (c) 2011-2016, hubin (jobob@qq.com).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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;

/**
* <p>
* IService 实现类( 泛型:M 是 mapper 对象,T 是实体 , PK 是主键泛型 )
* </p>
*
* @author hubin
* @Date 2016-04-20
*/
public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {

private static final Log logger = LogFactory.getLog(ServiceImpl.class);

@Autowired
protected M baseMapper;

@SuppressWarnings("unchecked")
protected Class<T> currentModleClass() {
return ReflectionKit.getSuperClassGenricType(getClass(), 1);
}

/**
* <p>
* 批量操作 SqlSession
* </p>
*/
protected SqlSession sqlSessionBatch() {
return SqlHelper.sqlSessionBatch(currentModleClass());
}

/**
* <p>
* 判断数据库操作是否成功
* </p>
*
* @param result
* 数据库操作返回影响条数
* @return boolean
*/
public boolean retBool(int result) {
return result >= 1;
}

/**
* <p>
* TableId 注解存在更新记录,否插入一条记录
* </p>
*
* @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<T> entityList) {
return insertBatch(entityList, 30);
}

public boolean insertOrUpdateBatch(List<T> entityList) {
return insertOrUpdateBatch(entityList, 30);
}

public boolean insertOrUpdateBatch(List<T> 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<T> 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<String, Object> columnMap) {
if (MapUtils.isEmpty(columnMap)) {
throw new MybatisPlusException("deleteByMap columnMap is empty.");
}
return retBool(baseMapper.deleteByMap(columnMap));
}

public boolean delete(Wrapper<T> wrapper) {
return retBool(baseMapper.delete(wrapper));
}

public boolean deleteBatchIds(List<? extends Serializable> idList) {
return retBool(baseMapper.deleteBatchIds(idList));
}

public boolean updateById(T entity) {
return retBool(baseMapper.updateById(entity));
}

public boolean update(T entity, Wrapper<T> wrapper) {
return retBool(baseMapper.update(entity, wrapper));
}

public boolean updateBatchById(List<T> 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<T> selectBatchIds(List<? extends Serializable> idList) {
return baseMapper.selectBatchIds(idList);
}

public List<T> selectByMap(Map<String, Object> columnMap) {
return baseMapper.selectByMap(columnMap);
}

public T selectOne(Wrapper<T> wrapper) {
List<T> 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<T> wrapper) {
return baseMapper.selectCount(wrapper);
}

public List<T> selectList(Wrapper<T> wrapper) {
return baseMapper.selectList(wrapper);
}

public Page<T> selectPage(Page<T> page) {
page.setRecords(baseMapper.selectPage(page, null));
return page;
}

public Page<T> selectPage(Page<T> page, Wrapper<T> wrapper) {
if (null != wrapper) {
wrapper.orderBy(page.getOrderByField(), page.isAsc());
}
page.setRecords(baseMapper.selectPage(page, wrapper));
return page;
}

}