springdata开发MongoDB示例
Posted 贝隆
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springdata开发MongoDB示例相关的知识,希望对你有一定的参考价值。
springdata框架
使用IDEA初始化springboot+springdata框架,开发mongodb示例,pom.xml文件如下
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demomongodb</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demomongodb</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
主要实现思路
Mongotemplate
首先创建一个实体,用来对应mongodb中的文档
1 //@Document(collection = "test_stock") 2 @Document 3 public class Stock { 4 5 private String id; 6 private String ts_code; 7 private String trade_date; 8 private float open; 9 private float high; 10 private float low; 11 private float close; 12 private float pre_close; 13 private float change; 14 private float pct_chg; 15 private Long vol; 16 private float amount; 17 18 public Stock() { 19 } 20 21 public String getId() { 22 return id; 23 } 24 25 public void setId(String id) { 26 this.id = id; 27 } 28 29 public String getTs_code() { 30 return ts_code; 31 } 32 33 public void setTs_code(String ts_code) { 34 this.ts_code = ts_code; 35 } 36 37 public String getTrade_date() { 38 return trade_date; 39 } 40 41 public void setTrade_date(String trade_date) { 42 this.trade_date = trade_date; 43 } 44 45 public float getOpen() { 46 return open; 47 } 48 49 public void setOpen(float open) { 50 this.open = open; 51 } 52 53 public float getHigh() { 54 return high; 55 } 56 57 public void setHigh(float high) { 58 this.high = high; 59 } 60 61 public float getLow() { 62 return low; 63 } 64 65 public void setLow(float low) { 66 this.low = low; 67 } 68 69 public float getClose() { 70 return close; 71 } 72 73 public void setClose(float close) { 74 this.close = close; 75 } 76 77 public float getPre_close() { 78 return pre_close; 79 } 80 81 public void setPre_close(float pre_close) { 82 this.pre_close = pre_close; 83 } 84 85 public float getChange() { 86 return change; 87 } 88 89 public void setChange(float change) { 90 this.change = change; 91 } 92 93 public float getPct_chg() { 94 return pct_chg; 95 } 96 97 public void setPct_chg(float pct_chg) { 98 this.pct_chg = pct_chg; 99 } 100 101 public Long getVol() { 102 return vol; 103 } 104 105 public void setVol(Long vol) { 106 this.vol = vol; 107 } 108 109 public float getAmount() { 110 return amount; 111 } 112 113 public void setAmount(float amount) { 114 this.amount = amount; 115 } 116 117 @Override 118 public String toString() { 119 return "Stock{" + 120 "id=‘" + id + ‘‘‘ + 121 ", ts_code=‘" + ts_code + ‘‘‘ + 122 ", trade_date=‘" + trade_date + ‘‘‘ + 123 ", open=" + open + 124 ", high=" + high + 125 ", low=" + low + 126 ", close=" + close + 127 ", pre_close=" + pre_close + 128 ", change=" + change + 129 ", pct_chg=" + pct_chg + 130 ", vol=" + vol + 131 ", amount=" + amount + 132 ‘}‘; 133 } 134 }
其次,用使用mongotemplate的操作访问mongodb数据库,spring单元测试查询数据
1 package com.example.demomongodb; 2 3 import com.example.demomongodb.dao.StockRepository; 4 import com.example.demomongodb.document.Stock; 5 import org.junit.Test; 6 import org.junit.runner.RunWith; 7 import org.slf4j.Logger; 8 import org.slf4j.LoggerFactory; 9 import org.springframework.beans.factory.annotation.Autowired; 10 import org.springframework.boot.test.context.SpringBootTest; 11 import org.springframework.data.domain.Example; 12 import org.springframework.data.domain.Page; 13 import org.springframework.data.domain.PageRequest; 14 import org.springframework.data.domain.Sort; 15 import org.springframework.data.mongodb.core.MongoTemplate; 16 import org.springframework.data.mongodb.core.query.Criteria; 17 import org.springframework.data.mongodb.core.query.Query; 18 import org.springframework.test.context.junit4.SpringRunner; 19 20 import java.util.List; 21 22 @RunWith(SpringRunner.class) 23 @SpringBootTest 24 public class DemomongodbApplicationTests { 25 private static final String COLLECTION_NAME = "test_stock"; 26 27 @Autowired 28 private StockRepository stockRepository; 29 30 @Autowired 31 private MongoTemplate mongoTemplate; 32 33 private static final Logger logger = LoggerFactory.getLogger(DemomongodbApplicationTests.class); 34 35 @Test 36 public void queryDemo() { 37 //add sort 38 Criteria criteria = Criteria.where("ts_code").is("600318.SH"); 39 Query query = new Query(criteria); 40 List<Stock> results = mongoTemplate.find(query, Stock.class, COLLECTION_NAME); 41 for (Stock each:results) { 42 logger.info("each={}", each.toString()); 43 } 44 } 45 46 }
对应的application.yml如下
1 spring: 2 data: 3 mongodb: 4 uri: mongodb://mydb:admin@localhost:27017/mydb
代码github地址,欢迎star
https://github.com/xiaof-github/springdatamongodb
以上是关于springdata开发MongoDB示例的主要内容,如果未能解决你的问题,请参考以下文章
Cloudfoundry + SpringData + MongoDB ...无法部署
使用 SpringData-MongoDB 将 Java 8 Instant 存储为 BSON 日期