MongoDB
 sql >> Teknologi Basis Data >  >> NoSQL >> MongoDB

Spring Data Mongo Repository ::Metode bersama yang umum di semua masalah Repo

Inilah solusi terbaik!

Langkah Pertama:
Tambahkan metode khusus ke antarmuka!
增加一个自定义的方法

#antarmuka khusus

/**
 * Basic Repository for common custom methods
 * @author liangping
 */

import java.io.Serializable;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.PagingAndSortingRepository;

@NoRepositoryBean
public interface WootideRepositoryCustom <T, ID extends Serializable>
      extends PagingAndSortingRepository<T, ID>, MongoRepository<T, ID> {

      public Page<T> search(Query query, Pageable pageable);
}

Implementasi

Langkah Kedua:
Tambahkan penerapan untuk metode khusus Anda!
实现你的自定义方法

/**
 * implement for wootide basic repository
 * @author liangping
 */

import java.io.Serializable;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.data.mongodb.repository.support.SimpleMongoRepository;

public class WootideRepositoryImpl<T, ID extends Serializable> extends
        SimpleMongoRepository<T, ID> implements WootideRepositoryCustom<T, ID> {

    public WootideRepositoryImpl(MongoEntityInformation<T, ID> metadata,
            MongoOperations mongoOperations) {
        super(metadata, mongoOperations);
    }

    @Override
    public Page<T> search(Query query, Pageable pageable) {
        long total = this.getMongoOperations().count(query, this.getEntityInformation().getJavaType() );
        return new PageImpl<T>(this.getMongoOperations().find(query.with(pageable), this.getEntityInformation().getJavaType()), pageable, total);
    }

}

Buat pabrik baru untuk repositori khusus

/**
 * Repository Factory for all Subrepository
 * @author liangping
 */

import java.io.Serializable;

import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.data.mongodb.repository.support.MappingMongoEntityInformation;
import org.springframework.data.mongodb.repository.support.MongoRepositoryFactory;
import org.springframework.data.mongodb.repository.support.MongoRepositoryFactoryBean;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;

public class WootideRepositoryFactoryBean<R extends MongoRepository<T, I>, T, I extends Serializable>
        extends MongoRepositoryFactoryBean<R, T, I> {

    @Override
    protected RepositoryFactorySupport getFactoryInstance(
            MongoOperations operations) {
        return new WootideMongoRepositoryFactory<T,I>( operations );
    }

    private static class WootideMongoRepositoryFactory<T, ID extends Serializable>
            extends MongoRepositoryFactory {

        private MongoOperations mongo;
        public WootideMongoRepositoryFactory(MongoOperations mongoOperations) {
            super(mongoOperations);
            this.mongo = mongoOperations;
        }

        @SuppressWarnings("unchecked")
        protected Object getTargetRepository(RepositoryMetadata metadata) {

            TypeInformation<T> information =  ClassTypeInformation.from((Class<T>)metadata.getDomainType());
            MongoPersistentEntity<T> pe = new BasicMongoPersistentEntity<T>(information);
            MongoEntityInformation<T,ID> mongometa = new MappingMongoEntityInformation<T, ID>(pe);

            return new WootideRepositoryImpl<T, ID>( mongometa,  mongo);
        }

        protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
            return WootideRepositoryCustom.class;
        }
    }
}

Buat itu berhasil

<mongo:repositories base-package="com.***.mongodb" 
factory-class="com.***.mongodb.custom.WootideRepositoryFactoryBean"/>

Semoga berhasil!



  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. Bagaimana Menguji Peningkatan Aplikasi MongoDB Anda?

  2. MongoDB menggabungkan data di dalam array objek

  3. Apakah kerangka Agregasi Mongodb lebih cepat dari peta/kurangi?

  4. MongoDB $abs

  5. Bagaimana Memastikan Cluster MongoDB Anda Dapat Bertahan dari Pemadaman Amazon AWS?