JpaService.java 17.8 KB
Newer Older
M
MaxKey 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright [2021] [MaxKey of copyright http://www.maxkey.top]
 * 
 * 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.
 */
 

M
MaxKey 已提交
18
package org.dromara.mybatis.jpa;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
19 20 21

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
M
MaxKey 已提交
22
import java.util.Collections;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
23
import java.util.List;
M
MaxKey 已提交
24 25

import org.apache.commons.collections.CollectionUtils;
M
MaxKey 已提交
26
import org.dromara.mybatis.jpa.entity.JpaEntity;
M
MaxKey 已提交
27
import org.dromara.mybatis.jpa.entity.JpaPage;
M
MaxKey 已提交
28
import org.dromara.mybatis.jpa.entity.JpaPageResults;
M
MaxKey 已提交
29
import org.dromara.mybatis.jpa.query.Query;
M
MaxKey 已提交
30
import org.dromara.mybatis.jpa.spring.MybatisJpaContext;
M
MaxKey 已提交
31 32 33
import org.dromara.mybatis.jpa.util.BeanUtil;
import org.dromara.mybatis.jpa.util.InstanceUtil;
import org.dromara.mybatis.jpa.util.StringUtils;
M
MaxKey 已提交
34 35
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
36

M
MaxKey 已提交
37 38
import com.fasterxml.jackson.annotation.JsonIgnore;

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
39 40

/**
M
update  
MaxKey 已提交
41
 * JPA Service
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
42 43 44 45
 * @author Crystal.Sea
 *
 * @param <T>
 */
M
rename  
MaxKey 已提交
46
public  class  JpaService <T extends JpaEntity> {
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
47
	
M
MaxKey 已提交
48
	private static final  Logger logger = LoggerFactory.getLogger(JpaService.class);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
49 50 51 52
	
	/**
	 * mapper class
	 */
M
MaxKey 已提交
53
	@JsonIgnore
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
54 55 56 57 58
	private String mapperClass = "";
	
	/**
	 * entity Class
	 */
M
MaxKey 已提交
59
	@JsonIgnore
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
60 61 62 63 64 65
	@SuppressWarnings("rawtypes")
	private Class entityClass;
	
	/**
	 * mapper 
	 */
M
MaxKey 已提交
66
	@JsonIgnore
M
rename  
MaxKey 已提交
67
	private IJpaMapper<T> mapper = null;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
68
	
M
rename  
MaxKey 已提交
69
	public JpaService() {}
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
70 71 72 73 74 75
	
	/**
	 * Load mapperClass by class type
	 * @param cls
	 */
	@SuppressWarnings("unchecked")
M
rename  
MaxKey 已提交
76
	public JpaService(@SuppressWarnings("rawtypes") Class cls) {
M
3.1  
MaxKey 已提交
77
		logger.trace("class name : {}" , cls.getSimpleName());
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
78 79 80 81 82
		mapperClass = cls.getSimpleName();
		Type[] pType = ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments();
		if (pType != null && pType.length >= 1) {
			this.entityClass = (Class<T>) pType[0];
		} else {
M
update  
MaxKey 已提交
83
			logger.error("invalide initail, need generic type parameter! ");
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
84 85
			throw new RuntimeException("invalide initail, need generic type parameter!");
		}
M
update  
MaxKey 已提交
86
		logger.trace("class : {}" , entityClass.getSimpleName());
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
87 88 89 90 91 92
	}

	/**
	 *  Load mapperClass by class name
	 * @param mapperClass
	 */
M
rename  
MaxKey 已提交
93
	public JpaService(String mapperClass) {
M
update  
MaxKey 已提交
94
		logger.trace("class : {}" , mapperClass);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
95 96 97
		this.mapperClass = mapperClass;
	}

M
rename  
MaxKey 已提交
98
	public void setMapper(IJpaMapper<T> mapper) {
M
MaxKey 已提交
99 100 101
		this.mapper = mapper;
	}
	
M
MaxKey 已提交
102
	//get or set mapper
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
103 104 105 106 107
	/**
	 * Load Mapper from spring container by mapperClass as bean id
	 * @return IBaseMapper
	 */
	@SuppressWarnings( { "unchecked" })
M
rename  
MaxKey 已提交
108
	public IJpaMapper<T> getMapper() {
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
109 110
		try {
			if(mapper == null) {
M
update  
MaxKey 已提交
111
				String mapperClassBean = StringUtils.firstToLowerCase(mapperClass);
M
update  
MaxKey 已提交
112
				logger.info("mapperClass Bean is {}" , mapperClassBean);
M
rename  
MaxKey 已提交
113
				mapper = (IJpaMapper<T>) MybatisJpaContext.getBean(mapperClassBean);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
114 115
			}
		} catch(Exception e) {
M
update  
MaxKey 已提交
116
			logger.error("getMapper Exception " , e);
M
MaxKey 已提交
117
		} 
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
118 119 120
		return mapper;
	}

M
MaxKey 已提交
121
	//follow function for fetch page
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
122
	
M
MaxKey 已提交
123
	public JpaPageResults<T> fetch(JpaPage page , T entity) {
M
MaxKey 已提交
124 125
		try {
			beforePageResults(page);
M
MaxKey 已提交
126
			List<T> resultslist = getMapper().fetch(page, entity);
M
MaxKey 已提交
127 128
			return buildPageResults(page , resultslist);
		}catch (Exception e) {
M
MaxKey 已提交
129
			logger.error("fetch Exception " , e);
M
MaxKey 已提交
130 131 132 133
		}
		return null;
	}
	
M
MaxKey 已提交
134
	public JpaPageResults<T> fetch(JpaPage page ,Query query) {
M
MaxKey 已提交
135 136
		try {
			beforePageResults(page);
M
MaxKey 已提交
137
			List<T> resultslist = getMapper().fetchByCondition(page, query , this.entityClass);
M
MaxKey 已提交
138 139
			return buildPageResults(page , resultslist);
		}catch (Exception e) {
M
MaxKey 已提交
140
			logger.error("fetch Exception " , e);
M
MaxKey 已提交
141 142 143 144
		}
		return null;
	}
	
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
145 146 147 148 149
	/**
	 * query page list entity by entity 
	 * @param entity
	 * @return
	 */
M
MaxKey 已提交
150
	public JpaPageResults<T> fetchPageResults(T entity) {
M
MaxKey 已提交
151 152 153 154 155 156 157 158
		try {
			beforePageResults(entity);
			List<T> resultslist = getMapper().fetchPageResults(entity);
			return buildPageResults(entity , resultslist);
		}catch (Exception e) {
			logger.error("fetchPageResults Exception " , e);
		}
		return null;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
159 160
	}
	
M
MaxKey 已提交
161
	public JpaPageResults<T> fetchPageResults(JpaPage page , T entity) {
M
MaxKey 已提交
162 163 164 165 166 167 168 169
		try {
			beforePageResults(entity);
			List<T> resultslist = getMapper().fetchPageResults(page , entity);
			return buildPageResults(entity , resultslist);
		}catch (Exception e) {
			logger.error("fetchPageResults page Exception " , e);
		}
		return null;
M
MaxKey 已提交
170
	}
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
171 172 173 174 175 176
	
	/**
	 * query page list entity by entity 
	 * @param entity
	 * @return
	 */
M
MaxKey 已提交
177 178
	public JpaPageResults<T> fetchPageResults(String mapperId,T entity) {
		return fetchPageResults(mapperId , null , entity);
M
MaxKey 已提交
179 180 181 182 183 184 185 186
	}
	
	/**
	 * query page list entity by entity 
	 * @param entity
	 * @return
	 */
	@SuppressWarnings("unchecked")
M
MaxKey 已提交
187
	public JpaPageResults<T> fetchPageResults(String mapperId,JpaPage page ,T entity) {
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
188
		try {
M
MaxKey 已提交
189 190 191 192 193
			beforePageResults(entity);
			List<T> resultslist = (List<T>)InstanceUtil.invokeMethod(getMapper(), mapperId, 
					page == null ? new Object[]{entity} : new Object[]{page , entity});
			return buildPageResults(entity , resultslist);
		}catch (NoSuchMethodException e) {
M
MaxKey 已提交
194
			logger.error("Mapper no fetchPageResults Method Exception " , e);
M
MaxKey 已提交
195
		}catch (Exception e) {
M
MaxKey 已提交
196
			logger.error("fetchPageResults Exception " , e);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
197
		}
M
3.1  
MaxKey 已提交
198
		return null;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
199 200
	}
	
M
MaxKey 已提交
201
	protected void beforePageResults(JpaPage page) {
M
MaxKey 已提交
202 203 204 205 206
		page.setPageResultSelectUUID(page.generateId());
		page.setStartRow(calculateStartRow(page.getPageNumber() ,page.getPageSize()));
		page.setPageable(true);
	}
	
M
MaxKey 已提交
207
	protected JpaPageResults<T> buildPageResults(JpaPage page , List<T> resultslist) {
M
MaxKey 已提交
208 209 210 211 212 213 214
		page.setPageable(false);
		Integer totalPage = resultslist.size();
		
		Integer totalCount = 0;
		if(page.getPageNumber() == 1 && totalPage < page.getPageSize()) {
			totalCount = totalPage;
		}else {
M
MaxKey 已提交
215
			totalCount = parseCount(getMapper().fetchCount(page));
M
MaxKey 已提交
216 217
		}
		
M
MaxKey 已提交
218
		return new JpaPageResults<>(page.getPageNumber(),page.getPageSize(),totalPage,totalCount,resultslist);
M
MaxKey 已提交
219 220
	}
	
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
221 222 223 224 225
	/**
	 * query Count by entity 
	 * @param entity
	 * @return
	 */
M
MaxKey 已提交
226
	public Integer fetchCount(JpaPage page) {
M
MaxKey 已提交
227
		Integer count = 0;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
228
		try {
M
MaxKey 已提交
229 230
			count = getMapper().fetchCount(page);
			logger.debug("fetchCount count : {}" , count);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
231
		} catch(Exception e) {
M
MaxKey 已提交
232
			logger.error("fetchCount Exception " , e);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
233
		}
M
MaxKey 已提交
234
		return count;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
235 236
	}
	
M
MaxKey 已提交
237
	//follow function for query
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
238 239 240 241 242 243 244 245 246
	/**
	 *  query list entity by entity 
	 * @param entity
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<T> query(T entity) {
		try {
			if(entity == null) {
M
update  
MaxKey 已提交
247
				entity = (T) entityClass.getDeclaredConstructor().newInstance();
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
248
			}
M
2.8  
MaxKey 已提交
249
			return getMapper().query(entity);
M
MaxKey 已提交
250
		} catch(Exception e) {
M
update  
MaxKey 已提交
251
			logger.error("query Exception " , e);
M
MaxKey 已提交
252
		}
M
MaxKey 已提交
253
		return Collections.emptyList();
M
MaxKey 已提交
254 255 256 257 258 259 260 261 262 263
	}
	
	/**
	 *  query list entity by Query 
	 * @param entity
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<T> query(Query query) {
		try {
M
MaxKey 已提交
264
			return getMapper().queryByCondition((T)entityClass.getDeclaredConstructor().newInstance(),query);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
265
		} catch(Exception e) {
M
update  
MaxKey 已提交
266
			logger.error("query Exception " , e);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
267
		}
M
MaxKey 已提交
268
		return Collections.emptyList();
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
269 270
	}
	
M
MaxKey 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
	/**
	 *  load entity by Query 
	 * @param entity
	 * @return
	 */
	public T load(Query query) {
		try {
			List<T> loadList = query(query);
			return  CollectionUtils.isEmpty(loadList) ? null : loadList.get(0);
		} catch(Exception e) {
			logger.error("load One Exception " , e);
		}
		return null;
	}
	
M
find  
MaxKey 已提交
286 287 288 289
	/**
	 * findAll from table
	 * @return
	 */
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
290 291 292 293
	public List<T> findAll() {
		try {
			return getMapper().findAll(this.entityClass);
		} catch(Exception e) {
M
MaxKey 已提交
294
			logger.error("findAll Exception" , e);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
295
		}
M
MaxKey 已提交
296
		return Collections.emptyList();
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
297 298
	}
	
M
find  
MaxKey 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
	
	/**
	 * select with filter and args
	 * 
	 * <pre>
	 * find(" StdNo = ? or StdNo = ?",new Object[]{"10024","10004"},new int[]{Types.VARCHAR,Types.INTEGER})
	 * </pre>
	 * @param filter
	 * @param args
	 * @param argTypes
	 * @return List<T>
	 * 
	 */
	public List<T> find(String filter , Object[] args , int[] argTypes) {
		try {
			return getMapper().find(this.entityClass,filter ,args , argTypes);
		} catch(Exception e) {
M
update  
MaxKey 已提交
316
			logger.error("findAll Exception " , e);
M
find  
MaxKey 已提交
317
		}
M
MaxKey 已提交
318
		return Collections.emptyList();
M
find  
MaxKey 已提交
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
	}
	
	/**
	 * select with filter 
	 * <pre>
	 * find(" StdNo = '10024')
	 * </pre>
	 * @param filter
	 * @return List<T>
	 */
	public List<T> find(String filter) {
		return find(filter ,null , null);
	}
	
	/**
	 * select with filter and args
	 * @param filter
	 * @param args
	 * @param argTypes
	 * @return T
	 */
	public T findOne(String filter , Object[] args , int[] argTypes) {
		try {
			List<T> findList = find(filter ,args , argTypes);
M
MaxKey 已提交
343
			return  CollectionUtils.isEmpty(findList) ? null : findList.get(0);
M
find  
MaxKey 已提交
344
		} catch(Exception e) {
M
update  
MaxKey 已提交
345
			logger.error("findAll Exception " , e);
M
find  
MaxKey 已提交
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
		}
		return null;
	}
	
	/**
	 * select with filter
	 * <pre>
	 * find(" StdNo = '10024')
	 * </pre>
	 * @param filter
	 * @return T
	 */
	public T findOne(String filter) {
		return findOne( filter ,null , null);
	}
	
M
MaxKey 已提交
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
	/**
	 * find entity by id List
	 * @param idList
	 * @return List<T>
	 */
	public List<T> findByIds(List<String> idList) {
		try {
			logger.trace("findByIds {}" , idList);
			List<T> findList = getMapper().findByIds(this.entityClass,idList,null);
			logger.trace("findByIds count : {}" , findList.size());
			return findList;
		} catch(Exception e) {
			logger.error("findByIds Exception " , e);
		}
		return Collections.emptyList();
	}
	
	/**
	 * find entity by id List
	 * @param idList
	 * @param partitionKey
	 * @return List<T>
	 */
	public List<T> findByIds(List<String> idList,String partitionKey) {
		try {
			logger.trace("findByIds {} , partitionKey {}" , idList , partitionKey);
			List<T> findList = getMapper().findByIds(this.entityClass , idList , partitionKey);
			logger.debug("findByIds count : {}" , findList.size());
			return findList;
		} catch(Exception e) {
			logger.error("findByIds Exception " , e);
		}
		return Collections.emptyList();
	}
	
	/**
	 * find entity by ids,split with ,
	 * @param ids
	 * @return List<T>
	 */
	public List<T> findByIds(String ids) {
		List<String> idList = StringUtils.string2List(ids, ",");
		return findByIds(idList);
	}
	
	/**
	 * find entity by ids,split with ,
	 * @param ids
	 * @param partitionKey
	 * @return
	 */
	public List<T> findByIds(String ids,String partitionKey) {
		List<String> idList = StringUtils.string2List(ids, ",");
		return findByIds(idList,partitionKey);
	}
	

	/**
	 * find  entity by ids , split with ,
	 * @param ids
	 * @param split
	 * @return List<T>
	 */
	public List<T> findByIdsSplit(String ids , String split) {
		List<String> idList = StringUtils.string2List(ids, StringUtils.isBlank(split)? "," : split );
		return findByIds(idList);
	}
	
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
430 431 432 433 434 435 436
	/**
	 * query one entity by entity id
	 * @param id
	 * @return
	 */
	public T get(String id) {
		try {
M
update  
MaxKey 已提交
437
			logger.debug("entityClass  {} , primaryKey {}" , entityClass.toGenericString() , id);
M
3.1  
MaxKey 已提交
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
			return  getMapper().get(this.entityClass,id,null);
		} catch(Exception e) {
			logger.error("get Exception " , e);
		}
		return null;
	}
	
	/**
	 * query one entity by entity id
	 * @param id
	 * @param partitionKey
	 * @return T
	 */
	public T get(String id,String partitionKey) {
		try {
			logger.debug("entityClass  {} , primaryKey {} , partitionKey {}" , entityClass.toGenericString() , id,partitionKey);
			return  getMapper().get(this.entityClass,id,partitionKey);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
455
		} catch(Exception e) {
M
update  
MaxKey 已提交
456
			logger.error("get Exception " , e);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
457 458 459 460
		}
		return null;
	}
	
M
MaxKey 已提交
461
	//follow function for insert update and delete
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
462 463 464 465 466 467 468
	/**
	 * insert new entity
	 * @param entity
	 * @return
	 */
	public boolean insert(T entity) {
		try {
M
MaxKey 已提交
469
			Integer count = getMapper().insert(entity);
M
update  
MaxKey 已提交
470
			logger.debug("insert count : {}" , count);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
471 472
			return  count > 0;
		} catch(Exception e) {
M
update  
MaxKey 已提交
473
			logger.error("insert Exception " , e);
M
MaxKey 已提交
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
		}
		return false;
	}
	
	/**
	 * insert entity with batch
	 * @param listEntity
	 * @return
	 */
	public boolean insertBatch(List<T> listEntity){
		try {
			if(BeanUtil.isNotNull(listEntity)) {
				Integer count = 0;
				for(T entity  : listEntity) {
					if(getMapper().insert(entity)>0) {
						count ++;
					}
				}
M
update  
MaxKey 已提交
492
				logger.debug("Insert Batch count : {}" , count);
M
MaxKey 已提交
493 494 495
				return count > 0;
			}
		} catch(Exception e) {
M
update  
MaxKey 已提交
496
			logger.error("Insert Batch Exception " , e);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
497 498 499 500
		}
		return false;
	}
	
MaxKey单点登录官方's avatar
JPA 3  
MaxKey单点登录官方 已提交
501
	/**
M
MaxKey 已提交
502
	 * JPA persist ,  save
MaxKey单点登录官方's avatar
JPA 3  
MaxKey单点登录官方 已提交
503 504 505 506 507 508 509 510
	 * @param entity
	 * @return boolean
	 */
	public boolean persist(T entity) {
		return insert(entity);
	}
	
	/**
M
MaxKey 已提交
511
	 * JPA merge , save or update
MaxKey单点登录官方's avatar
JPA 3  
MaxKey单点登录官方 已提交
512 513 514 515
	 * @param entity
	 * @return boolean
	 */
	public boolean merge(T entity) {
M
MaxKey 已提交
516 517
		List<T> resultList = query(entity);
		if(resultList == null || resultList.isEmpty()) {
MaxKey单点登录官方's avatar
JPA 3  
MaxKey单点登录官方 已提交
518 519 520 521 522 523
			return insert(entity);
		}else {
			return update(entity);
		}
	}
	
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
524 525 526 527 528 529 530 531
	/**
	 * update entity
	 * @param entity
	 * @return
	 */
	public boolean update(T entity) {
		try {
			Integer count=getMapper().update(entity);
M
update  
MaxKey 已提交
532
			logger.debug("update count : {}" , count);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
533 534
			return count > 0;
		} catch(Exception e) {
M
update  
MaxKey 已提交
535
			logger.error("update Exception " , e);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
536 537 538 539 540 541 542 543 544 545 546
		}
		return false;
	}
	
	/**
	 * delete entity by entity
	 * @param entity
	 * @return
	 */
	public boolean delete(T entity) {
		try {
M
3.1  
MaxKey 已提交
547
			Integer count = getMapper().delete(entity);
M
update  
MaxKey 已提交
548
			logger.debug("delete count : {}" , count);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
549 550
			return count > 0;
		} catch(Exception e) {
M
update  
MaxKey 已提交
551
			logger.error("delete Exception " , e);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
552 553 554 555
		}
		return false;
	}
	
M
MaxKey 已提交
556 557
	/**
	 * batch delete entity by id List
M
3.1  
MaxKey 已提交
558 559
	 * @param idList
	 * @return boolean
M
MaxKey 已提交
560 561
	 */
	public boolean deleteBatch(List<String> idList) {
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
562
		try {
M
update  
MaxKey 已提交
563
			logger.trace("deleteBatch {}" , idList);
M
3.1  
MaxKey 已提交
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
			Integer count = getMapper().deleteBatch(this.entityClass,idList,null);
			logger.debug("deleteBatch count : {}" , count);
			return count > 0;
		} catch(Exception e) {
			logger.error("deleteBatch Exception " , e);
		}
		return false;
	}
	
	/**
	 * batch delete entity by id List
	 * @param idList
	 * @param partitionKey
	 * @return boolean
	 */
	public boolean deleteBatch(List<String> idList,String partitionKey) {
		try {
			logger.trace("deleteBatch {} , partitionKey {}" , idList , partitionKey);
			Integer count = getMapper().deleteBatch(this.entityClass , idList , partitionKey);
M
update  
MaxKey 已提交
583
			logger.debug("deleteBatch count : {}" , count);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
584 585
			return count > 0;
		} catch(Exception e) {
M
update  
MaxKey 已提交
586
			logger.error("deleteBatch Exception " , e);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
587 588 589 590 591
		}
		return false;
	}
	
	/**
M
MaxKey 已提交
592 593
	 * batch delete entity by ids,split with ,
	 * @param ids
M
3.1  
MaxKey 已提交
594
	 * @return boolean
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
595
	 */
M
MaxKey 已提交
596 597 598 599
	public boolean deleteBatch(String ids) {
		List<String> idList = StringUtils.string2List(ids, ",");
		return deleteBatch(idList);
	}
M
3.1  
MaxKey 已提交
600 601 602 603 604 605 606 607 608 609 610 611
	
	/**
	 * batch delete entity by ids,split with ,
	 * @param ids
	 * @param partitionKey
	 * @return
	 */
	public boolean deleteBatch(String ids,String partitionKey) {
		List<String> idList = StringUtils.string2List(ids, ",");
		return deleteBatch(idList,partitionKey);
	}
	
M
MaxKey 已提交
612

M
3.1  
MaxKey 已提交
613 614 615 616 617 618 619 620
	/**
	 * batch delete entity by ids , split with ,
	 * @param ids
	 * @param split
	 * @return
	 */
	public boolean deleteBatchSplit(String ids , String split) {
		List<String> idList = StringUtils.string2List(ids, StringUtils.isBlank(split)? "," : split );
M
MaxKey 已提交
621 622 623
		return deleteBatch(idList);
	}
	
M
3.1  
MaxKey 已提交
624 625 626 627 628
	/**
	 * delete one entity by id
	 * @param id
	 * @return
	 */
M
MaxKey 已提交
629
	public boolean remove(String id){
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
630
		try {
M
3.1  
MaxKey 已提交
631 632
			logger.debug("id {} " , id );
			Integer count=getMapper().remove(this.entityClass,id,null);
M
update  
MaxKey 已提交
633
			logger.debug("remove count : {}" , count);
M
MaxKey 已提交
634
			return count > 0;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
635
		} catch(Exception e) {
M
update  
MaxKey 已提交
636
			logger.error("remove Exception " , e);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
637 638 639 640
		}
		return false;
	}
	
M
3.1  
MaxKey 已提交
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
	/**
	 * delete one entity by id
	 * @param id
	 * @param partitionKey
	 * @return
	 */
	public boolean remove(String id,String partitionKey){
		try {
			logger.debug("id {} , partitionKey {}" , id , partitionKey);
			Integer count = getMapper().remove(this.entityClass,id,partitionKey);
			logger.debug("remove count : {}" , count);
			return count > 0;
		} catch(Exception e) {
			logger.error("remove Exception " , e);
		}
		return false;
	}
	
	/**
	 * logicDelete entity by ids
	 * @param idList
	 * @return
	 */
M
MaxKey 已提交
664
	public boolean logicDelete(List<String> idList) {
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
665
		try {
M
3.1  
MaxKey 已提交
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
			logger.trace("logicDelete idList {}" , idList);
			Integer count = getMapper().logicDelete(this.entityClass,idList,null);
			logger.debug("logicDelete count : {}" , count);
			return count > 0;
		} catch(Exception e) {
			logger.error("logicDelete Exception " , e);
		}
		return true;
	}
	
	/**
	 * logicDelete entity by ids
	 * @param idList
	 * @param partitionKey
	 * @return
	 */
	public boolean logicDelete(List<String> idList,String partitionKey) {
		try {
			logger.trace("logicDelete idList {} , partitionKey {}" , idList , partitionKey);
			Integer count = getMapper().logicDelete(this.entityClass,idList,partitionKey);
M
update  
MaxKey 已提交
686
			logger.debug("logicDelete count : {}" , count);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
687 688
			return count > 0;
		} catch(Exception e) {
M
update  
MaxKey 已提交
689
			logger.error("logicDelete Exception " , e);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
690
		}
M
MaxKey 已提交
691
		return true;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
692
	}
MaxKey单点登录官方's avatar
2.5  
MaxKey单点登录官方 已提交
693
	
M
3.1  
MaxKey 已提交
694 695 696 697 698
	/**
	 * logicDelete entity by ids
	 * @param ids string
	 * @return
	 */
M
MaxKey 已提交
699
	public boolean logicDelete(String ids) {
MaxKey单点登录官方's avatar
2.5  
MaxKey单点登录官方 已提交
700
		List<String> idList = StringUtils.string2List(ids, ",");
M
MaxKey 已提交
701
		return logicDelete(idList);
MaxKey单点登录官方's avatar
2.5  
MaxKey单点登录官方 已提交
702
	}
M
MaxKey 已提交
703
	
M
3.1  
MaxKey 已提交
704 705 706 707 708 709 710 711
	/**
	 * logicDelete entity by ids
	 * @param ids string
	 * @param split
	 * @return
	 */
	public boolean logicDeleteSplit(String ids , String split) {
		List<String> idList = StringUtils.string2List(ids, StringUtils.isBlank(split)? "," : split );
M
MaxKey 已提交
712 713 714
		return logicDelete(idList);
	}
	
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
715
	
M
MaxKey 已提交
716
	//follow is  for query paging
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
717 718 719 720 721
	/**
	 * parse Object Count to Integer
	 * @param totalCount
	 * @return
	 */
M
MaxKey 已提交
722
	protected Integer parseCount(Object totalCount){
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
723 724 725 726
		Integer retTotalCount=0;
		if(totalCount == null) {
			return retTotalCount;
		}else{
M
MaxKey 已提交
727
			retTotalCount = Integer.parseInt(totalCount.toString());
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
728 729 730 731 732 733 734 735 736 737
		}
		return retTotalCount;
	}
	
	/**
	 * calculate total Count
	 * @param entity
	 * @param totalCount
	 * @return
	 */
M
MaxKey 已提交
738
	protected Integer calculateTotalPage(JpaEntity entity,Integer totalCount){
MaxKey单点登录官方's avatar
GA  
MaxKey单点登录官方 已提交
739
		return (totalCount + entity.getPageSize() - 1) / entity.getPageSize();
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
740 741 742 743 744 745 746 747
	}
	
	/**
	 * calculate StartRow
	 * @param page
	 * @param pageResults
	 * @return
	 */
M
MaxKey 已提交
748
	protected Integer calculateStartRow(Integer page,Integer pageSize){
MaxKey单点登录官方's avatar
GA  
MaxKey单点登录官方 已提交
749
		return (page - 1) * pageSize;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
750 751
	}
}