spring boot系列spring boot 使用mongodb

Posted kaixinmao1987

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot系列spring boot 使用mongodb相关的知识,希望对你有一定的参考价值。

1 pom.xml配置

增加包依赖:spring-boot-starter-data-mongodb

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

2 application.properties配置文件中增加:

spring.data.mongodb.uri=mongodb://127.0.0.1:27017/ceshidb

ceshidb是数据库名字

    2.4版本以上的mongodb采用如下格式配置:

spring.data.mongodb.uri=mongodb://root(userName):root(password)@localhost(ip地址):27017(端口号)/gis数据库)

 3 创建实体类

package com.cfj.ceshi.entity;


public class UserEntity {
    
    private static final long serialVersionUID = 1L;
    private Long id;
    private String userName;
    private String passWord;
    

    public UserEntity() {
        super();
    }

    public UserEntity(String userName, String passWord) {
        super();
        this.passWord = passWord;
        this.userName = userName;
    }

    public Long getId() {
        return id;
    }

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

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    
    @Override
    public String toString() {
        return "userName " + this.userName + ", pasword " + this.passWord;
    }

}

4 创建dao接口和实现类

package com.cfj.ceshi.dao;

import com.cfj.ceshi.entity.UserEntity;

public interface UserDao {
    
    public void saveUser(UserEntity user);

    public UserEntity findUserByUserName(String userName);

    public int updateUser(UserEntity user);

    public void deleteUserById(Long id);

}
package com.cfj.ceshi.dao.impl;



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.Repository;

import com.cfj.ceshi.dao.UserDao;
import com.cfj.ceshi.entity.UserEntity;
import com.mongodb.WriteResult;

@Repository
public class UserDaoImpl implements UserDao{
    
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void saveUser(UserEntity user) {
        //mongoTemplate.save(user);
        mongoTemplate.save(user, "jihe01");//指定集合
        
    }

    @Override
    public UserEntity findUserByUserName(String userName) {
        Query query = new Query(Criteria.where("userName").is(userName));
        //UserEntity user =  mongoTemplate.findOne(query, UserEntity.class);
        UserEntity user =  mongoTemplate.findOne(query, UserEntity.class, "jihe01");//指定集合
        return user;
    }

    @Override
    public int updateUser(UserEntity user) {
        Query query=new Query(Criteria.where("id").is(user.getId()));
        Update update= new Update().set("userName", user.getUserName()).set("passWord", user.getPassWord());
        //更新查询返回结果集的第一条
        WriteResult result =mongoTemplate.updateFirst(query,update,UserEntity.class, "jihe01");
        //更新查询返回结果集的所有
        // mongoTemplate.updateMulti(query,update,UserEntity.class);
        if(result!=null)
            return result.getN();
        else
            return 0;
    }

    @Override
    public void deleteUserById(Long id) {
        Query query=new Query(Criteria.where("id").is(id));
        mongoTemplate.remove(query,UserEntity.class, "jihe01");
        
    }
    
    

}

 5 创建service接口和实现类

package com.cfj.ceshi.service;

import com.cfj.ceshi.entity.UserEntity;

public interface UserService {
    
    public void saveUser(UserEntity user);

    public UserEntity findUserByUserName(String userName);

    public int updateUser(UserEntity user);

    public void deleteUserById(Long id);
   
}
package com.cfj.ceshi.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cfj.ceshi.dao.UserDao;
import com.cfj.ceshi.entity.UserEntity;
import com.cfj.ceshi.service.UserService;


@Service
public class UserServiceImpl implements UserService{
    
    @Autowired
    private UserDao userDao;

    @Override
    public void saveUser(UserEntity user) {
        userDao.saveUser(user);
    }

    @Override
    public UserEntity findUserByUserName(String userName) {
        return userDao.findUserByUserName(userName);
    }

    @Override
    public int updateUser(UserEntity user) {
        return userDao.updateUser(user);
    }

    @Override
    public void deleteUserById(Long id) {
        userDao.deleteUserById(id);
    }
    

}

6 创建web(control)层

package com.cfj.ceshi.web;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cfj.ceshi.entity.UserEntity;
import com.cfj.ceshi.service.UserService;

@RestController
public class UserController {
    
    @Autowired
    private UserService userService;
    
    
    //按名字查询
    @RequestMapping("/getUserByName")
    public String getUserByName(String name) {
        UserEntity user= userService.findUserByUserName(name);
        return user.toString();
    }
    
    //增加方法
    @RequestMapping("/add")
    public void save() {
         UserEntity user=new UserEntity();
         user.setId(3l);
         user.setUserName("小明");
         user.setPassWord("fffooo123");
         userService.saveUser(user);
     }
     //更新方法
     @RequestMapping("/update")
     public void update(UserEntity user) {
         userService.updateUser(user);
     }
     //删除方法
     @RequestMapping(value="/delete")
     public void delete(Long id) {
         userService.deleteUserById(id);
     }
    

}

 7 可以使用postman等工具进行测试

 

代码地址:https://gitee.com/kaixinmao/springboot-mongodb

以上是关于spring boot系列spring boot 使用mongodb的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot系列Spring Boot入门

spring boot系列spring boot 配置spring data jpa (查询方法)

Spring Boot系列Spring Initializer快速创建Spring Boot项目

Spring Boot系列 Spring Boot介绍和基础POM文件

spring boot 系列之四:spring boot 整合JPA

Spring Boot系列教程七:Spring boot集成MyBatis