Commit 6e93264a1d8100295ca2e3b0a2bc58f547c22a26
1 parent
5c658ba897
Exists in
master
and in
8 other branches
增加批量
Showing 2 changed files with 104 additions and 92 deletions
platform-common/src/main/java/com/lyms/platform/common/dao/BaseMongoDAO.java
View file @
6e93264
1 | 1 | package com.lyms.platform.common.dao; |
2 | 2 | |
3 | +import java.util.Collection; | |
3 | 4 | import java.util.List; |
4 | 5 | |
5 | 6 | import org.springframework.data.mongodb.core.query.Query; |
... | ... | @@ -48,6 +49,9 @@ |
48 | 49 | */ |
49 | 50 | public T save(T entity, String collection); |
50 | 51 | |
52 | + | |
53 | + public void batchSave(Collection<T> list); | |
54 | + | |
51 | 55 | /** |
52 | 56 | * 通过ID获取记录 |
53 | 57 | * |
... | ... | @@ -69,7 +73,6 @@ |
69 | 73 | /** |
70 | 74 | * 分页查询 |
71 | 75 | * |
72 | - * @param page | |
73 | 76 | * @param query |
74 | 77 | * @return |
75 | 78 | */ |
... | ... | @@ -91,6 +94,7 @@ |
91 | 94 | * @return |
92 | 95 | */ |
93 | 96 | public long count(Query query, String collection); |
97 | + | |
94 | 98 | |
95 | 99 | } |
platform-common/src/main/java/com/lyms/platform/common/dao/BaseMongoDAOImpl.java
View file @
6e93264
1 | 1 | package com.lyms.platform.common.dao; |
2 | 2 | |
3 | -import java.util.List; | |
4 | - | |
3 | +import com.lyms.platform.common.dao.operator.Page; | |
4 | +import com.lyms.platform.common.utils.MongoConvertHelper; | |
5 | +import com.lyms.platform.common.utils.ReflectionUtils; | |
6 | +import org.apache.commons.collections.CollectionUtils; | |
5 | 7 | import org.springframework.beans.factory.annotation.Autowired; |
6 | 8 | import org.springframework.data.mongodb.core.MongoTemplate; |
7 | 9 | import org.springframework.data.mongodb.core.query.Query; |
8 | 10 | import org.springframework.data.mongodb.core.query.Update; |
9 | 11 | import org.springframework.util.Assert; |
10 | 12 | |
11 | -import com.lyms.platform.common.dao.operator.Page; | |
12 | -import com.lyms.platform.common.utils.MongoConvertHelper; | |
13 | -import com.lyms.platform.common.utils.ReflectionUtils; | |
13 | +import java.util.Collection; | |
14 | +import java.util.List; | |
14 | 15 | |
15 | 16 | public class BaseMongoDAOImpl<T> implements BaseMongoDAO<T> { |
16 | 17 | |
17 | - /** | |
18 | - * spring mongodb 集成操作类 | |
19 | - */ | |
20 | - @Autowired | |
21 | - private MongoTemplate mongoTemplate; | |
18 | + /** | |
19 | + * spring mongodb 集成操作类 | |
20 | + */ | |
21 | + @Autowired | |
22 | + private MongoTemplate mongoTemplate; | |
22 | 23 | |
23 | - @Override | |
24 | - public List<T> find(Query query) { | |
25 | - Assert.notNull(query, "execute find method query must not null."); | |
26 | - return mongoTemplate.find(query, this.getEntityClass()); | |
27 | - } | |
24 | + @Override | |
25 | + public void batchSave(Collection<T> list) { | |
26 | + if (CollectionUtils.isNotEmpty(list)) { | |
27 | + mongoTemplate.insertAll(list); | |
28 | + } | |
29 | + } | |
28 | 30 | |
29 | - @Override | |
30 | - public T findOne(Query query) { | |
31 | - Assert.notNull(query, "execute findOne method query must not null."); | |
32 | - return mongoTemplate.findOne(query, this.getEntityClass()); | |
33 | - } | |
31 | + @Override | |
32 | + public List<T> find(Query query) { | |
33 | + Assert.notNull(query, "execute find method query must not null."); | |
34 | + return mongoTemplate.find(query, this.getEntityClass()); | |
35 | + } | |
34 | 36 | |
35 | - /** | |
36 | - * 修改根据所有查询条件查出来的数据 | |
37 | - */ | |
38 | - public void update(Query query, T obj) { | |
39 | - Update update = MongoConvertHelper | |
40 | - .convertToNativeUpdate(ReflectionUtils.getUpdateField(obj)); | |
41 | - Assert.notNull(update, "execute update method must not null."); | |
42 | - mongoTemplate.updateMulti(query, update, this.getEntityClass()); | |
43 | - } | |
37 | + @Override | |
38 | + public T findOne(Query query) { | |
39 | + Assert.notNull(query, "execute findOne method query must not null."); | |
40 | + return mongoTemplate.findOne(query, this.getEntityClass()); | |
41 | + } | |
44 | 42 | |
45 | - @Override | |
46 | - public T save(T entity) { | |
47 | - Assert.notNull(entity, "execute insert method must not null."); | |
48 | - mongoTemplate.insert(entity); | |
49 | - return entity; | |
50 | - } | |
43 | + /** | |
44 | + * 修改根据所有查询条件查出来的数据 | |
45 | + */ | |
46 | + public void update(Query query, T obj) { | |
47 | + Update update = MongoConvertHelper | |
48 | + .convertToNativeUpdate(ReflectionUtils.getUpdateField(obj)); | |
49 | + Assert.notNull(update, "execute update method must not null."); | |
50 | + mongoTemplate.updateMulti(query, update, this.getEntityClass()); | |
51 | + } | |
51 | 52 | |
52 | - @Override | |
53 | - public T save(T entity, String collection) { | |
54 | - Assert.notNull(entity, "execute insert method must not null."); | |
55 | - mongoTemplate.insert(entity, collection); | |
56 | - return entity; | |
57 | - } | |
53 | + @Override | |
54 | + public T save(T entity) { | |
55 | + Assert.notNull(entity, "execute insert method must not null."); | |
56 | + mongoTemplate.insert(entity); | |
57 | + return entity; | |
58 | + } | |
58 | 59 | |
59 | - @Override | |
60 | - public T findById(String id) { | |
61 | - return mongoTemplate.findById(id, this.getEntityClass()); | |
62 | - } | |
60 | + @Override | |
61 | + public T save(T entity, String collection) { | |
62 | + Assert.notNull(entity, "execute insert method must not null."); | |
63 | + mongoTemplate.insert(entity, collection); | |
64 | + return entity; | |
65 | + } | |
63 | 66 | |
64 | - @Override | |
65 | - public T findById(String id, String collectionName) { | |
66 | - return mongoTemplate | |
67 | - .findById(id, this.getEntityClass(), collectionName); | |
68 | - } | |
67 | + @Override | |
68 | + public T findById(String id) { | |
69 | + return mongoTemplate.findById(id, this.getEntityClass()); | |
70 | + } | |
69 | 71 | |
70 | - public Page<T> findPage(Query query) { | |
71 | - Page<T> page = new Page<T>(); | |
72 | - page.setPage(query.getSkip()); | |
73 | - page.setLimit(query.getLimit()); | |
74 | - | |
75 | - long count = this.count(query); | |
76 | - page.reBuild((int) count); | |
77 | - List<T> rows = this.find(query); | |
78 | - page.setDataList(rows); | |
79 | - return page; | |
80 | - } | |
72 | + @Override | |
73 | + public T findById(String id, String collectionName) { | |
74 | + return mongoTemplate | |
75 | + .findById(id, this.getEntityClass(), collectionName); | |
76 | + } | |
81 | 77 | |
82 | - @Override | |
83 | - public long count(Query query) { | |
84 | - return mongoTemplate.count(query, this.getEntityClass()); | |
85 | - } | |
78 | + public Page<T> findPage(Query query) { | |
79 | + Page<T> page = new Page<T>(); | |
80 | + page.setPage(query.getSkip()); | |
81 | + page.setLimit(query.getLimit()); | |
86 | 82 | |
87 | - public long count(Query query, String collection) { | |
88 | - return mongoTemplate.count(query, collection); | |
89 | - } | |
83 | + long count = this.count(query); | |
84 | + page.reBuild((int) count); | |
85 | + List<T> rows = this.find(query); | |
86 | + page.setDataList(rows); | |
87 | + return page; | |
88 | + } | |
90 | 89 | |
91 | - public void delete(Query query) { | |
92 | - mongoTemplate.findAllAndRemove(query, this.getEntityClass()); | |
93 | - } | |
90 | + @Override | |
91 | + public long count(Query query) { | |
92 | + return mongoTemplate.count(query, this.getEntityClass()); | |
93 | + } | |
94 | 94 | |
95 | - /** | |
96 | - * 获取需要操作的实体类class | |
97 | - * | |
98 | - * @return | |
99 | - */ | |
100 | - private Class<T> getEntityClass() { | |
101 | - return ReflectionUtils.getSuperClassGenricType(getClass()); | |
102 | - } | |
95 | + public long count(Query query, String collection) { | |
96 | + return mongoTemplate.count(query, collection); | |
97 | + } | |
98 | + | |
99 | + public void delete(Query query) { | |
100 | + mongoTemplate.findAllAndRemove(query, this.getEntityClass()); | |
101 | + } | |
102 | + | |
103 | + /** | |
104 | + * 获取需要操作的实体类class | |
105 | + * | |
106 | + * @return | |
107 | + */ | |
108 | + private Class<T> getEntityClass() { | |
109 | + return ReflectionUtils.getSuperClassGenricType(getClass()); | |
110 | + } | |
103 | 111 | } |