当前位置:   article > 正文

SpringBoot2.1.9 MongoDB逻辑操作_springboot entityinformation

springboot entityinformation

一、基础类配置

  1. @NoRepositoryBean
  2. public interface IMongoRepository<T, ID> extends MongoRepository<T, ID> {
  3. String getTableName();
  4. void deleteAll(Collection<ID> ids);
  5. MongoOperations getMongoOperations();
  6. MongoEntityInformation<T, ID> getEntityInformation();
  7. Class<T> getEntityClass();
  8. Class<ID> getIdClass();
  9. }
  1. public class AbstractMongoRepository<T, ID> extends SimpleMongoRepository<T, ID> implements IMongoRepository<T, ID> {
  2. private final MongoOperations mongoOperations;
  3. private final MongoEntityInformation<T, ID> entityInformation;
  4. public AbstractMongoRepository(MongoEntityInformation<T, ID> metadata, MongoOperations mongoOperations) {
  5. super(metadata, mongoOperations);
  6. this.mongoOperations = mongoOperations;
  7. this.entityInformation = metadata;
  8. }
  9. @Override
  10. public String getTableName(){
  11. return entityInformation.getCollectionName();
  12. }
  13. @Override
  14. public void deleteAll(Collection<ID> ids) {
  15. ids.forEach(this::deleteById);
  16. }
  17. @Override
  18. public MongoOperations getMongoOperations() {
  19. return mongoOperations;
  20. }
  21. @Override
  22. public MongoEntityInformation<T, ID> getEntityInformation() {
  23. return entityInformation;
  24. }
  25. @Override
  26. public Class<T> getEntityClass() {
  27. return entityInformation.getJavaType();
  28. }
  29. @Override
  30. public Class<ID> getIdClass() {
  31. return entityInformation.getIdType();
  32. }
  33. }
  1. @SpringBootApplication
  2. @EnableMongoRepositories(repositoryBaseClass = AbstractMongoRepository.class)
  3. public class MongoApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(MongoApplication.class, args);
  6. }
  7. }

而、查询

(1)普通查询

  1. public interface UserRepository extends IMongoRepository<User, ObjectId> {
  2. //jpa根据名字自动适配查询
  3. List<User> findByName(String name);
  4. List<User> findByNameAndAge(String name, int age);
  5. //手动指定查询
  6. default List<User> listByNameAndAge(String name, int age){
  7. Query q = new Query(Criteria.where(User.Fields.name).is(name)
  8. .and(User.Fields.age).is(age));
  9. return this.getMongoOperations().find(q, this.getEntityClass());
  10. }
  11. default List<User> find(ObjectId id){
  12. Query q = new Query(Criteria.where("_id").is(id));
  13. return this.getMongoOperations().find(q, this.getEntityClass());
  14. }
  15. default Optional<User> find2(ObjectId id){
  16. return this.findById(id);
  17. }
  18. default List<User> listByAge(int ageMin,int ageMax) {
  19. Query q = new Query(Criteria.where(User.Fields.name).exists(true)
  20. .andOperator(Criteria.where(User.Fields.age).gte(ageMin),
  21. Criteria.where(User.Fields.age).lt(ageMax)));
  22. return this.getMongoOperations().find(q, User.class, this.getTableName());
  23. }
  24. }

(2)排序

  1. public interface UserRepository extends IMongoRepository<User, ObjectId> {
  2. default List<User> listSort(int age){
  3. Query q = new Query(Criteria.where(User.Fields.age).is(age));
  4. q.with(Sort.by(Sort.Order.desc(User.Fields.name), Sort.Order.desc(User.Fields.age)));
  5. return this.getMongoOperations().find(q, this.getEntityClass());
  6. }
  7. }

(3)分页

  1. public interface UserRepository extends IMongoRepository<User, ObjectId> {
  2. default List<User> listLimit(int age){
  3. Query q = new Query(Criteria.where(User.Fields.age).is(age));
  4. q.skip(50).limit(10);
  5. return this.getMongoOperations().find(q, this.getEntityClass());
  6. }
  7. }

