摘要

本文主要简单介绍下如何在Spring Boot 项目中使用Spring data mongdb.没有深入探究,仅供入门参考。

文末有代码链接

准备

安装mongodb

需要连接mongodb,所以需要提前安装mongodb.在本地安装

安装文档见官网 install mongodb

安装完mongodb后配置环境变量。创建目录“C:\data\db”作为mongo 数据存储的默认文件

注意本篇文章代码连接的mongo3.2.

在window7 64 bit 上安装mongo,可以选择Windows Server 2008 R2 64-bit

例程

配置数据库连接

在main/resource/ 下添加application.yml文件

spring:
        data:
             mongodb:
               host: localhost
               port: 27017
               database: test

Spring boot 会检测MongoDb中是否存在db test。如果没有会创建

model

创建一个person document 类。作为Mongodb的document 映射。加上@Document 注解

@Document(collection="personDocument")
public class PersonDocument {
        @Id
        String id;
        String name;
        String description;
       } 

repository

repository 就是DAO,将域类型和它的ID类型作为类型参数。Spring Data JPA 在JPA(Java Persistence Api)又做了一层封装。因此我们可以看到,只需要编写repository 接口即可。

@Repository
public interface PersonRepository extends MongoRepository<PersonDocument,String>{
    PersonDocument findByName(String name);
}

这些接口会通过Proxy来自动实现。所以接口里面的方法名不是随意写。需要遵循一定的规范。像上面的 “findByName”是查询语句,Name必须是PersonDocument中的字段,否则会报错。

And 关键字表示的就是SQL中的and

LessThan 关键词表示的就是SQL 中的 <

等等,还有许多基本的SQL实现

service

service 层和之前的写法没什么不同,调用repository,实现业务逻辑

@EnableMongoRepositories(basePackages = "com.example.dao.repository")
@Service
public class PersonServiceImpl  implements PersonService{

    @Autowired
    private PersonRepository personRepo;

    public PersonDocument getPersonDocumentByName(String name) {
        PersonDocument doc = personRepo.findByName(name);
        return doc;
    }

    public PersonDocument create(PersonDocument personDoc) {
        personDoc = personRepo.save(personDoc);
        return personDoc;
    }

}

test

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

    @Autowired
    private PersonService personService;

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void run(String... arg0) throws Exception {
        PersonDocument doc = new PersonDocument("tom", "student");
        personService.create(doc);
        PersonDocument doc2 = personService.getPersonDocumentByName("tom");
        System.out.println("result:" + doc2.getName());

    }

完整代码 github source code

Issues

Q1.连接mongodb 正常,数据库中也有populate数据。但是使用spring-data-mongo repository 查询不出数据

A1.首先试了下findAll 方法。发现没有查询出数据。最后检查了下application 配置文件。这个文件没有报错。配置的Mongo 连接参数也对。要不然也连接不上。虽然文件没有报错。但是有warning.参考了标准配置后解决了问题

参考

http://docs.spring.io/spring-data/mongodb/docs/1.2.x/reference/html/mongo.repositories.html

http://docs.spring.io/spring-data/mongodb/docs/1.4.0.RELEASE/reference/html/mongo.core.html#mongodb-getting-started

https://github.com/springside/springside4/wiki/Spring-Data-JPA

Spring Boot 中应用Spring data mongdb的更多相关文章

  1. Spring Boot中使用 Spring Security 构建权限系统

    Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下文中配置的Bean,为应用系统提供声明式的安全 ...

  2. Spring Boot中使用Spring Security进行安全控制

    我们在编写Web应用时,经常需要对页面做一些安全控制,比如:对于没有访问权限的用户需要转到登录表单页面.要实现访问控制的方法多种多样,可以通过Aop.拦截器实现,也可以通过框架实现(如:Apache ...

  3. 【swagger】1.swagger提供开发者文档--简单集成到spring boot中【spring mvc】【spring boot】

    swagger提供开发者文档 ======================================================== 作用:想使用swagger的同学,一定是想用它来做前后台 ...

  4. Spring Boot中集成Spring Security 专题

    check to see if spring security is applied that the appropriate resources are permitted: @Configurat ...

  5. 在Spring Boot中使用Spring Security实现权限控制

    丢代码地址 https://gitee.com/a247292980/spring-security 再丢pom.xml <properties> <project.build.so ...

  6. spring boot中扩展spring mvc 源码分析

    首先,确认你是对spring boot的自动配置相关机制是有了解的,如果不了解请看我spring boot相关的源码分析. 通常的使用方法是继承自org.springframework.boot.au ...

  7. Spring Boot 中使用 Spring Security, OAuth2 跨域问题 (自己挖的坑)

    使用 Spring Boot 开发 API 使用 Spring Security + OAuth2 + JWT 鉴权,已经在 Controller 配置允许跨域: @RestController @C ...

  8. spring-boot-starter-security Spring Boot中集成Spring Security

    spring security是springboot支持的权限控制系统. security.basic.authorize-mode 要使用权限控制模式. security.basic.enabled ...

  9. Spring Boot中使用Spring Security进行安全控制转载来自翟永超

    我们在编写Web应用时,经常需要对页面做一些安全控制,比如:对于没有访问权限的用户需要转到登录表单页面.要实现访问控制的方法多种多样,可以通过Aop.拦截器实现,也可以通过框架实现(比如:Apache ...

随机推荐

  1. [LeetCode] The Maze 迷宫

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  2. codevs 1069 关押罪犯

    提交地址:http://codevs.cn/problem/1069/ 1069 关押罪犯 2010年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻 ...

  3. [BZOJ 4919]大根堆

    Description 题库链接 给定一棵 \(n\) 个节点的有根树,每个点有一个权值 \(val_i\) .你需要选择尽可能多的节点,使得:对于任意两个点 \(i,j\) ,如果 \(i\) 在树 ...

  4. [HNOI 2016]大数

    Description 题库链接 给你一个长度为 \(n\) ,可含前导零的大数,以及一个质数 \(p\) . \(m\) 次询问,每次询问你一个大数的子区间 \([l,r]\) ,求出子区间中有多少 ...

  5. [SHOI2008]汉诺塔

    Description 汉诺塔由三根柱子(分别用A B C表示)和n个大小互不相同的空心盘子组成.一开始n个盘子都摞在柱子A上, 大的在下面,小的在上面,形成了一个塔状的锥形体. 对汉诺塔的一次合法的 ...

  6. [POI2006]OKR-Periods of Words

    题目描述 一个串是有限个小写字符的序列,特别的,一个空序列也可以是一个串. 一个串P是串A的前缀, 当且仅当存在串B, 使得 A = PB. 如果 P A 并且 P 不是一个空串,那么我们说 P 是A ...

  7. ●线段树的三个题(poj 3225,hdu 1542,hdu 1828)

    ●poj 3225 Help with Intervals(线段树区间问题) ○赘述题目 给出以下集合操作: 然后有初始的一个空集S,和以下题目给出的操作指令,并输入指令: 要求进行指令操作后,按格式 ...

  8. ●BZOJ 2393 Cirno的完美算数教室

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2393 题解: 容斥原理,暴力搜索,剪枝...和 [Scoi2010 幸运数字] 一样的(只是 ...

  9. hdu 1754 线段树(Max+单点修改)

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  10. [BZOJ]3672 购票(Noi2014)

    革命尚未成功,同志还需努力. Description 今年夏天,NOI在SZ市迎来了她30周岁的生日.来自全国 n 个城市的OIer们都会从各地出发,到SZ市参加这次盛会. 全国的城市构成了一棵以SZ ...