SpringBoot非官方教程 | 第八篇:springboot整合mongodb
转载请标明出处:
原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot8-mongodb/
本文出自方志朋的博客
这篇文章主要介绍springboot如何整合mongodb。
准备工作
- 安装 MongoDB
- jdk 1.8
- maven 3.0
- idea
环境依赖
在pom文件引入spring-boot-starter-data-mongodb依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
##数据源配置
如果mongodb端口是默认端口,并且没有设置密码,可不配置,sprinboot会开启默认的。
spring.data.mongodb.uri=mongodb://localhost:27017/springboot-db
mongodb设置了密码,这样配置:
spring.data.mongodb.uri=mongodb://name:pass@localhost:27017/dbname
定义一个简单的实体
mongodb
package com.forezp.entity;
import org.springframework.data.annotation.Id;
public class Customer {
@Id
public String id;
public String firstName;
public String lastName;
public Customer() {}
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return String.format(
"Customer[id=%s, firstName='%s', lastName='%s']",
id, firstName, lastName);
}
}
数据操作dao层
public interface CustomerRepository extends MongoRepository<Customer, String> {
public Customer findByFirstName(String firstName);
public List<Customer> findByLastName(String lastName);
}
写一个接口,继承MongoRepository,这个接口有了几本的CURD的功能。如果你想自定义一些查询,比如根据firstName来查询,获取根据lastName来查询,只需要定义一个方法即可。注意firstName严格按照存入的mongodb的字段对应。在典型的java的应用程序,写这样一个接口的方法,需要自己实现,但是在springboot中,你只需要按照格式写一个接口名和对应的参数就可以了,因为springboot已经帮你实现了。
测试
@SpringBootApplication
public class SpringbootMongodbApplication implements CommandLineRunner {
@Autowired
private CustomerRepository repository;
public static void main(String[] args) {
SpringApplication.run(SpringbootMongodbApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
repository.deleteAll();
// save a couple of customers
repository.save(new Customer("Alice", "Smith"));
repository.save(new Customer("Bob", "Smith"));
// fetch all customers
System.out.println("Customers found with findAll():");
System.out.println("-------------------------------");
for (Customer customer : repository.findAll()) {
System.out.println(customer);
}
System.out.println();
// fetch an individual customer
System.out.println("Customer found with findByFirstName('Alice'):");
System.out.println("--------------------------------");
System.out.println(repository.findByFirstName("Alice"));
System.out.println("Customers found with findByLastName('Smith'):");
System.out.println("--------------------------------");
for (Customer customer : repository.findByLastName("Smith")) {
System.out.println(customer);
}
}
在springboot的应用程序,加入测试代码。启动程序,控制台打印了:
Customers found with findAll():
-------------------------------
Customer[id=58f880f589ffb696b8a6077e, firstName=‘Alice’, lastName=‘Smith’]
Customer[id=58f880f589ffb696b8a6077f, firstName=‘Bob’, lastName=‘Smith’]
Customer found with findByFirstName(‘Alice’):
--------------------------------
Customer[id=58f880f589ffb696b8a6077e, firstName=‘Alice’, lastName=‘Smith’]
Customers found with findByLastName(‘Smith’):
--------------------------------
Customer[id=58f880f589ffb696b8a6077e, firstName=‘Alice’, lastName=‘Smith’]
Customer[id=58f880f589ffb696b8a6077f, firstName=‘Bob’, lastName=‘Smith’]
测试通过。
源码下载:https://github.com/forezp/SpringBootLearning
参考资料
扫码关注公众号有惊喜
(转载本站文章请注明作者和出处 方志朋的博客)
SpringBoot非官方教程 | 第八篇:springboot整合mongodb的更多相关文章
- SpringBoot进阶教程 | 第四篇:整合Mybatis实现多数据源
这篇文章主要介绍,通过Spring Boot整合Mybatis后如何实现在一个工程中实现多数据源.同时可实现读写分离. 准备工作 环境: windows jdk 8 maven 3.0 IDEA 创建 ...
- (转)SpringBoot非官方教程 | 第七篇:springboot开启声明式事务
springboot开启事务很简单,只需要一个注解@Transactional 就可以了.因为在springboot中已经默认对jpa.jdbc.mybatis开启了事事务,引入它们依赖的时候,事物就 ...
- SpringBoot非官方教程 | 第二十三篇: 异步方法
转载请标明出处: 原文首发于https://www.fangzhipeng.com/springboot/2017/07/11/springboot-ansy/ 本文出自方志朋的博客 这篇文章主要介绍 ...
- (转)SpringBoot非官方教程 | 第三篇:SpringBoot用JdbcTemplates访问Mysql
本文介绍springboot通过jdbc访问关系型MySQL,通过spring的JdbcTemplate去访问. 准备工作 jdk 1.8 maven 3.0 idea mysql 初始化mysql: ...
- SpringBoot非官方教程 | 第六篇:springboot整合mybatis
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot-mybatis/ 本文出自方志朋的博客 本文主要 ...
- SpringBoot非官方教程 | 第五篇:springboot整合 beatlsql
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot5-beatlsql/ 本文出自方志朋的博客 Be ...
- SpringBoot非官方教程 | 第四篇:SpringBoot 整合JPA
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot4-jpaJ/ 本文出自方志朋的博客 JPA全称J ...
- SpringBoot非官方教程 | 第七篇:springboot开启声明式事务
转载请标明出处: http://blog.csdn.net/forezp/article/details/70833629 本文出自方志朋的博客 springboot开启事务很简单,只需要一个注解@T ...
- SpringBoot非官方教程 | 第二十篇: 处理表单提交
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot-form/ 本文出自方志朋的博客 这篇文件主要介 ...
随机推荐
- 【Linux相识相知】rpm包管理
我们日常在使用Windows的时候,如果要安装某个软件,最简单的方法就是在程序包的官网直接下载软件包到本地,一般是以.exe或者.msi格式的文件,然后一直下一步下一步就能安装成功了,但是在使用lin ...
- linux防火墙与端口设置
1.编辑iptables文件 # sudo vi /etc/sysconfig/iptables 添加如下一行 -A INPUT -p tcp -m state --state NEW -m tcp ...
- 1.net平台
.net/dotnet:一般指的是.Net Framework框架,是一种平台,一种技术. .net Framewoek框架是.net平台不可缺少的一部分,它提供了一个稳定的运行环境来保证.net ...
- c#解析json字符串处理(最清晰易懂的方法)
注:博客迁移到csdn,本文最新地址:https://blog.csdn.net/sajiazaici/article/details/77647625 以下为原文 本文是全网第二简单的方法,因为我女 ...
- Java内部类详解 2
Java内部类详解 说起内部类这个词,想必很多人都不陌生,但是又会觉得不熟悉.原因是平时编写代码时可能用到的场景不多,用得最多的是在有事件监听的情况下,并且即使用到也很少去总结内部类的用法.今天我们就 ...
- [转]滚动视差?CSS 不在话下/background attachment
何为滚动视差 视差滚动(Parallax Scrolling)是指让多层背景以不同的速度移动,形成立体的运动效果,带来非常出色的视觉体验. 作为网页设计的热点趋势,越来越多的网站应用了这项技术. ...
- Java入门到精通——调错篇之Eclipse Java compiler level dose not match the version of the installed Java project
一.错误现象. java项目显示红色,并且类中引用包中会报红色错误,在Eclipse下面显示下面错误提示如图: 二.错误原因. 通过字面意思一看就很明白java的版本不对. 三.解决办法. 3.1右键 ...
- .NET开源工作流RoadFlow-流程运行-菜单配置
经过流程设计和表单设计后,一个流程就设置完成了,下面说说怎么让设计好的流程运行起来吧. 流程和表单设计完成发布后都在应用程序库里,我们只要将流程在角色应用中配置给相应的用户即可让流程选择了. 在系统管 ...
- Android 第三方类库简单使用之EventBus
Android 第三方类库之EventBus 1 PS 工欲善其事必先利其器. Eventbus也是一款在开发中常用的利器 这篇也对EventBus的简单介绍和使用,与之前个xutils介绍的级别一样 ...
- python闭包&装饰器&偏函数
什么是闭包? 首先还得从基本概念说起,什么是闭包呢?来看下维基上的解释: 在计算机科学中,闭包(Closure)是词法闭包(Lexical Closure)的简称,是引用了自由变量的函数.这个被引用的 ...