二、更新

  1. public interface UserRepository extends IMongoRepository<User, ObjectId> {
  2. default void update(User user){
  3. this.save(user);
  4. }
  5. default long updateAge(ObjectId id, int age){
  6. Query q = new Query(Criteria.where("_id").is(id));
  7. Update update = new Update();
  8. update.set(User.Fields.age, age);
  9. return this.getMongoOperations().updateFirst(q, update, getTableName()).getModifiedCount();
  10. }
  11. default long updateAge(String name, int age){
  12. Query q = new Query(Criteria.where(User.Fields.name).is(name));
  13. Update update = new Update();
  14. update.set(User.Fields.age, age);
  15. return this.getMongoOperations().updateMulti(q, update, getTableName()).getModifiedCount();
  16. }
  17. }

三、删除

  1. public interface UserRepository extends IMongoRepository<User, ObjectId> {
  2. default void delete(ObjectId id){
  3. this.deleteById(id);
  4. }
  5. default void delete1(User user){
  6. this.delete(user);
  7. }
  8. default DeleteResult delete2(ObjectId id){
  9. Query q = new Query(Criteria.where("_id").is(id));
  10. return this.getMongoOperations().remove(q, getTableName());
  11. }
  12. }

四、索引

  1. public interface UserRepository extends IMongoRepository<User, ObjectId> {
  2. default void createIndex() {
  3. MongoCollection<Document> collection = getMongoOperations().getCollection(getTableName());
  4. BasicDBObject indexOptions = new BasicDBObject();
  5. indexOptions.put(User.Fields.name, -1);
  6. String alarmSummaryId = collection.createIndex(indexOptions);
  7. indexOptions = new BasicDBObject();
  8. indexOptions.put(User.Fields.age, 1);
  9. String createAt = collection.createIndex(indexOptions);
  10. }
  11. }

五、聚合

  1. public interface UserRepository extends IMongoRepository<User, ObjectId> {
  2. default Map<ObjectId,String> listAgeFirstCity(int skip, int limit){
  3. Criteria criteria = Criteria.where(User.Fields.city).exists(true);
  4. Aggregation aggregation = Aggregation.newAggregation(Aggregation.match(criteria),
  5. Aggregation.group(User.Fields.age).first( "$city").as( "city"),
  6. Aggregation.skip(skip), Aggregation.limit(limit));
  7. List<Map> list = this.getMongoOperations().aggregate(aggregation, getTableName(), Map.class).getMappedResults();
  8. Map<ObjectId, String> data = new HashMap<>();
  9. for(Map<String,Object> map : list){
  10. data.put((ObjectId) map.get("_id"), (String)map.get("city"));
  11. }
  12. return data;
  13. }
  14. }

六、连接

  1. public interface UserRepository extends IMongoRepository<User, ObjectId> {
  2. default List<UserVo> list() {
  3. LookupOperation lookupOperation= LookupOperation.newLookup().
  4. from(AdminRepository.instance().getTableName()). //关联从表名
  5. localField(User.Fields.name). //主表关联字段
  6. foreignField(Admin.Fields.name).//从表关联的字段
  7. as("adminInfo"); //查询结果名
  8. Criteria left = Criteria.where(User.Fields.city).exists(true)
  9. .andOperator(Criteria.where(User.Fields.age).gte(18),
  10. Criteria.where(User.Fields.age).lt(65));
  11. Criteria right = Criteria.where("adminInfo.0").exists(true).and("adminInfo.0.admin").is(1);
  12. Aggregation aggregation = Aggregation.newAggregation(Aggregation.match(left), lookupOperation,
  13. Aggregation.match(right), Aggregation.unwind("adminInfo", true));
  14. List<UserVo> results = this.getMongoOperations().aggregate(aggregation, this.getTableName(), UserVo.class).getMappedResults();
  15. return results;
  16. }
  17. @Setter
  18. @Getter
  19. public static class UserVo extends User{
  20. private Admin adminInfo;
  21. }
  22. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/55990
推荐阅读
相关标签
  

闽ICP备14008679号