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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
| import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.metadata.IPage; import com.mongodb.BasicDBObject; import com.mongodb.DBObject; import com.mongodb.client.result.UpdateResult; import hss.server.hss.entity.HssHistoryEntity; import hss.server.utils.DateUtils; import hss.server.utils.PageUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.index.Index; import org.springframework.data.mongodb.core.index.IndexOperations; import org.springframework.data.mongodb.core.query.*; import org.springframework.stereotype.Component; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils;
import java.io.Serializable; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.*;
@Component @Slf4j public class MongoDBUtil {
private static final Query EMPTY_QUERY = new BasicQuery("{}"); private static MongoTemplate template;
@Autowired public void setTemplate(MongoTemplate template) { MongoDBUtil.template = template; }
private void setIndex() { IndexOperations indexOps = template.indexOps(HssHistoryEntity.class); Index index = new Index("equipmentId", Sort.Direction.ASC); index.on("typeId", Sort.Direction.ASC); indexOps.ensureIndex(index); }
private static Query idEqQuery(Serializable id) { Criteria criteria = Criteria.where("id").is(id); return Query.query(criteria); }
private Query idInQuery(Collection<? extends Serializable> idList) { Criteria criteria = Criteria.where("id").in(idList); return Query.query(criteria); }
private Query eqQuery(Map<String, Object> data) { if (CollectionUtils.isEmpty(data)) { return EMPTY_QUERY; } else { Criteria criteria = new Criteria(); data.forEach((k, v) -> criteria.and(k).is(v)); return Query.query(criteria); } }
private static <T> Serializable getIdValue(T entity) { try { Field field = entity.getClass().getDeclaredField("id"); field.setAccessible(true); return (Serializable) field.get(entity); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); } return null; }
private <T> Update getUpdate(T entity) { Field[] fields = entity.getClass().getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); try { System.out.println(field.getName() + " " + field.get(entity)); } catch (IllegalAccessException e) { e.printStackTrace(); } } return null; }
public <T> void save(T entity) { template.save(entity); }
public <T> void inset(T entity) { template.insert(entity); }
public <T> void saveBatch(Collection<T> entityList) { template.insertAll(entityList); }
public void removeById(Serializable id, Class<?> clazz) { template.remove(idEqQuery(id.toString()), clazz); }
public void removeByMap(Map<String, Object> columnMap, Class<?> clazz) { template.remove(eqQuery(columnMap), clazz); }
public void removeByIds(Collection<? extends Serializable> idList, Class<?> clazz) { template.remove(idInQuery(idList), clazz); }
public void remove(Query query, Class<?> clazz) { template.remove(query, clazz); }
public <T> boolean updateById(T entity) { Assert.notNull(entity, "entity must not be null!"); JSONObject obj = (JSONObject) JSONObject.toJSON(entity); DBObject update = new BasicDBObject(); update.put("$set", obj); UpdateResult result = template.updateFirst(idEqQuery(getIdValue(entity)), new BasicUpdate(update.toString()), entity.getClass()); return result.getModifiedCount() == 1L; }
public <T> void updateBatchById(Collection<T> entityList) { entityList.forEach(e -> updateById(e)); }
public void update(Query query, Update update, Class<?> clazz) { template.updateMulti(query, update, clazz); }
public static <T> void saveOrUpdate(T entity) { Assert.notNull(entity, "entity must not be null!"); String key = JSONObject.toJSONString(entity); Update inc = new Update().inc(key, 1); template.upsert(idEqQuery(getIdValue(entity)), inc, entity.getClass()); }
public <T> void saveOrUpdateBatch(Collection<T> entityList) { entityList.forEach(MongoDBUtil::saveOrUpdate); }
public <T> T getById(Serializable id, Class<T> clazz) { return template.findById(id.toString(), clazz); }
public <T> T getOne(Query query, Class<T> clazz) { return template.findOne(query, clazz); }
public <T> List<T> listByIds(Collection<? extends Serializable> idList, Class<T> clazz) { return template.find(idInQuery(idList), clazz); }
public <T> List<T> listByMap(Map<String, Object> columnMap, Class<T> clazz) { return template.find(eqQuery(columnMap), clazz); }
public <T> List<T> list(Class<T> clazz) { return template.findAll(clazz); }
public <T> List<T> list(Query query, Class<T> clazz) { return template.find(query, clazz); }
public <T> long count(Class<T> clazz) { return template.count(EMPTY_QUERY, clazz); }
public <T> long count(Query query, Class<T> clazz) { return template.count(query, clazz); }
public <T> PageUtils page(Map<String, Object> params, Query query, Class<T> clazz) { IPage<T> page = new hss.server.utils.Query<T>().getPage(params); page.setTotal(count(query, clazz)); Pageable pageable = PageRequest.of((int) page.getCurrent() - 1, (int) page.getSize()); query.with(pageable); List<T> records = template.find(query, clazz); page.setPages(page.getPages()); page.setRecords(records); return new PageUtils(page); } }
|