springboot整合mongodb CRUD

1.导入配置文件


        
            org.mongodb
            mongo-java-driver
            3.8.0
        
        
            org.springframework.data
            spring-data-mongodb
            2.0.9.RELEASE
        
spring.data.mongodb.host=127.0.0.1
spring.data.mongodb.port=27017
spring.data.mongodb.database=local

启动本地mongodb

image.png

连接工具

image.png

2.新建实体类UserInfo

package com.mochain.epay.mongodb.entity;

import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

import java.io.Serializable;
import java.math.BigDecimal;

@Data
@Document(collection = "userInfo")
public class UserInfo implements Serializable {

    private static final long serialVersionUID = 1L;

    //唯一主键
    @Id
    @Field("id")
    private Long id;

    @Field("user_name")
    private String userName;

    @Field("age")
    private Integer age;

    @Field("height")
    private BigDecimal height;

}

3.新建dao

package com.mochain.epay.mongodb.dao;

import com.mochain.epay.mongodb.entity.UserInfo;

import java.util.List;

public interface UserInfoDao {
    /**
     * 批量创建对象
     *
     * @param userList
     */
    void batchInsert(List userList);

    /**
     * 创建对象
     *
     * @param userInfo
     */
    void insert(UserInfo userInfo);

    /**
     * 根据ID查询对象
     *
     * @param id
     * @return
     */
    UserInfo findByUserId(Long id);

    /**
     * 更新
     *
     * @param userInfo
     */
    void update(UserInfo userInfo);

    /**
     * 删除
     *
     * @param id
     */
    void delete(Long id);

    /**
     * 查询列表
     *
     * @return
     */
    List findUserList();

}

4.新建dao实现

package com.mochain.epay.mongodb.dao.impl;

import com.mochain.epay.mongodb.dao.UserInfoDao;
import com.mochain.epay.mongodb.entity.UserInfo;
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.Component;

import java.util.List;

@Component
public class UserInfoDaoImpl implements UserInfoDao {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void batchInsert(List userList) {
        for (UserInfo userInfo : userList) {
            mongoTemplate.save(userInfo);
        }
    }

    @Override
    public void insert(UserInfo userInfo) {
        mongoTemplate.save(userInfo);
    }

    @Override
    public UserInfo findByUserId(Long id) {
        Query query = new Query(Criteria.where("id").is(id));
        return mongoTemplate.findOne(query, UserInfo.class);
    }

    @Override
    public void update(UserInfo userInfo) {
        Query query = new Query(Criteria.where("id").is(userInfo.getId()));
        Update update = new Update();
        update.set("user_name", userInfo.getUserName());
        update.set("age", userInfo.getAge());
        //更新查询返回结果集的第一条
        mongoTemplate.updateFirst(query, update, UserInfo.class);
    }

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

    @Override
    public List findUserList() {
        return mongoTemplate.findAll(UserInfo.class);
    }
}

5.dao测试

package com.mochain.epay.mongodb;

import com.mochain.epay.mongodb.dao.UserInfoDao;
import com.mochain.epay.mongodb.entity.UserInfo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.math.BigDecimal;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MongodbTest {

    @Autowired
    private UserInfoDao userInfoDao;

    @Test
    public void add() {
        UserInfo userInfo = new UserInfo();
        for (int i = 0; i < 100; i++) {
            userInfo.setId((long) i);
            userInfo.setUserName("zhangsan" + i);
            userInfo.setAge(22);
            userInfo.setHeight(new BigDecimal("1.68"));
            userInfoDao.insert(userInfo);
        }
    }

    @Test
    public void findById() {
        UserInfo userInfo = userInfoDao.findByUserId((long) 1);
        System.out.println(userInfo.toString());
    }

    @Test
    public void update() {
        UserInfo userInfo = new UserInfo();
        userInfo.setId((long) 2);
        userInfo.setUserName("lisi");
        userInfo.setAge(25);
        userInfo.setHeight(new BigDecimal("1.78"));
        userInfoDao.update(userInfo);
    }

    @Test
    public void delete() {
        userInfoDao.delete((long) 2);
    }

    @Test
    public void list() {
        List userInfo = userInfoDao.findUserList();
        System.out.println(userInfo.toString());
    }

}

你可能感兴趣的:(springboot整合mongodb CRUD)