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 ...
随机推荐
- [转] Attach、Detach和DeleteObject
原文:Attach.Detach和DeleteObject,想飞的梦想 1.CWnd Attatch和Detach的关系 首先,要明白Windows对象和MFC对象的区别. MFC对象实际上并没有把整 ...
- Javascript 知识点简介
如何在HTML中引入JS? 所有重定向的HTML标签内都可以嵌入javascript代码. 浮点数不要用 == 来进行判断 var num=0; for(var i=0;i<10;i++) ...
- 【Python】一个python实例:给重要的文件创建备份.摘自crossin-python简明教程
问题:写一个可以为所有重要文件创建备份的程序 考虑:源路径和目标路径各是什么;所有重要文件-有哪些;备份文件格式是什么;定期备份的话,备份文件名称如何规定等等.(ps,我自己只想到一个路径和名称) 程 ...
- 【Tcpcopy】离线回放功能
最近因调试问题,需要一直进行tcpcopy,拿有问题的包进行测试.决定使用tcpcopy对录制脚本进行回放,以下为我操作的具体步骤.主要是三块 1 下载安装具有离线回放功能的tcpcopy 2 使用t ...
- faplayer编译配置经验
最近在做在线m3u8类格式的视频直播应用, 在获取m3u8的文件之后,如果采用Android系统播放器来播,会有各种各样的问题,容易卡死.不连续,也不能自定义一些选项.查找资料以后,决定采用fapla ...
- Magento 切换成中文后没有数据信息解决办法
进入后台的, 选择CMS---pages, 看到这个么? 如果这里显示的不是所有商店界面的话, 就点击进去,然后选择所有商店界面: 如下所示: 再点击保存就可以了.
- SQL注入攻击及防范
一.什么是SQL注入1.SQL注入的定义 SQL注入(SQL Injection) 利用了程序中的SQL的漏洞,进行攻击的方法. 2.SQL注入举例 1)利用SQL语法错误获取数据库表的结构 ...
- hadoop cdh 4.5的安装配置
春节前用的shark,是从github下载的源码,自己编译.shark的master源码仅支持hive 0.9,支持hive 0.11的shark只是个分支,不稳定,官方没有发布release版,在使 ...
- 也谈SSO,一个简单实用的单点登录Demo
关于SSO(单点登录),百度百科解释如下 : “SSO英文全称Single Sign On,单点登录.SSO是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统.它包括可以将这次主要 ...
- Handlebar
1.Handlebar中文网: http://www.ghostchina.com/introducing-the-handlebars-js-templating-engine/ 2.http:// ...