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中添加以下依赖
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-mongodbartifactId>
dependency>
4、修改application.yml配置文件
spring:
data:
mongodb:
uri: mongodb://localhost:27017/boot_mongo
application:
name: mongoweb
server:
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 findAll();
User getUser(Integer id);
void update(User user);
void insert(User user);
void insertAll(List users);
void remove(Integer id);
List 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 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 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 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 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 findAll();
User getUser(Integer id);
void update(User user);
void insert(User user);
void insertAll(List users);
void remove(Integer id);
List 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 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 users) {
userDao.insertAll(users);
}
@Override
public void remove(Integer id) {
userDao.remove(id);
}
@Override
public List 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 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 find(){
return userService.findAll();
}
@RequestMapping("/find/{start}")
public List 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