#1.结构图
maven配置这些就不赘述了,可以看我前一些天的博客
Repository源码
/**
* Central repository marker interface. Captures the domain type to manage as well as the domain type's id type. General
* purpose is to hold type information as well as being able to discover interfaces that extend this one during
* classpath scanning for easy Spring bean creation.
* <p>
* Domain repositories extending this interface can selectively expose CRUD methods by simply declaring methods of the
* same signature as those declared in {@link CrudRepository}.
*
* @see CrudRepository
* @param <T> the domain type the repository manages 实体类
* @param <ID> the type of the id of the entity the repository manages 实体id的类型
* @author Oliver Gierke
*/
@Indexed
public interface Repository<T, ID> {
}
MongoRepository源码,默认提供的相关方法
/**
* Mongo specific {@link org.springframework.data.repository.Repository} interface.
*
* @author Oliver Gierke
* @author Christoph Strobl
* @author Thomas Darimont
* @author Mark Paluch
* @author Khaled Baklouti
*/
@NoRepositoryBean
public interface MongoRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#saveAll(java.lang.Iterable)
*/
@Override
<S extends T> List<S> saveAll(Iterable<S> entities);
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#findAll()
*/
@Override
List<T> findAll();
/*
* (non-Javadoc)
* @see org.springframework.data.repository.PagingAndSortingRepository#findAll(org.springframework.data.domain.Sort)
*/
@Override
List<T> findAll(Sort sort);
/**
* Inserts the given entity. Assumes the instance to be new to be able to apply insertion optimizations. Use the
* returned instance for further operations as the save operation might have changed the entity instance completely.
* Prefer using {@link #save(Object)} instead to avoid the usage of store-specific API.
*
* @param entity must not be {@literal null}.
* @return the saved entity
* @since 1.7
*/
<S extends T> S insert(S entity);
/**
* Inserts the given entities. Assumes the given entities to have not been persisted yet and thus will optimize the
* insert over a call to {@link #save(Iterable)}. Prefer using {@link #save(Iterable)} to avoid the usage of store
* specific API.
*
* @param entities must not be {@literal null}.
* @return the saved entities
* @since 1.7
*/
<S extends T> List<S> insert(Iterable<S> entities);
/*
* (non-Javadoc)
* @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example)
*/
@Override
<S extends T> List<S> findAll(Example<S> example);
/*
* (non-Javadoc)
* @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example, org.springframework.data.domain.Sort)
*/
@Override
<S extends T> List<S> findAll(Example<S> example, Sort sort);
}
#2.操作方式
写一个接口继承该类MongoRepository,然后注入这个接口,此时这个接口就有一些基本的CRUD功能了.
图示如下:
/**
* Description:
* <p>
* Author:
* Date: 2019/11/4 15:51
*/
public interface ICustomerRepository extends MongoRepository {
}
save相关方法
find相关方法
delete相关方法:
insert相关方法:
是的,我们只需要写一个接口继承MongoRepository ,然后再注入这个接口,由SpringBoot框架帮助我们完成实现类.
#3.扩展自己的方法
然后你就有这个方法了
显然这只是一种方式,而SpringBoot整合MongoDB这只是冰山一角
你可以自己尝试把这个项目全部翻译一遍,然后做总结,写博客,如果你去做了,我想你在关于SpringBoot整合MongoDB这块,应该就没啥问题了!
#4.当然,你应该知道的
你随便写一个接口类(Interface),继承MongoRepository(Interface),然后再将这个接口注入进SpringBoot的容器,你就能有上面那些操作mongoDB数据库中数据的功能了.即,基本的操作MongoDB数据的CRUD功能.