提交 | 用户 | 时间
|
a6de49
|
1 |
/** |
H |
2 |
* Copyright (c) 2018 人人开源 All rights reserved. |
|
3 |
* <p> |
|
4 |
* https://www.renren.io |
|
5 |
* <p> |
|
6 |
* 版权所有,侵权必究! |
|
7 |
*/ |
|
8 |
|
|
9 |
package com.iailab.framework.common.service.impl; |
|
10 |
|
|
11 |
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
|
12 |
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
|
13 |
import com.baomidou.mybatisplus.core.metadata.IPage; |
|
14 |
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit; |
|
15 |
import com.iailab.framework.common.page.PageData; |
|
16 |
import com.iailab.framework.common.service.CrudService; |
|
17 |
import com.iailab.framework.common.util.object.ConvertUtils; |
|
18 |
import org.springframework.beans.BeanUtils; |
|
19 |
|
|
20 |
import java.util.Arrays; |
|
21 |
import java.util.List; |
|
22 |
import java.util.Map; |
|
23 |
|
|
24 |
/** |
|
25 |
* CRUD基础服务类 |
|
26 |
* |
|
27 |
* @author Mark sunlightcs@gmail.com |
|
28 |
*/ |
|
29 |
public abstract class CrudServiceImpl<M extends BaseMapper<T>, T, D> extends BaseServiceImpl<M, T> implements CrudService<T, D> { |
|
30 |
|
|
31 |
protected Class<D> currentDtoClass() { |
|
32 |
return (Class<D>)ReflectionKit.getSuperClassGenericType(getClass(), CrudServiceImpl.class, 2); |
|
33 |
} |
|
34 |
|
|
35 |
@Override |
|
36 |
public PageData<D> page(Map<String, Object> params) { |
|
37 |
IPage<T> page = baseDao.selectPage( |
|
38 |
getPage(params, null, false), |
|
39 |
getWrapper(params) |
|
40 |
); |
|
41 |
|
|
42 |
return getPageData(page, currentDtoClass()); |
|
43 |
} |
|
44 |
|
|
45 |
@Override |
|
46 |
public List<D> list(Map<String, Object> params) { |
|
47 |
List<T> entityList = baseDao.selectList(getWrapper(params)); |
|
48 |
|
|
49 |
return ConvertUtils.sourceToTarget(entityList, currentDtoClass()); |
|
50 |
} |
|
51 |
|
|
52 |
public abstract QueryWrapper<T> getWrapper(Map<String, Object> params); |
|
53 |
|
|
54 |
@Override |
|
55 |
public D get(Long id) { |
|
56 |
T entity = baseDao.selectById(id); |
|
57 |
|
|
58 |
return ConvertUtils.sourceToTarget(entity, currentDtoClass()); |
|
59 |
} |
|
60 |
|
|
61 |
@Override |
|
62 |
public void save(D dto) { |
|
63 |
T entity = ConvertUtils.sourceToTarget(dto, currentModelClass()); |
|
64 |
insert(entity); |
|
65 |
|
|
66 |
//copy主键值到dto |
|
67 |
BeanUtils.copyProperties(entity, dto); |
|
68 |
} |
|
69 |
|
|
70 |
@Override |
|
71 |
public void update(D dto) { |
|
72 |
T entity = ConvertUtils.sourceToTarget(dto, currentModelClass()); |
|
73 |
updateById(entity); |
|
74 |
} |
|
75 |
|
|
76 |
@Override |
|
77 |
public void delete(Long[] ids) { |
|
78 |
baseDao.deleteBatchIds(Arrays.asList(ids)); |
|
79 |
} |
|
80 |
} |