MongoTemplate基本操作

正文

本文基于spring boot项目,快速构建项目请参考:https://www.cnblogs.com/lay2017/p/8836273.html

回到顶部
添加mongo依赖

org.springframework.boot
spring-boot-starter-data-mongodb

回到顶部
配置连接
有密码

spring.data.mongodb.uri=mongodb://name:password@localhost:27017/test
无密码

spring.data.mongodb.uri=mongodb://localhost:27017/test
回到顶部
代码示例
实体类

复制代码
package cn.lay.mongo.entity;

import org.springframework.data.mongodb.core.mapping.Document;

/**

  • @author lay

  • @date 2018/09/13 14:53
    */
    @Document(collection = “user_collection”)
    public class User {

    private Long id;

    private String name;

    private Integer age;

    public Long getId() {
    return id;
    }

    public void setId(Long id) {
    this.id = id;
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public Integer getAge() {
    return age;
    }

    public void setAge(Integer age) {
    this.age = age;
    }

    @Override
    public String toString() {
    return “User{” +
    “id=” + id +
    “, name=’” + name + ‘’’ +
    “, age=” + age +
    ‘}’;
    }
    }
    复制代码
    service类

复制代码
package cn.lay.mongo.service;

import cn.lay.mongo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;

/**

  • @author lay

  • @date 2018/09/13 14:54
    */
    @Service
    public class UserService {

    @Autowired
    private MongoTemplate mt;

    public void saveUser(User user) {
    mt.save(user);
    }

    public void removeUser(User user) {
    Query query = new Query(Criteria.where(“id”).is(user.getId()));
    mt.remove(query, User.class);
    }

    public void updateUser(User user) {
    Query query = new Query(Criteria.where(“id”).is(user.getId()));
    Update update = new Update().set(“name”, “gary”);
    mt.updateMulti(query, update, User.class);
    }

    public User select(User user) {
    Query query = new Query(Criteria.where(“id”).is(user.getId()));
    User user1 = mt.findOne(query, User.class);
    return user1;
    }

    public List selectAll() {
    List users = mt.findAll(User.class);
    return users;
    }
    }
    复制代码
    controller

复制代码
package cn.lay.mongo.controller;

import cn.lay.mongo.entity.User;
import cn.lay.mongo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**

  • @author lay

  • @date 2018/09/13 14:45
    */
    @RestController
    @RequestMapping(“user”)
    public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping(“get”)
    public User getUser() {
    User user = new User();
    user.setId(1L);
    return userService.select(user);
    }

    @GetMapping(“save”)
    public void saveUser() {
    User user = new User();
    user.setId(1L);
    user.setName(“marry”);
    user.setAge(24);
    userService.saveUser(user);
    }

    @GetMapping(value = “remove”)
    public void removeUser() {
    User user = new User();
    user.setId(1L);
    user.setName(“lay”);
    user.setAge(24);
    userService.removeUser(user);
    }

    @GetMapping(value = “update”)
    public void updateUser() {
    User user = new User();
    user.setId(1L);
    user.setName(“lay”);
    user.setAge(24);
    userService.updateUser(user);
    }

    @GetMapping(“list”)
    public List listUser() {
    return userService.selectAll();
    }

}
复制代码

你可能感兴趣的:(mongo)