springboot集成boot

首先idea 中新建项目,在nosql 中选中MongoDB
这里写图片描述
这里写图片描述

最终的pom.xml文件有这个依赖

<dependency>

   <groupId>org.springframework.boot</groupId>

   <artifactId>spring-boot-starter-data-mongodb</artifactId>

</dependency>

在application.properties(重名为application.yml也行,看个人喜好)中添

spring:
    #mongo配置
  data:
​    mongodb:
​      database: MyFirstdb
​      uri: mongodb://127.0.0.1:27017是在MongoDB建立一个DataBase 和Collection如图(很早之前就建立了,省事就这么用了)

这里写图片描述
然后对应实体



package com.utopia.springboothn.model;


import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;


import java.io.Serializable;



/**

  *@author hn

  *@date 2018/08/02

 */

@Document(collection = "first")

public class First implements Serializable {/**

​     * 将MongoDB的"_id" 对应 实体中的uid字段

​    */@Field("_id")

​    private String uid;

​    private String title;

​    private String description;

​    private String by;

​    private int likes;

​    /**

​     *这个属性不是数据库字段,不用映射数据库

​     */@Transientprivate String memo;



​    public String getUid() {

​        return uid;

​    }



​    public void setUid(String uid) {

​        this.uid = uid;

​    }



​    public String getTitle() {

​        return title;

​    }



​    public void setTitle(String title) {

​        this.title = title;

​    }

​    public String getDescription() {

​        return description;

​    }

​    public void setDescription(String description) {

​        this.description = description;

​    }

​    public String getBy() {

​        return by;

​    }

​    public void setBy(String by) {

​        this.by = by;

​    }

​    public int getLikes() {

​        return likes;

​    }

​    public void setLikes(int likes) {

​        this.likes = likes;

​    }

​    public String getMemo() {

​        return memo;

​    }

​    public void setMemo(String memo) {

​        this.memo = memo;

​    }

}

service层


package com.utopia.springboothn.service;



import com.utopia.springboothn.model.First;



import java.util.List;



/**

  *@author hn

  *@date 2018/08/02

 */

public interface IFirstService {public void saveFirst(First first);



​    public First findById(String id);



​    public List<First> listAll();





}

以及实现:

package com.utopia.springboothn.service.impl;



import com.utopia.springboothn.model.First;

import com.utopia.springboothn.service.IFirstService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.mongodb.core.MongoOperations;

import org.springframework.data.mongodb.core.query.Criteria;

import org.springframework.data.mongodb.core.query.Query;

import org.springframework.stereotype.Component;



import java.util.List;

@Component("firstService")

public class FirstServiceImpl implements IFirstService{

​    @Autowired

​    MongoOperations operations;

​    @Override

​    public void saveFirst(First first) {

​        operations.save(first);

​    }



​    @Override

​    public First findById(String id) {

​        return operations.findById(new Query(Criteria.where("id").is(id)),First.class);

​    }



​    @Override

​    public List<First> listAll() {

​        return operations.findAll(First.class);

​    }

}

最后controller层

package com.utopia.springboothn.controller;

import com.mongodb.MongoException;

import com.utopia.springboothn.common.BaseJson;

import com.utopia.springboothn.common.BaseJsonData;

import com.utopia.springboothn.model.First;

import com.utopia.springboothn.service.IFirstService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController

@RequestMapping(value = "/mongodb")

public class MongoController {

​    @Autowired

​    IFirstService firstService;

​    @RequestMapping(value = "/first")

​    public BaseJson save(First first){

​        BaseJson json = new BaseJson();

​        try {

​            firstService.saveFirst(first);

​        } catch (MongoException e){

​            json.setErrorMessage(e.getMessage());

​        }

​        return json;

​    }

​    @RequestMapping(value = "/list")

​    public BaseJsonData list(First first){

​        BaseJsonData json = new BaseJsonData();

​        try {

​           List<First> results = firstService.listAll();

​           json.setData(results);

​        } catch (MongoException e){

​            json.setErrorMessage(e.getMessage());

​        }

​        return json;

​    }

}

最终大功告成,启动应用程序

这里写图片描述
在浏览器中或者postman中输入地址:http://localhost:8080/mongodb/list

这里写图片描述
项目的git地址:https://github.com/hnTos/springboot-hn.git

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011546953/article/details/81592355
上一篇MySQL的explain的相关参数含义说明
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