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

  • 相关阅读:
    HTML标签(2)
    HTML简介(1)
    JqueryUI input 自动提示 autocomplete
    Linux基础--单ubuntu 系统 u盘启动 install
    Spark Parquet file split
    HashMap与ConcurrentHashMap
    线程池阻塞队列之ArrayBlockingQueue
    线程池阻塞队列之LinkedBlockingQueue
    线程池的拒绝策略
    关闭线程池shutdown 和 shutdownNow 的区别
  • 原文地址:https://www.cnblogs.com/yshyee/p/6760986.html
Copyright © 2020-2023  润新知