本篇文章给大家分享的是有关使用MongoDB如何对Spring进行整合,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
添加依赖
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>1.10.3.RELEASE</version></dependency>
其余Spring相关的忽略
Spring的配置applicationContext-mongo.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation=" http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd"> <context:property-placeholder ignore-unresolvable="true" location="classpath:/mongodb.properties"/> <mongo:mongo-client id="mongoClient" host="${mongo.host}" port="${mongo.port}"> <!-- credentials="${mongo.user}:${mongo.pwd}@${mongo.defaultDbName}"--> </mongo:mongo-client> <mongo:db-factory id="mongoDbFactory" dbname="${mongo.database}" mongo-ref="mongoClient"/> <!-- 默认Mongodb类型映射 --> <bean id="defaultMongoTypeMapper" class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper"> <constructor-arg name="typeKey"> <!-- 这里设置为空,可以把 spring data mongodb 多余保存的_class字段去掉 --> <null/> </constructor-arg> </bean> <mongo:repositories base-package="com.critc.mongo"/> <!-- 自动扫描以下包的有Doucment注解的类 --> <mongo:mapping-converter id="mappingConverter" base-package="com.critc.mongo.model" type-mapper-ref="defaultMongoTypeMapper"> </mongo:mapping-converter> <!-- Mongodb的模板 --> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/> <constructor-arg name="mongoConverter" ref="mappingConverter"/> </bean></beans>
mongo:mongo-client
是定义MongoDB的客户端连接,需要host和port参数,如果需要账号密码的话,需要增加credentials配置
mongo:db-factory
配置连接工厂,指定具体的连接数据库,本例默认为test
defaultMongoTypeMapper
默认Mongodb类型映射
mongo:mapping-converterMongoDB
的实体映射
mongoTemplate
这是最主要的,定义模板类,依赖连接工厂和实体映射
这里举一个article的增删改查的例子。
相关实体Article.java
@Document(collection = "article_info")public class Article { @Id private String id;//id @Field("title") private String title;//标题 @Field("url") private String url;//链接 @Field("author") private String author;//作者 @Field("tags") private List<String> tags;//tag 标签 @Field("visit_count") private Long visitCount;//访问次数 @Field("add_time") private Date addTime;//添加时间// get set方法省略
@Document(collection = "article_info")
这个注解和Hibernate的注解Entiry
非常相似,就是定义一个文档,对象MongoDB存储的Collection的名称是article_info
@Id指该字段是主键,不能缺少
@Field("add_time")指该字段映射MongoDB的实际字段,如果一致可以省略
ArticleRepository实际访问接口
@Repository("ArticleRepository")public interface ArticleRepository extends PagingAndSortingRepository<Article, String> { //分页查询 public Page<Article> findAll(Pageable pageable); //根据author查询 public List<Article> findByAuthor(String author); //根据作者和标题查询 public List<Article> findByAuthorAndTitle(String author, String title); //忽略参数大小写 public List<Article> findByAuthorIgnoreCase(String author); //忽略所有参数大小写 public List<Article> findByAuthorAndTitleAllIgnoreCase(String author, String title); //排序 public List<Article> findByAuthorOrderByVisitCountDesc(String author); public List<Article> findByAuthorOrderByVisitCountAsc(String author); //自带排序条件 public List<Article> findByAuthor(String author, Sort sort);}
Spring的data repository封装了一套增删改查的方法,就和JPA实现的一样,ArticleRepository
继承PagingAndSortingRepository
,就集成了常用的增删改查方法,比如save、findOne、exists、findAll、delete等等,可以采用默认实现方式来完成常用的增删改查操作。
测试上述各个方法ArticleRepositoryTest.java
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = {"classpath*:applicationContext-mongo.xml"})public class ArticleRepositoryTest { @Autowired private ArticleRepository articleRepository; @Test public void add() { //增加一条记录 Article article = new Article(); article.setId("1"); article.setTitle("MongoTemplate的基本使用"); article.setAuthor("kcy"); article.setUrl("http://jianshu.com/"); article.setTags(Arrays.asList("java", "mongodb", "spring")); article.setVisitCount(0L); article.setAddTime(new Date()); articleRepository.save(article); //批量添加 List<Article> articles = new ArrayList<>(10); for (int i = 0; i < 10; i++) { Article article2 = new Article(); article2.setId(String.valueOf(i + 1)); article2.setTitle("MongoTemplate的基本使用"); article2.setAuthor("kcy"); article2.setUrl("http://jianshu.com" + i); article2.setTags(Arrays.asList("java", "mongodb", "spring")); article2.setVisitCount(0L); article2.setAddTime(new Date()); articles.add(article2); } articleRepository.save(articles); } @Test public void update() { Article article = articleRepository.findOne("1"); article.setVisitCount(article.getVisitCount() + 1); articleRepository.save(article); } @Test public void batchUpdate() { List<Article> articles = articleRepository.findByAuthor("kcy"); articles.forEach(article -> { article.setAuthor("kcy2"); }); articleRepository.save(articles); } @Test public void delete() { Article article = articleRepository.findOne("10"); articleRepository.delete(article); } @Test public void batchDelete() { List<Article> articles = articleRepository.findByAuthor("kcy2"); articleRepository.delete(articles); } @Test public void findAll() { Iterable<Article> articles = articleRepository.findAll(); articles.forEach(article -> { System.out.println(article.toString()); }); } @Test public void findByAuthor() { List<Article> articles = articleRepository.findByAuthor("kcy"); articles.forEach(article -> { System.out.println(article.toString()); }); } @Test public void findByAuthorAndTitle() { List<Article> articles = articleRepository.findByAuthorAndTitle("kcy", "MongoTemplate的基本使用"); articles.forEach(article -> { System.out.println(article.toString()); }); } @Test public void findByAuthorIgnoreCase() { List<Article> articles = articleRepository.findByAuthorIgnoreCase("JASON"); articles.forEach(article -> { System.out.println(article.getId()); }); } @Test public void findByAuthorAndTitleAllIgnoreCase() { List<Article> articles = articleRepository.findByAuthorAndTitleAllIgnoreCase("KCY", "MONGOTEMPLATE的基本使用"); articles.forEach(article -> { System.out.println(article.toString()); }); } @Test public void findByAuthorOrderByVisitCountDesc() { List<Article> articles = articleRepository.findByAuthorOrderByVisitCountDesc("kcy"); articles.forEach(article -> { System.out.println(article.toString()); }); } @Test public void findByAuthorOrderByVisitCountAsc() { List<Article> articles = articleRepository.findByAuthorOrderByVisitCountAsc("kcy"); articles.forEach(article -> { System.out.println(article.toString()); }); } @Test public void findByAuthorBySort() { List<Article> articles = articleRepository.findByAuthor("kcy", new Sort(Direction.ASC, "VisitCount")); articles.forEach(article -> { System.out.println(article.toString()); }); } @Test public void findByPage() { int page = 1; int size = 2; Pageable pageable = new PageRequest(page, size, new Sort(Direction.ASC, "VisitCount")); Page<Article> pageInfo = articleRepository.findAll(pageable); //总数量 System.out.println(pageInfo.getTotalElements()); //总页数 System.out.println(pageInfo.getTotalPages()); for (Article article : pageInfo.getContent()) { System.out.println(article.toString()); } }}
上面一段代码较长,基本上MongoDB常用的各种例子都讲清楚了,比如增加、批量增加、修改、删除、按id查找、按标题查询、分页等等。
以上就是使用MongoDB如何对Spring进行整合,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注编程网行业资讯频道。