摘要

本文主要简单介绍下如何在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. 【Java入门提高篇】Day15 Java泛型再探——泛型通配符及上下边界

    上篇文章中介绍了泛型是什么,为什么要使用泛型以及如何使用泛型,相信大家对泛型有了一个基本的了解,本篇将继续讲解泛型的使用,让你对泛型有一个更好的掌握和更深入的认识. 上篇中介绍完泛型之后,是不是觉得泛 ...

  2. JSONObject和JSONArray区别及基本用法

    一.JSONObject和JSONArray的数据表示形式 JSONObject的数据是用 {  } 来表示的, 例如:   { "id" : "123", & ...

  3. react组件开发规范(一)

    这是通过修改项目运行在Google上时的警告,总结的的部分react组件开发规范: (1)编写组件时,一定要写PropTypes,切莫为了省事儿而不写! 如果一个Props不是required,一定在 ...

  4. tooltip.css-2.0文档

     tooltip.css 纯CSS鼠标提示工具. v. 2.0.0 更新日期:2018.4.12 预览DEMO.  安装: 只需在页面中引入"tooltip.css"或" ...

  5. [HDU 2036]改革春风吹满地

    Description “ 改革春风吹满地,不会AC没关系;实在不行回老家,还有一亩三分地.谢谢!(乐队奏乐)”话说部分学生心态极好,每天就知道游戏,这次考试如此简单的题目,也是云里雾里,而且,还竟然 ...

  6. [NOIp 2012]国王游戏

    Description 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n 位大臣排成一排,国 ...

  7. codefroces 612E Square Root of Permutation

    A permutation of length n is an array containing each integer from 1 to n exactly once. For example, ...

  8. [HNOI2013]数列

    题目描述 小T最近在学着买股票,他得到内部消息:F公司的股票将会疯涨.股票每天的价格已知是正整数,并且由于客观上的原因,最多只能为N.在疯涨的K天中小T观察到:除第一天外每天的股价都比前一天高,且高出 ...

  9. [HNOI2009]有趣的数列

    题目描述 我们称一个长度为2n的数列是有趣的,当且仅当该数列满足以下三个条件: (1)它是从1到2n共2n个整数的一个排列{ai}: (2)所有的奇数项满足a1<a3<...<a2n ...

  10. 【NOIP2011TG】solution

    老师最近叫我把NOIPTG的题目给刷掉,于是就开始刷吧= = 链接:https://www.luogu.org/problem/lists?name=&orderitem=pid&ta ...