Spring Boot 中应用Spring data mongdb
摘要
本文主要简单介绍下如何在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
https://github.com/springside/springside4/wiki/Spring-Data-JPA
Spring Boot 中应用Spring data mongdb的更多相关文章
- Spring Boot中使用 Spring Security 构建权限系统
Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下文中配置的Bean,为应用系统提供声明式的安全 ...
- Spring Boot中使用Spring Security进行安全控制
我们在编写Web应用时,经常需要对页面做一些安全控制,比如:对于没有访问权限的用户需要转到登录表单页面.要实现访问控制的方法多种多样,可以通过Aop.拦截器实现,也可以通过框架实现(如:Apache ...
- 【swagger】1.swagger提供开发者文档--简单集成到spring boot中【spring mvc】【spring boot】
swagger提供开发者文档 ======================================================== 作用:想使用swagger的同学,一定是想用它来做前后台 ...
- Spring Boot中集成Spring Security 专题
check to see if spring security is applied that the appropriate resources are permitted: @Configurat ...
- 在Spring Boot中使用Spring Security实现权限控制
丢代码地址 https://gitee.com/a247292980/spring-security 再丢pom.xml <properties> <project.build.so ...
- spring boot中扩展spring mvc 源码分析
首先,确认你是对spring boot的自动配置相关机制是有了解的,如果不了解请看我spring boot相关的源码分析. 通常的使用方法是继承自org.springframework.boot.au ...
- Spring Boot 中使用 Spring Security, OAuth2 跨域问题 (自己挖的坑)
使用 Spring Boot 开发 API 使用 Spring Security + OAuth2 + JWT 鉴权,已经在 Controller 配置允许跨域: @RestController @C ...
- spring-boot-starter-security Spring Boot中集成Spring Security
spring security是springboot支持的权限控制系统. security.basic.authorize-mode 要使用权限控制模式. security.basic.enabled ...
- Spring Boot中使用Spring Security进行安全控制转载来自翟永超
我们在编写Web应用时,经常需要对页面做一些安全控制,比如:对于没有访问权限的用户需要转到登录表单页面.要实现访问控制的方法多种多样,可以通过Aop.拦截器实现,也可以通过框架实现(比如:Apache ...
随机推荐
- [LeetCode] Teemo Attacking 提莫攻击
In LLP world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned ...
- HTTP响应状态解析
100 客户端应当继续发送请求.这个临时响应是用来通知客户端它的部分请求已经被服务器接收,且仍未被拒绝.客户端应当继续发送请求的剩余部分,或者如果请求已经完成,忽略这个响应.服务器必须在请求完成后向客 ...
- error and solve
1.缺少对应的jar包 出错信息: Multiple markers at this line - The type org.springframework.beans.factory.Aware c ...
- Redis常用命令--SortedSet
SortedSet是一个类似于Set的集合数据类型,里面的每个字符串元素都关联到一个score(整数或浮点数),并且总是通过score来进行排序着. 并且可以取得一定范围内的元素. 在Redis中大概 ...
- [HNOI2012]集合选数
题目描述 <集合论与图论>这门课程有一道作业题,要求同学们求出{1, 2, 3, 4, 5}的所有满足以 下条件的子集:若 x 在该子集中,则 2x 和 3x 不能在该子集中. 同学们不喜 ...
- [WC2006]水管局长数据加强版
Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一 ...
- [AH/HNOI2017]抛硬币
题目描述 小 A 和小 B 是一对好朋友,他们经常一起愉快的玩耍.最近小 B 沉迷于**师手游,天天刷本,根本无心搞学习.但是已经入坑了几个月,却一次都没有抽到 SSR,让他非常怀疑人生.勤勉的小 A ...
- [Codeforces]856E - Satellites
传送门 做法:每个卫星分别用连到左边圆与x轴交点的线的斜率和连到右边交点的线旋转90度的斜率可以表示成一个区间,问题转化成支持加/删区间和询问其中两个区间是否有交以及它们的交是否被其他区间包含.我一开 ...
- hdu 5869 区间不同GCD个数(树状数组)
Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K ( ...
- 习题9-3 UVA1629(dp)
Cake Slicing 题意:有一个n行m列的网格上有一些黑点,要求进行切割,使最后每块上只有一个黑点,求最少的刀数 思路:记忆化搜索,枚举每一条边来切,每一次搜索自己所能切割的所有情况取最小值 但 ...