摘要

本文主要简单介绍下如何在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. dev gridControl 自定义绘制列头颜色

    1.添加事件CustomDrawColumnHeader private void gvw1_CustomDrawColumnHeader(object sender, DevExpress.Xtra ...

  2. win10配置Memcached及MVC5测试分布式缓存入门

    win10配置Memcached: 1.安装包下载 2.解压后有: 3.以管理员省份运行cmd: 4.安装:输入cmd命令: E:/memcached-amd64/memcached.exe -d  ...

  3. 机器学习基石:09 Linear Regression

    线性回归假设: 代价函数------均方误差: 最小化样本内代价函数: 只有满秩方阵才有逆矩阵. 线性回归算法流程: 线性回归算法是隐式迭代的. 线性回归算法泛化可能的保证: 根据矩阵的迹的性质:tr ...

  4. vi/vim下tab的长度修改

    默认下的长度是8,如果要想修改可以在根目录下新建'.vimrc'文件 里面的内容是: [root@localhost 09:06 ~]# cat .vimrc set tabstop=4 set sh ...

  5. Struts2--拦截器Interceptor

    拦截器是的我们可以在方法的执行前后定义执行的操作.可以作为一个非常有力的工具在数据验证,属性设置,安全,日志等等方面. 拦截器可以链接起来形成一个拦截器栈.框架会按照拦截器定义的顺序依次调用这些拦截器 ...

  6. [Codeforces 919F]A Game With Numbers

    Description 题库链接 两个人 Van♂ 游戏,每人手上各有 \(8\) 张牌,牌上数字均为 \([0,4]\) 之间的数.每个人在自己的回合选自己手牌中数字不为 \(0\) 的一张与对方手 ...

  7. [Codeforces 919E]Congruence Equation

    Description 题库链接 求满足 \[n\cdot a^n\equiv b \pmod{p}\] 的 \(n\) 的个数, \(1\leq n\leq x\) , \(a,b,p,x\) 均已 ...

  8. [Codeforces 864A]Fair Game

    Description Petya and Vasya decided to play a game. They have n cards (n is an even number). A singl ...

  9. [HNOI 2005]狡猾的商人

    Description 刁姹接到一个任务,为税务部门调查一位商人的账本,看看账本是不是伪造的.账本上记录了n个月以来的收入情况,其中第i 个月的收入额为Ai(i=1,2,3...n-1,n), .当 ...

  10. 伊布(ib)

    [问题描述]ib 被困在了一个美术馆里,她需要收集美术馆内的每种颜料才能获得逃出美术馆的钥匙美术馆由 n*m 的房间构成,每个房间里有一种颜料,解锁进入后就可以收集.有的房间不能解锁,如果解锁的话会直 ...