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:测试

  • 相关阅读:
    cf1100 F. Ivan and Burgers
    cf 1033 D. Divisors
    LeetCode 17. 电话号码的字母组合
    LeetCode 491. 递增的子序列
    LeetCode 459.重复的子字符串
    LeetCode 504. 七进制数
    LeetCode 3.无重复字符的最长子串
    LeetCode 16.06. 最小差
    LeetCode 77. 组合
    LeetCode 611. 有效三角形个数
  • 原文地址:https://www.cnblogs.com/yshyee/p/6760986.html
Copyright © 2011-2022 走看看