1. 程式人生 > >MongoTemplate基本操作

MongoTemplate基本操作

正文

回到頂部 新增mongo依賴 org.springframework.boot spring-boot-starter-data-mongodb 回到頂部 配置連線 有密碼

spring.data.mongodb.uri=mongodb://name:[email protected]: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(); }

} 複製程式碼