使用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 }
View Code

相关文章:

  • 2022-12-23
  • 2021-12-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-13
  • 2021-04-15
  • 2021-08-09
猜你喜欢
  • 2021-12-22
  • 2021-05-11
  • 2021-06-22
  • 2022-12-23
  • 2022-12-23
  • 2021-05-10
  • 2022-02-27
相关资源
相似解决方案