PlusServiceImpl.java
3.16 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
package com.huaheng.framework.mybatisPlus;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
public class PlusServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements IPlusService<T>
{
@Override
public T selectOne(Wrapper<T> wrapper) {
List<T> list = selectPage(new Page<T>(1, 1), wrapper).getRecords();
if (list.size() > 0) return list.get(0);
else return null;
}
@Override
public T selectFirstEntity(T entity) throws IllegalAccessException {
List<T> list = selectPage(new Page<T>(1, 1), entity).getRecords();
if (list.size() > 0) return list.get(0);
else return null;
}
@Override
public List<T> selectListEntityByEqual(T entity) throws IllegalAccessException {
EntityWrapper<T> entityWrapper = getWrapper(entity);
return super.selectList(entityWrapper);
}
@Override
public List<Map<String, Object>> selectListMapByEqual(T entity, String SqlSelect) throws IllegalAccessException {
EntityWrapper<T> entityWrapper = getWrapper(entity);
entityWrapper.setSqlSelect(SqlSelect);
return super.selectMaps(entityWrapper);
}
@Override
public Page<T> selectPage(Page<T> page, T entity) throws IllegalAccessException {
EntityWrapper<T> entityWrapper = getWrapper(entity);
return super.selectPage(page, entityWrapper);
}
@Override
public Page<Map<String, Object>> selectMapsPage(Page page, T entity) throws IllegalAccessException {
EntityWrapper<T> entityWrapper = getWrapper(entity);
return super.selectMapsPage(page, entityWrapper);
}
@Override
public Integer selectCount(T entity) throws IllegalAccessException {
EntityWrapper<T> entityWrapper = getWrapper(entity);
return super.selectCount(entityWrapper);
}
@Override
public boolean updateByCondition(T record, T condition) throws IllegalAccessException {
EntityWrapper<T> entityWrapper = getWrapper(condition);
return super.update(record, entityWrapper);
}
@Override
public boolean delete(T entity) throws IllegalAccessException {
EntityWrapper<T> entityWrapper = getWrapper(entity);
return super.delete(entityWrapper);
}
@Override
public <T> EntityWrapper getWrapper (T entity) throws IllegalAccessException {
EntityWrapper<T> entityWrapper = new EntityWrapper<T>();
entityWrapper.setEntity(entity);
// Field[] fields = entity.getClass().getDeclaredFields();
// Field.setAccessible(fields, true);
// for (int i = 0; i < fields.length; i++) {
// String filedName = fields[i].getName();
// Object filedValue = fields[i].get(entity);
// if (!filedName.equals("serialVersionUID") && filedValue != null)
// entityWrapper.eq(filedName, filedValue);
// }
return entityWrapper;
}
}