Spring-boot访问MongoDB
1、访问配置信息
package hello; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.authentication.UserCredentials;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate; import com.mongodb.Mongo;
import com.mongodb.MongoClient; @Configuration
public class MongoConfig extends AbstractMongoConfiguration { @Bean
public Mongo mongo() throws Exception {
return new MongoClient();
} @Bean
public MongoTemplate mongoTemplate() throws Exception {
UserCredentials user = new UserCredentials("scott", "tiger");
return new MongoTemplate(mongo(), "test1", user);
} @Override
protected String getDatabaseName() {
// TODO Auto-generated method stub
return "test1";
} }
2、POJO类
package hello;
import org.springframework.data.annotation.Id;
public class Customer {
@Id
private String id;
private String firstName;
private 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);
}
}
3、Repository接口
package hello;
import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface CustomerRepository extends MongoRepository<Customer, String> {
public Customer findByFirstName(String firstName);
public List<Customer> findByLastName(String lastName);
}
4、Application
package hello; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import; @SpringBootApplication
@AutoConfigureBefore
@Import(MongoConfig.class)
public class Application implements CommandLineRunner { @Autowired
private CustomerRepository repository; public static void main(String[] args) {
SpringApplication.run(Application.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);
} } }
Spring-boot访问MongoDB的更多相关文章
- 使用spring boot访问mongodb数据库
一. spring boot中传参的方法 1.自动化配置 spring Boot 对于开发人员最大的好处在于可以对 Spring 应用进行自动配置.Spring Boot 会根据应用中声明的第三方依赖 ...
- spring boot访问数据库
1. Spring JAP 基本使用说明: Spring boot 访问数据库基本上都是通过Spring JPA封装的Bean作为API的,Spring JPA 将访问数据库通过封装,只要你的类实现了 ...
- springboot(十一):Spring boot中mongodb的使用
mongodb是最早热门非关系数据库的之一,使用也比较普遍,一般会用做离线数据分析来使用,放到内网的居多.由于很多公司使用了云服务,服务器默认都开放了外网地址,导致前一阵子大批 MongoDB 因配置 ...
- (转)Spring Boot(十一):Spring Boot 中 MongoDB 的使用
http://www.ityouknow.com/springboot/2017/05/08/spring-boot-mongodb.html MongoDB 是最早热门非关系数据库的之一,使用也比较 ...
- Spring Boot(十一):Spring Boot 中 MongoDB 的使用
MongoDB 是最早热门非关系数据库的之一,使用也比较普遍,一般会用做离线数据分析来使用,放到内网的居多.由于很多公司使用了云服务,服务器默认都开放了外网地址,导致前一阵子大批 MongoDB 因配 ...
- MongoDB最简单的入门教程之四:使用Spring Boot操作MongoDB
Spring Boot 是一个轻量级框架,可以完成基于 Spring 的应用程序的大部分配置工作.Spring Boot的目的是提供一组工具,以便快速构建容易配置的Spring应用程序,省去大量传统S ...
- Spring Boot - 访问外部接口最全总结
Spring Boot - 访问外部接口 在Spring-Boot项目开发中,存在着本模块的代码需要访问外面模块接口,或外部url链接的需求, 比如调用外部的地图API或者天气API. Spring ...
- Spring Boot开发MongoDB应用实践
本文继续上一篇定时任务中提到的邮件服务,简单讲解Spring Boot中如何使用MongoDB进行应用开发. 上文中提到的这个简易邮件系统大致设计思路如下: 1.发送邮件支持同步和异步发送两种 2.邮 ...
- 记一次spring boot中MongoDB Prematurely reached end of stream的异常解决
在spring boot项目中使用了mongodb,当一段时间没有操作mongodb,下次操作mongodb时就会出现异常.异常如下: org.springframework.data.mongodb ...
- 84. Spring Boot集成MongoDB【从零开始学Spring Boot】
至于MongoDB网上有很多相关的资料,所以在这里不进行过多的介绍,我们在这里主要是介绍下如何将mongodb与spring boot结合使用.本节大纲: (1) 准备工作: (2) 新建一个mave ...
随机推荐
- ln (link)命令
ln是linux中又一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个同步的链接.当我们需要在不同的目录,用到相同的文件时,我们不需要在每一个需要的目录下都放一个必须相同的文件,我们只要在 ...
- SoapUI入门
注:需要使用发布的webService接口 我们一般用的是impl接口调用,不大用得上soapUI.看到一份简历上写了使用soapUI做webService测试,想了解一下什么是soapUI soap ...
- JS实现网页下载[转]
function downloadById() { var url = "http://localhost:64610/WorkProcess/ResumeDetail/7&q ...
- 三相异步电动机过载保护及报警PLC控制
一.项目所需设备.工具.材料 见表7-1. 表7-1 项目所需设备.工具.材料 二. 训练内容: 1.项目描述 试设计一电动机过载保护程序,要求电动机过载时能自动停止运转,同时发出10秒钟的声光报 ...
- uvalive 4670 Dominating Patterns
在文本串中找出现次数最多的子串. 思路:AC自动机模板+修改一下print函数. #include<stdio.h> #include<math.h> #include< ...
- python 使用__future__
Python的每个新版本都会增加一些新的功能,或者对原来的功能作一些改动.有些改动是不兼容旧版本的,也就是在当前版本运行正常的代码,到下一个版本运行就可能不正常了. 从Python 2.7到Pytho ...
- ALM11需求和测试覆盖率图解1
覆盖率分析视图 需求覆盖率选项卡
- Keil uCos 2.52 stm32 【worldsing笔记】
1.uCOSii V2.52 a.加了7个可以配置的钩子函数宏 #define OS_TASK_CREATE_HOOK_EN 0 /* 任务创建时调用钩子函数 使能 ...
- unity编辑器的搜索框好特么坑啊,居然不支持*号通配符
上图 t:Scene或者点搜索框旁边的 分类按钮 用*.unity是什么也搜索不出来的
- SQL 正则表达式使模式匹配和数据提取变得更容易
SQL Server 2005 正则表达式使模式匹配和数据提取变得更容易 David Banister 本文讨论: 使用正则表达式进行高效的 SQL 查询 SQL Server 2005 对正则表达式 ...