- 浏览: 60022 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (93)
- java (3)
- ios (9)
- wp (15)
- android (0)
- js (1)
- 服务器 (0)
- db (0)
- linux (1)
- python (0)
- xcode (0)
- ide (2)
- maven (0)
- spring (0)
- sql (0)
- 第三方 (1)
- nexus (0)
- nginx (11)
- tomcat (0)
- jenkins (0)
- zookeeper (1)
- git (1)
- svn (0)
- uml (0)
- redis (4)
- activemq (1)
- flume (0)
- kafka (0)
- mysql (1)
- memcached (0)
- mybatis (0)
- mac (0)
- mongo (1)
- docker (6)
- cache (0)
- jvm (0)
- markdown (0)
- springboot (24)
- mycat (3)
- LTS (3)
- 运维 (0)
- opts (1)
- netty (1)
- tcc (0)
- ffmpeg (2)
- 直播 (6)
- cxf (0)
- nodejs (0)
- storm (0)
- elasticjob (0)
- php (0)
最新评论
pom.xml
=========================================
<!-- Spring Boot mongodb 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
=========================================
application.properties
=========================================
mongo.host=host
mongo.port=port
mongo.username=username
mongo.password=password
mongo.database=db
mongo.connections-per-host=10
mongo.threads-allowed-to-block-for-connection-multiplier=5
mongo.connect-timeout=60000
mongo.max-wait-time=120000
mongo.auto-connect-retry=true
mongo.socket-keep-alive=false
mongo.socket-timeout=0
=========================================
MongodbConfig.java
=========================================
@Configuration
public class MongodbConfig {
@Value("${mongo.host}")
private String host;
@Value("${mongo.port}")
private int port;
@Value("${mongo.username}")
private String username;
@Value("${mongo.database}")
private String db;
@Value("${mongo.password}")
private char[] password;
@Value("${mongo.connections-per-host}")
private int connectionsPerHost;
@Value("${mongo.threads-allowed-to-block-for-connection-multiplier}")
private int threadsAllowedToBlockForConnectionMultiplier;
@Value("${mongo.connect-timeout}")
private int connectTimeout;
@Value("${mongo.max-wait-time}")
private int maxWaitTime;
@Value("${mongo.socket-keep-alive}")
private boolean socketKeepAlive;
@Value("${mongo.socket-timeout}")
private int socketTimeout;
//<mongo:mongo-client
// id="dapMongo"
// host="#{mongo_properties['mongo.host']}"
// port="#{mongo_properties['mongo.port']}"
// credentials="#{mongo_properties['mongo.username']}:#{mongo_properties['mongo.password']}@#{mongo_properties['mongo.database']}">
//
// <mongo:client-options
// connections-per-host="#{mongo_properties['mongo.connections-per-host']}"
// threads-allowed-to-block-for-connection-multiplier="#{mongo_properties['mongo.threads-allowed-to-block-for-connection-multiplier']}"
// connect-timeout="#{mongo_properties['mongo.connect-timeout']}"
// max-wait-time="#{mongo_properties['mongo.max-wait-time']}"
// socket-keep-alive="#{mongo_properties['mongo.socket-keep-alive']}"
// socket-timeout="#{mongo_properties['mongo.socket-timeout']}"/>
//
// </mongo:mongo-client>
@Bean(name = "mongoClient")
public MongoClient mongoClient(){
ServerAddress serverAddress = new ServerAddress(host, port);
List<MongoCredential> credentials = new LinkedList<MongoCredential>();
MongoCredential credential = MongoCredential.createCredential(username, db, password);
credentials.add(credential);
MongoClient mongoClient = new MongoClient(serverAddress, credentials, mongoOperations());
return mongoClient;
}
private MongoClientOptions mongoOperations(){
MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
builder.connectionsPerHost(connectionsPerHost);
builder.threadsAllowedToBlockForConnectionMultiplier(threadsAllowedToBlockForConnectionMultiplier);
builder.connectTimeout(connectTimeout);
builder.maxWaitTime(maxWaitTime);
builder.socketKeepAlive(socketKeepAlive);
builder.socketTimeout(socketTimeout);
MongoClientOptions mongoOperations = builder.build();
return mongoOperations;
}
// <mongo:db-factory id="dapFactory" mongo-ref="dapMongo"
// dbname="#{mongo_properties['mongo.database']}"/>
@Bean(name = "mongoDbFactory")
public MongoDbFactory mongoDbFactory(@Qualifier("mongoClient")MongoClient mongoClient,
@Value("${mongo.database}")String database){
MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, database);
return mongoDbFactory;
}
// <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
// <constructor-arg name="mongoDbFactory" ref="dapFactory"/>
// </bean>
@Bean(name = "db-mongoTemplate")
public MongoTemplate mongoTemplate(@Qualifier("mongoDbFactory")MongoDbFactory mongoDbFactory){
MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory);
return mongoTemplate;
}
}
=========================================
CityServiceImpl.java
=========================================
@Service
public class CityServiceImpl implements CityService {
@Resource(name = "db-mongoTemplate")
private MongoTemplate mongoTemplate;
@Override
public void save(City city) {
mongoTemplate.save(city);
}
@Override
public City findByName(String cityName) {
Query query = new Query(Criteria.where("cityName").is(cityName));
City city = mongoTemplate.findOne(query, City.class);
return city;
}
@Override
public void updateCity(City city) {
Query query = new Query(Criteria.where("id").is(city.getId()));
Update update = new Update().set("cityName", city.getCityName()).set("description", city.getDescription());
//更新查询返回结果集的第一条
mongoTemplate.updateFirst(query, update, City.class);
//更新查询返回结果集的所有
// mongoTemplate.updateMulti(query,update,UserEntity.class);
}
@Override
public void deleteById(Long id) {
Query query = new Query(Criteria.where("id").is(id));
mongoTemplate.remove(query, City.class);
}
}
=========================================
TestMongodb.java
=========================================
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class TestMongodb {
@Autowired
CityService cityService;
@Test
public void testSave(){
City city = new City();
city.setId(1l);
city.setCityName("test");
city.setDescription("hello");
city.setProvinceId(1l);
cityService.save(city);
}
}
=========================================
<!-- Spring Boot mongodb 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
=========================================
application.properties
=========================================
mongo.host=host
mongo.port=port
mongo.username=username
mongo.password=password
mongo.database=db
mongo.connections-per-host=10
mongo.threads-allowed-to-block-for-connection-multiplier=5
mongo.connect-timeout=60000
mongo.max-wait-time=120000
mongo.auto-connect-retry=true
mongo.socket-keep-alive=false
mongo.socket-timeout=0
=========================================
MongodbConfig.java
=========================================
@Configuration
public class MongodbConfig {
@Value("${mongo.host}")
private String host;
@Value("${mongo.port}")
private int port;
@Value("${mongo.username}")
private String username;
@Value("${mongo.database}")
private String db;
@Value("${mongo.password}")
private char[] password;
@Value("${mongo.connections-per-host}")
private int connectionsPerHost;
@Value("${mongo.threads-allowed-to-block-for-connection-multiplier}")
private int threadsAllowedToBlockForConnectionMultiplier;
@Value("${mongo.connect-timeout}")
private int connectTimeout;
@Value("${mongo.max-wait-time}")
private int maxWaitTime;
@Value("${mongo.socket-keep-alive}")
private boolean socketKeepAlive;
@Value("${mongo.socket-timeout}")
private int socketTimeout;
//<mongo:mongo-client
// id="dapMongo"
// host="#{mongo_properties['mongo.host']}"
// port="#{mongo_properties['mongo.port']}"
// credentials="#{mongo_properties['mongo.username']}:#{mongo_properties['mongo.password']}@#{mongo_properties['mongo.database']}">
//
// <mongo:client-options
// connections-per-host="#{mongo_properties['mongo.connections-per-host']}"
// threads-allowed-to-block-for-connection-multiplier="#{mongo_properties['mongo.threads-allowed-to-block-for-connection-multiplier']}"
// connect-timeout="#{mongo_properties['mongo.connect-timeout']}"
// max-wait-time="#{mongo_properties['mongo.max-wait-time']}"
// socket-keep-alive="#{mongo_properties['mongo.socket-keep-alive']}"
// socket-timeout="#{mongo_properties['mongo.socket-timeout']}"/>
//
// </mongo:mongo-client>
@Bean(name = "mongoClient")
public MongoClient mongoClient(){
ServerAddress serverAddress = new ServerAddress(host, port);
List<MongoCredential> credentials = new LinkedList<MongoCredential>();
MongoCredential credential = MongoCredential.createCredential(username, db, password);
credentials.add(credential);
MongoClient mongoClient = new MongoClient(serverAddress, credentials, mongoOperations());
return mongoClient;
}
private MongoClientOptions mongoOperations(){
MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
builder.connectionsPerHost(connectionsPerHost);
builder.threadsAllowedToBlockForConnectionMultiplier(threadsAllowedToBlockForConnectionMultiplier);
builder.connectTimeout(connectTimeout);
builder.maxWaitTime(maxWaitTime);
builder.socketKeepAlive(socketKeepAlive);
builder.socketTimeout(socketTimeout);
MongoClientOptions mongoOperations = builder.build();
return mongoOperations;
}
// <mongo:db-factory id="dapFactory" mongo-ref="dapMongo"
// dbname="#{mongo_properties['mongo.database']}"/>
@Bean(name = "mongoDbFactory")
public MongoDbFactory mongoDbFactory(@Qualifier("mongoClient")MongoClient mongoClient,
@Value("${mongo.database}")String database){
MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, database);
return mongoDbFactory;
}
// <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
// <constructor-arg name="mongoDbFactory" ref="dapFactory"/>
// </bean>
@Bean(name = "db-mongoTemplate")
public MongoTemplate mongoTemplate(@Qualifier("mongoDbFactory")MongoDbFactory mongoDbFactory){
MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory);
return mongoTemplate;
}
}
=========================================
CityServiceImpl.java
=========================================
@Service
public class CityServiceImpl implements CityService {
@Resource(name = "db-mongoTemplate")
private MongoTemplate mongoTemplate;
@Override
public void save(City city) {
mongoTemplate.save(city);
}
@Override
public City findByName(String cityName) {
Query query = new Query(Criteria.where("cityName").is(cityName));
City city = mongoTemplate.findOne(query, City.class);
return city;
}
@Override
public void updateCity(City city) {
Query query = new Query(Criteria.where("id").is(city.getId()));
Update update = new Update().set("cityName", city.getCityName()).set("description", city.getDescription());
//更新查询返回结果集的第一条
mongoTemplate.updateFirst(query, update, City.class);
//更新查询返回结果集的所有
// mongoTemplate.updateMulti(query,update,UserEntity.class);
}
@Override
public void deleteById(Long id) {
Query query = new Query(Criteria.where("id").is(id));
mongoTemplate.remove(query, City.class);
}
}
=========================================
TestMongodb.java
=========================================
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class TestMongodb {
@Autowired
CityService cityService;
@Test
public void testSave(){
City city = new City();
city.setId(1l);
city.setCityName("test");
city.setDescription("hello");
city.setProvinceId(1l);
cityService.save(city);
}
}
发表评论
-
springboot:condition
2017-07-26 11:10 360public class LinuxCondition imp ... -
springboot:tomcat启动
2017-07-20 15:02 9201.在pom.xml里设置 <packaging> ... -
springboot:shiro
2017-07-13 15:52 964第一次学习系统学习shiro 并将shiro集成到sprin ... -
springboot:upload
2017-07-06 10:25 750FileUploadConfiguration.java == ... -
springboot:servlet
2017-07-06 10:17 504Application.java ============== ... -
springboot:freemarker
2017-07-05 17:33 556pom.xml ======================= ... -
springboot:task
2017-07-05 12:11 441TaskPool.java ================= ... -
springboot:热部署
2017-07-05 11:23 355pom.xml: ====================== ... -
springboot:注解
2017-07-04 11:36 624@EnableAutoConfiguration注解 excl ... -
springboot:server属性配置
2017-07-04 10:05 750server配置 ====================== ... -
springboot:memcached
2017-07-03 17:23 925pom.xml ======================= ... -
springboot:health
2017-07-03 16:43 396<dependency> ... -
springboot:quartz集群
2017-07-02 20:40 998pom.xml ======================= ... -
springboot:ControllerAdvice
2017-07-02 14:09 372全局异常拦截 //@ControllerAdvice(anno ... -
springboot:dubbo
2017-07-02 10:40 500server: ======================= ... -
springboot:amq
2017-07-01 22:20 466pom.xml ======================= ... -
springboot:redis(jedis)
2017-07-01 14:10 904application.properties ======== ... -
springboot:mybatis&druid&pagehelper
2017-07-01 13:35 411=============================== ... -
springboot:logback
2017-06-30 16:20 553=============================== ... -
springboot:interceptor
2017-06-30 14:04 630IncpConfig.java 增加拦截器config 继承W ...
相关推荐
基于springboot+mongodb搭建的简约个人博客系统 基于springboot+mongodb搭建的简约个人博客系统 基于springboot+mongodb搭建的简约个人博客系统 基于springboot+mongodb搭建的简约个人博客系统 基于springboot+...
spring.data.mongodb.uri=mongodb://username:password@localhost:27017/mydatabase ``` 或 ```yaml # application.yml spring: data: mongodb: uri: mongodb://username:password@localhost:27017/mydatabase ...
spring.data.mongodb.uri=mongodb://username:password@localhost:27017/dbname?connectTimeoutMS=30000&socketTimeoutMS=30000 ``` 或者 ```yaml # application.yml 示例 spring: data: mongodb: uri: mongodb...
2. 配置MongoDB:在`application.properties`中指定MongoDB的连接信息。 3. 创建MongoDB Repository:创建一个接口,继承`MongoRepository`或其子接口,定义查询方法。 4. 实体类:定义与MongoDB集合对应的Java类,...
spring.data.mongodb.uri=mongodb://username:password@localhost:27017/dbname # 或者在 YAML 格式中 spring: data: mongodb: uri: mongodb://username:password@localhost:27017/dbname ``` 在SpringBoot中,...
java springboot mongodb 通过mongotemplate获取mongo服务器当前时间。
本项目结合了SpringBoot、MongoDB和Echarts这三个技术,构建了一个强大的数据可视化平台。下面将详细介绍这三个技术以及它们如何协同工作来实现数据可视化。 首先,SpringBoot是基于Spring框架的一个轻量级开发工具...
在构建“springboot+mongodb 水下网络拓扑发现系统”时,我们结合了Spring Boot框架和MongoDB数据库,以实现一个高效且灵活的数据存储和处理解决方案。Spring Boot简化了Spring应用程序的开发过程,而MongoDB则为非...
spring.data.mongodb.uri=mongodb://localhost:27017/mydatabase ``` Spring Data MongoDB提供了一个`MongoTemplate`类,它允许我们直接操作MongoDB。但更常见的是,我们会创建领域对象并使用Repository接口,这...
spring.data.mongodb.uri=mongodb://username:password@localhost:27017/mydatabase ``` 接下来,我们将讨论SpringData MongoDB的使用。SpringData提供了一种声明式的方式来处理MongoDB的操作,如增删查改。通过...
《整合Springboot、Mybatis-Plus与MongoDB的实战指南》 在当今的软件开发领域,Springboot以其轻量级、快速开发的优势,已经成为Java Web应用的首选框架。MongoDB作为NoSQL数据库的代表,因其非关系型特性和强大的...
mongodb版本号3.2,需手动开启http服务,启动时使用命令 ./mongod --httpinterface 新增 修改 删除 查询 排序 分页 可查看: https://blog.csdn.net/m0_37132783/article/details/96992439
spring.data.mongodb.uri=mongodb://username:password@localhost:27017/mydatabase ``` 3. **创建模型类**:为了与 MongoDB 存储数据进行交互,你需要定义一个 Java 类作为文档模型,使用注解如 `@Document` 来...
Springboot整合MongoDB进行CRUD操作的两种方式 在分布式和微服务架构中,Springboot和MongoDB是非常流行的技术栈。 Springboot是最简单的使用Spring的方式,而MongoDB是最流行的NoSQL数据库。两者在实际应用中使用...
spring.data.mongodb.uri=mongodb://localhost:27017/mydatabase ``` 接下来,我们将创建MongoDB的Repository接口。Spring Data MongoDB提供了一个基于注解的接口模型,允许我们定义基本的CRUD操作。例如,我们可以...
1、基于springboot+mongodb搭建的简约个人博客系统源码+项目说明.zip 2、该资源包括项目的全部源码,下载可以直接使用! 3、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考...
在本项目中,"springboot_demo1_springbootmongodb_springboot_MongoDB_"是一个关于SpringBoot集成MongoDB的应用示例。SpringBoot是Java开发的一个轻量级框架,它简化了Spring应用程序的创建和配置过程,而MongoDB则...
SpringBoot 与 MongoDB 的整合主要通过 `spring-boot-starter-data-mongodb` 依赖实现,这使得我们能够轻松地进行 CRUD(创建、读取、更新、删除)操作。 Redis 是一个开源的、高性能的键值对数据存储系统,通常...
spring.data.mongodb.uri=mongodb://localhost:27017/mydatabase ``` 或者 ```yaml # application.yml 示例 spring: data: mongodb: uri: mongodb://localhost:27017/mydatabase ``` 二、实体类与数据访问层 ...