zoukankan      html  css  js  c++  java
  • spring boot 1.5.2 操作mongodb3.4.0

    1:build.gradle 添加mongodb依赖

    dependencies {
        compile('org.springframework.boot:spring-boot-starter-web')
        compile('org.springframework.boot:spring-boot-starter-data-mongodb')
        compile group: 'org.mongodb', name: 'mongo-java-driver', version: '3.4.2'
    
        testCompile('org.springframework.boot:spring-boot-starter-test')
    }

    2:启动本地mongdb服务

    D:MongoDBServer3.4in>mongod --config "D:MongoDBServer3.4mongo.conf" --auth

    3:配置application.properties文件

    #mongodb config
    spring.data.mongodb.host=127.0.0.1
    spring.data.mongodb.port=27017
    spring.data.mongodb.username=gwzh
    spring.data.mongodb.password=gwzh
    spring.data.mongodb.database=gwzh
    spring.data.mongodb.authentication-database=gwzh
    
    #server config
    server.port=9001

    4:代码

    (1)UserVo.java

    package com.example.user;
    
    import org.springframework.data.annotation.Id;
    
    /**
     * Created by yan on 2017/4/25.
     */
    public class UserVo {
        @Id
        private String userid;
        private String username;
        private Integer age;
    
        public String getUserid() {
            return userid;
        }
    
        public void setUserid(String userid) {
            this.userid = userid;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    }

    (2)UserRepository.java

    package com.example.user;
    
    
    import org.springframework.data.mongodb.repository.MongoRepository;
    
    
    /**
     * Created by yan on 2017/4/25.
     */
    public interface UserRepository extends MongoRepository<UserVo,String> {
    
        UserVo findByUserid(String userid);
    }

    (3)UserCtrl.java

    package com.example.user;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    
    /**
     * Created by yan on 2017/4/25.
     */
    @RestController
    @RequestMapping(
            value = "/user",
            produces = "application/json;charset=utf-8",
            headers = "Accept=application/json"
    )
    public class UserCtrl {
    
        @Autowired
        UserRepository userRepository;
    
        @RequestMapping(
                value = "/add",
                method = RequestMethod.POST
        )
        public UserVo addUser(@RequestBody UserVo vo){
            return userRepository.save(vo);
        }
    
        @RequestMapping(
                value = "/list",
                method = RequestMethod.GET
        )
        public List<UserVo> getUsers(){
            return userRepository.findAll();
        }
    
        @RequestMapping(
                value = "/delete",
                method = RequestMethod.POST
        )
        public boolean deleteUser(@RequestBody UserVo vo){
            userRepository.delete(vo.getUserid());
            return true;
        }
    }

    5:测试

  • 相关阅读:
    【HDOJ6701】Make Rounddog Happy(启发式合并)
    【HDOJ6731】Angle Beats(极角排序)
    【BZOJ1132】Tro(叉积)
    【CF1236D】Alice and the Doll(set)
    Storm
    Spark
    Python基础(2)
    数据库漫谈
    Python基础(1)
    C/C++链接过程相关
  • 原文地址:https://www.cnblogs.com/yshyee/p/6760986.html
Copyright © 2011-2022 走看看