一、環境
1、maven 3.3.9
2、eclipse
3、springboot 1.5.6
4、mongo 3.4.6 二、開發
1、建立一個springboot項目,引入1.5.6依賴。
2、項目結構如下
3、在pom中添加以下依賴
<!-- spring-boot-starter-data-mongodb --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
4、修改application.yml設定檔
spring: data: mongodb: uri: mongodb://localhost:27017/boot_mongo application: name: mongowebserver: port: 8888
5、編寫實體類User.java
package com.mongo.mongoweb.model;import java.io.Serializable;import org.springframework.data.annotation.Id;/** * user 實體類 * * 作者: 段浩傑 2017年7月29日 */public class User implements Serializable { /** * */ private static final long serialVersionUID = 1L; // @id這個註解來對應mongo的_id這個欄位 @Id private String _id; private int id; private String name; private int age; public User(String _id, int id, String name, int age) { this._id = _id; this.id = id; this.name = name; this.age = age; } public User(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String get_id() { return _id; } public void set_id(String _id) { this._id = _id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public User() { super(); } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String toString() { return "User [_id=" + _id + ", id=" + id + ", name=" + name + ", age=" + age + "]"; }}
6、編寫UserDao
package com.mongo.mongoweb.dao;import java.util.List;import org.springframework.data.domain.Pageable;import com.mongo.mongoweb.model.User;/** * userDao介面定義 * * 作者: 段浩傑 2017年7月30日 */public interface UserDao { List<User> findAll(); User getUser(Integer id); void update(User user); void insert(User user); void insertAll(List<User> users); void remove(Integer id); List<User> findByPage(User user, Pageable pageable);}
7、編寫UserDaoImpl
package com.mongo.mongoweb.daoimpl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.domain.Pageable;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.Repository;import com.mongo.mongoweb.dao.UserDao;import com.mongo.mongoweb.model.User;/** * 介面實現 * * 作者: 段浩傑 2017年7月30日 */@Repository("userDao")public class UserDaoImpl implements UserDao { /** * 由springboot自動注入,預設配置會產生mongoTemplate這個bean */ @Autowired private MongoTemplate mongoTemplate; /** * 尋找全部 */ @Override public List<User> findAll() { return mongoTemplate.findAll(User.class); } /** * 根據id得到對象 */ @Override public User getUser(Integer id) { return mongoTemplate.findOne(new Query(Criteria.where("id").is(id)), User.class); } /** * 插入一個使用者 */ @Override public void insert(User user) { mongoTemplate.insert(user); } /** * 根據id刪除一個使用者 */ @Override public void remove(Integer id) { Criteria criteria = Criteria.where("id").is(id); Query query = new Query(criteria); mongoTemplate.remove(query,User.class); } /** * 分頁尋找 * * user代表過濾條件 * * pageable代表分頁bean */ @Override public List<User> findByPage(User user, Pageable pageable) { Query query = new Query(); if (user != null && user.getName() != null) { //模糊查詢 query = new Query(Criteria.where("name").regex("^" + user.getName())); } List<User> list = mongoTemplate.find(query.with(pageable), User.class); return list; } /** * 根據id更新 */ @Override public void update(User user) { Criteria criteria = Criteria.where("id").is(user.getId()); Query query = new Query(criteria); Update update = Update.update("name", user.getName()).set("age", user.getAge()); mongoTemplate.updateMulti(query, update, User.class); } /** * 插入一個集合 */ @Override public void insertAll(List<User> users) { mongoTemplate.insertAll(users); }}
8、編寫UserService
package com.mongo.mongoweb.service;import java.util.List;import org.springframework.data.domain.Pageable;import com.mongo.mongoweb.model.User;public interface UserService { List<User> findAll(); User getUser(Integer id); void update(User user); void insert(User user); void insertAll(List<User> users); void remove(Integer id); List<User> findByPage(User user,Pageable pageable);}
9、編寫UserServiceImpl
package com.mongo.mongoweb.serviceimpl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.domain.Pageable;import org.springframework.stereotype.Service;import com.mongo.mongoweb.dao.UserDao;import com.mongo.mongoweb.model.User;import com.mongo.mongoweb.service.UserService;@Service("userService")public class UserServiceImpl implements UserService{ @Autowired private UserDao userDao; @Override public List<User> findAll() { return userDao.findAll(); } @Override public User getUser(Integer id) { return userDao.getUser(id); } @Override public void update(User user) { userDao.update(user); } @Override public void insert(User user) { userDao.insert(user); } @Override public void insertAll(List<User> users) { userDao.insertAll(users); } @Override public void remove(Integer id) { userDao.remove(id); } @Override public List<User> findByPage(User user, Pageable pageable) { return userDao.findByPage(user, pageable); }}
10、編寫UserController控制器
package com.mongo.mongoweb.controller;import java.util.ArrayList;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.domain.PageRequest;import org.springframework.data.domain.Pageable;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import com.mongo.mongoweb.model.User;import com.mongo.mongoweb.service.UserService;/** * user控制器 * * 作者: 段浩傑 2017年7月30日 */@RestController@RequestMapping("/user")public class UserController { @Autowired private UserService userService; @RequestMapping("/get/{id}") public User getUser(@PathVariable int id) { return userService.getUser(id); } @RequestMapping("/delete/{id}") public String delete(@PathVariable int id) { userService.remove(id); return "delete sucess"; } @RequestMapping("/add") public String insert() { User user =new User(16, ""+16, 16); userService.insert(user); return "sucess"; } @RequestMapping("/insert") public String insertAll() { List<User> list = new ArrayList<>(); for (int i = 10; i < 15; i++) { list.add(new User(i, "" + i, i)); } userService.insertAll(list); return "sucess"; } @RequestMapping("/find/all") public List<User> find(){ return userService.findAll(); } @RequestMapping("/find/{start}") public List<User> findByPage(@PathVariable int start,User user){ Pageable pageable=new PageRequest(start, 2); return userService.findByPage(user, pageable); } @RequestMapping("/update/{id}") public String update(@PathVariable int id){ User user =new User(id, ""+1, 1); userService.update(user); return "sucess"; }}
11、然後啟動運行即可 ,源碼地址:
https://git.oschina.net/lgr123/springbootMongoDB.git