SpringBoot整合MongoDB数据库方式(一)

#1.结构图

maven配置这些就不赘述了,可以看我前一些天的博客

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功能.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值