admin
2022-03-25 17055fd8d36504b79a5def28f5d4b4740faf012d
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
package org.yeshi.utils.mongo;
 
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
 
import javax.annotation.Resource;
import java.lang.reflect.ParameterizedType;
import java.util.List;
 
/**
 * @author Administrator
 * @title: MongodbBaseDao
 * @description: base dao
 * @date 2021/10/13 15:03
 */
public class MongodbBaseDao<T> {
 
    @Resource
    protected MongoTemplate mongoTemplate;
 
    /**
     * 插入数据
     *
     * @param bean
     * @return
     */
    public T save(T bean) {
        mongoTemplate.save(bean);
        return bean;
    }
 
    /**
     * 根据主键更新数据
     *
     * @param query
     * @param update
     */
    public void update(Query query, Update update) {
        mongoTemplate.upsert(query, update, this.getEntityClass());
    }
 
    /**
     * 查询一个数据
     *
     * @param query
     * @return
     */
    public T findOne(Query query) {
        return (T) mongoTemplate.findOne(query, this.getEntityClass());
    }
 
    /**
     * 查询多个数据
     *
     * @param query
     * @return
     */
    public List<T> findList(Query query) {
        return mongoTemplate.find(query, this.getEntityClass());
    }
 
 
    /**
     * 统计数量
     *
     * @param query
     * @return
     */
    public long count(Query query) {
        return mongoTemplate.count(query, this.getEntityClass());
    }
 
    /**
     * 主键查询
     *
     * @param id
     * @return
     */
    public T get(Object id) {
        return (T) mongoTemplate.findById(id, this.getEntityClass());
    }
 
 
    /**
     * 聚合查询
     *
     * @param opts
     * @param output
     * @return
     */
    public AggregationResults aggregate(List<? extends AggregationOperation> opts, Class output) {
        Aggregation aggregation = Aggregation.newAggregation(opts);
        return mongoTemplate.aggregate(aggregation, this.getEntityClass(), output);
    }
 
    /**
     * 通过主键删除
     *
     * @param id
     */
    public void delete(Object id) {
        Query query = Query.query(Criteria.where("id").is(id));
        mongoTemplate.remove(query, getEntityClass());
    }
 
    public void delete(Query query) {
        mongoTemplate.remove(query, getEntityClass());
    }
 
    @SuppressWarnings("unchecked")
    protected Class<T> getEntityClass() {
        Class<T> tClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass())
                .getActualTypeArguments()[0];
        return tClass;
    }
 
}