微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java操作mongodb & springboot整合mongodb

  简单的研究原生API操作MongoDB以及封装的工具类操作,最后也会研究整合spring之后作为dao层的完整的操作。

1.原生的API操作

pom.xml

        <!-- https://mvnrepository.com/artifact/org.mongodb/mongodb-driver -->
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongodb-driver</artifactId>
            <version>3.10.1</version>
        </dependency>

 

测试类:

package mongodb;

import java.util.ArrayList;
import java.util.List;

import org.bson.Document;

import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.ListCollectionsIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;

public class Demo {
    public static void main(String[] args) {
    }

    private static void deleteDoc() {
        try {
            // 连接到 mongodb 服务
            MongoClient mongoClient = new MongoClient("localhost", 27017);

            // 连接到数据库
            MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
            System.out.println("Connect to database successfully");

            MongoCollection<Document> collection = mongoDatabase.getCollection("test");
            System.out.println("集合 test 选择成功");

            collection.deleteOne(Filters.eq("likes", 200));
            // 删除所有符合条件的文档
            collection.deleteMany(Filters.eq("likes", 200));
            // 检索查看结果
            FindIterable<Document> findIterable = collection.find();
            MongoCursor<Document> mongoCursor = findIterable.iterator();
            while (mongoCursor.hasNext()) {
                System.out.println(mongoCursor.next());
            }

        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }
    }

    private static void updateDocument() {
        try {
            // 连接到 mongodb 服务
            MongoClient mongoClient = new MongoClient("localhost", 27017);

            // 连接到数据库
            MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
            System.out.println("Connect to database successfully");

            MongoCollection<Document> collection = mongoDatabase.getCollection("test");
            System.out.println("集合 test 选择成功");

            // 更新文档 将文档中likes=100的文档修改为likes=200
            collection.updateMany(Filters.eq("likes", 100), new Document("$set", new Document("likes", 200)));
            // 检索查看结果
            FindIterable<Document> findIterable = collection.find();
            MongoCursor<Document> mongoCursor = findIterable.iterator();
            while (mongoCursor.hasNext()) {
                System.out.println(mongoCursor.next());
            }

        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }
    }

    private static void queryDocument() {
        try {
            // 连接到 mongodb 服务
            MongoClient mongoClient = new MongoClient("localhost", 27017);

            // 连接到数据库
            MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
            System.out.println("Connect to database successfully");

            MongoCollection<Document> collection = mongoDatabase.getCollection("test");
            System.out.println("集合 test 选择成功");

            // 检索所有文档
            /**
             * 1. 获取迭代器FindIterable<Document> 2. 获取游标MongoCursor<Document> 3.
             * 通过游标遍历检索出的文档集合
             */
            FindIterable<Document> findIterable = collection.find();
            MongoCursor<Document> mongoCursor = findIterable.iterator();
            while (mongoCursor.hasNext()) {
                System.out.println(mongoCursor.next());
            }

        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }
    }

    private static void insertDoc() {
        try {
            // 连接到 mongodb 服务
            MongoClient mongoClient = new MongoClient("localhost", 27017);

            // 连接到数据库
            MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
            System.out.println("Connect to database successfully");

            MongoCollection<Document> collection = mongoDatabase.getCollection("test");
            System.out.println("集合 test 选择成功");

            // 插入文档
            /**
             * 1. 创建文档 org.bson.Document 参数为key-value的格式 2. 创建文档集合List
             * <Document> 3. 将文档集合插入数据库集合中 mongoCollection.insertMany(List
             * <Document>) 插入单个文档可以用 mongoCollection.insertOne(Document)
             */
            Document document = new Document("title", "MongoDB").append("description", "database").append("likes", 100)
                    .append("by", "Fly");
            List<Document> documents = new ArrayList<Document>();
            documents.add(document);
            collection.insertMany(documents);
            System.out.println("文档插入成功");
        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }
    }

    private static void getConnection() {
        try {
            // 连接到 mongodb 服务
            MongoClient mongoClient = new MongoClient("localhost", 27017);

            // 连接到数据库
            MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
            System.out.println("Connect to database successfully");

            // 获取所有的集合
            ListCollectionsIterable<Document> listCollections = mongoDatabase.listCollections();
            MongoCursor<Document> iterator = listCollections.iterator();
            while (iterator.hasNext()) {
                System.out.println(iterator.next());
            }

            MongoCollection<Document> collection = mongoDatabase.getCollection("test");
            System.out.println("集合 test 选择成功");
        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }
    }

    private static void createCollection() {
        try {
            // 连接到 mongodb 服务// 连接到 mongodb 服务
            MongoClient mongoClient = new MongoClient("localhost", 27017);

            // 连接到数据库// 连接到数据库
            MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
            System.out.println("Connect to database successfully");
            mongoDatabase.createCollection("test");
            System.out.println("集合创建成功");

        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }
    }

    private static void connectnopassword() {
        try {
            // 连接到 mongodb 服务
            MongoClient mongoClient = new MongoClient("localhost", 27017);

            // 连接到数据库
            MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
            System.out.println("Connect to database successfully");

        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }
    }
}

 

2.封装成工具类的操作

pom.xml

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.mongodb/mongodb-driver -->
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongodb-driver</artifactId>
            <version>3.10.1</version>
        </dependency>


        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.7</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.2</version>
        </dependency>
    </dependencies>

 

package Utils;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;

public class MongoHelper {

    private static final Logger logger = LoggerFactory.getLogger(MongoHelper.class);

    static final String dbname = "test";
    static final String ServerAddress = "127.0.0.1";
    static final int PORT = 27017;

    private MongoHelper() {
    }

    private static MongoClient mongoClient = new MongoClient(ServerAddress, PORT);

    // 模拟连接池(阻塞队列)
    private static LinkedBlockingQueue<MongoDatabase> mongoDatabases = new LinkedBlockingQueue<MongoDatabase>(5);

    static {
        initMongoDatabases();
    }

    private static void initMongoDatabases() {
        for (int i = 0; i < 5; i++) {
            MongoDatabase mDatabase = mongoClient.getDatabase(dbname);
            mongoDatabases.add(mDatabase);
        }
    }

    public static void closeMongoClient(MongoDatabase mongoDatabase) {
        mongoDatabases.add(mongoDatabase);
        logger.debug("CloseMongoClient successfully");
    }

    public static MongoDatabase getMongoDataBase() {
        try {
            MongoDatabase mDatabase = mongoDatabases.take();
            return mDatabase;
        } catch (InterruptedException e) {
            e.printstacktrace();
            return null;
        }
    }

}

 

package Utils;

import java.util.List;
import java.util.Map;

import org.bson.Document;

import com.mongodb.BasicDBObject;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoDatabase;

/**
 * MongoDB数据操作接口
 * 
 */
public interface MongoDao {

    /**
     * 根据id检索文档
     * 
     * @param db
     * @param table
     * @param id
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryByID(MongoDatabase db, String table, Object id) throws Exception;

    /**
     * 根据doc检索文档集合,当doc是空的时候检索全部
     * 
     * @param db
     * @param table
     * @param doc
     * @return
     * @throws Exception
     */
    public List<Map<String, Object>> queryByDoc(MongoDatabase db, String table, BasicDBObject doc) throws Exception;

    /**
     * 检索全部返回集合
     * 
     * @param db
     * @param table
     * @return
     * @throws Exception
     */
    public List<Map<String, Object>> queryAll(MongoDatabase db, String table) throws Exception;

    /**
     * 遍历迭代器返回文档集合
     * 
     * @param iterable
     * @return
     * @throws Exception
     */
    public List<Document> findIterable(FindIterable<Document> iterable) throws Exception;

    /**
     * 插入文档
     * 
     * @param db
     * @param table
     * @param doc
     * @return
     * @throws Exception
     */
    public boolean insert(MongoDatabase db, String table, Document doc) throws Exception;

    /**
     * 插入多条文档
     * 
     * @param db
     * @param table
     * @param doc
     * @return
     * @throws Exception
     */
    public boolean insertMany(MongoDatabase db, String table, List<Document> doc) throws Exception;

    /**
     * 删除文档
     * 
     * @param db
     * @param table
     * @param doc
     * @return
     * @throws Exception
     */
    public boolean delete(MongoDatabase db, String table, BasicDBObject doc) throws Exception;

    /**
     * 删除单条文档
     * 
     * @param db
     * @param table
     * @param doc
     * @return
     * @throws Exception
     */
    public boolean deleteOne(MongoDatabase db, String table, BasicDBObject doc) throws Exception;

    /**
     * 修改文档
     * 
     * @param db
     * @param table
     * @param oldDoc
     * @param newDoc
     * @return
     * @throws Exception
     */
    public boolean update(MongoDatabase db, String table, BasicDBObject oldDoc, BasicDBObject newDoc) throws Exception;

    /**
     * 修改单条文档
     * 
     * @param db
     * @param table
     * @param whereDoc
     * @param updateDoc
     * @return
     * @throws Exception
     */
    public boolean updateOne(MongoDatabase db, String table, BasicDBObject whereDoc, BasicDBObject updateDoc)
            throws Exception;

    /**
     * 创建集合
     * 
     * @param db
     * @param table
     * @throws Exception
     */
    public void createCol(MongoDatabase db, String table) throws Exception;

    /**
     * 删除集合
     * 
     * @param db
     * @param table
     * @throws Exception
     */
    public void dropCol(MongoDatabase db, String table) throws Exception;

}

 

package Utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mongodb.BasicDBObject;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;

/**
 * MongoDB数据操作实现类
 *
 */
public class MongoDaoImpl implements MongoDao {

    private static final Logger logger = LoggerFactory.getLogger(MongoDaoImpl.class);

    public Map<String, Object> queryByID(MongoDatabase db, String table, Object id) throws Exception {
        MongoCollection<Document> collection = db.getCollection(table);
        BasicDBObject query = new BasicDBObject("_id", id);
        // DBObject接口和BasicDBObject对象:表示一个具体的记录,BasicDBObject实现了DBObject,是key-value的数据结构,用起来和HashMap是基本一致的。
        FindIterable<Document> iterable = collection.find(query);

        Map<String, Object> jsonStrToMap = null;
        MongoCursor<Document> cursor = iterable.iterator();
        while (cursor.hasNext()) {
            Document user = cursor.next();
            String jsonString = user.toJson();
            jsonStrToMap = JsonStrToMap.jsonStrToMap(jsonString);// 这里用到我自己写的方法,主要是包json字符串转换成map格式,为后面做准备,方法放在后面
        }
        logger.debug("检索ID完毕,db:{},table:{},id:{} ", db.getName(), table, id);

        return jsonStrToMap;
    }

    public List<Map<String, Object>> queryByDoc(MongoDatabase db, String table, BasicDBObject doc) throws Exception {
        MongoCollection<Document> collection = db.getCollection(table);
        FindIterable<Document> iterable = collection.find(doc);
        /**
         * 1. 获取迭代器FindIterable<Document> 2. 获取游标MongoCursor
         * <Document> 3.通过游标遍历检索出的文档集合
         */

        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        MongoCursor<Document> cursor = iterable.iterator();
        while (cursor.hasNext()) {
            Document user = cursor.next();
            String jsonString = user.toJson();
            Map<String, Object> jsonStrToMap = JsonStrToMap.jsonStrToMap(jsonString);
            list.add(jsonStrToMap);
        }
        logger.debug("检索doc完毕,db:{},table:{},doc:{} ", db.getName(), table, doc.toJson());
        return list;
    }

    public List<Map<String, Object>> queryAll(MongoDatabase db, String table) throws Exception {
        MongoCollection<Document> collection = db.getCollection(table);
        FindIterable<Document> iterable = collection.find();

        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        MongoCursor<Document> cursor = iterable.iterator();
        while (cursor.hasNext()) {
            Document user = cursor.next();
            String jsonString = user.toJson();
            Map<String, Object> jsonStrToMap = JsonStrToMap.jsonStrToMap(jsonString);
            list.add(jsonStrToMap);
        }
        logger.debug("检索全部完毕,db:{},table:{}", db.getName(), table);
        return list;
    }

    public List<Document> findIterable(FindIterable<Document> iterable) throws Exception {
        List<Document> list = new ArrayList<Document>();
        MongoCursor<Document> cursor = iterable.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            list.add(doc);
        }
        cursor.close();
        return list;
    }

    public boolean insert(MongoDatabase db, String table, Document doc) throws Exception {
        MongoCollection<Document> collection = db.getCollection(table);
        collection.insertOne(doc);
        long count = collection.count(doc);
        if (count >= 1) {
            logger.debug("文档插入成功,影响条数:{},db:{},table:{},doc:{} ", count, db.getName(), table, doc.toJson());
            return true;
        } else {
            logger.debug("文档插入失败,影响条数:{},db:{},table:{},doc:{} ", count, db.getName(), table, doc.toJson());
            return false;
        }

    }

    public boolean insertMany(MongoDatabase db, String table, List<Document> doc) throws Exception {

        MongoCollection<Document> collection = db.getCollection(table);
        long preCount = collection.count();
        collection.insertMany(doc);
        long NowCount = collection.count();

        if ((NowCount - preCount) == doc.size()) {
            logger.debug("文档插入成功,影响条数:{},db:{},table:{}", doc.size(), db.getName(), table);
            return true;
        } else {
            logger.debug("文档插入失败,影响条数:{},db:{},table:{}", (NowCount - preCount), db.getName(), table);
            return false;
        }

    }

    public boolean delete(MongoDatabase db, String table, BasicDBObject doc) throws Exception {
        MongoCollection<Document> collection = db.getCollection(table);
        DeleteResult deleteManyResult = collection.deleteMany(doc);
        long deletedCount = deleteManyResult.getDeletedCount();

        if (deletedCount > 0) {
            logger.debug("文档删除成功,影响条数:{},db:{},table:{},doc:{} ", deletedCount, db.getName(), table, doc.toJson());
            return true;
        } else {
            logger.debug("文档删除失败,影响条数:{},db:{},table:{},doc:{} ", 0, db.getName(), table, doc.toJson());
            return false;
        }
    }

    public boolean deleteOne(MongoDatabase db, String table, BasicDBObject doc) throws Exception {
        MongoCollection<Document> collection = db.getCollection(table);
        DeleteResult deleteOneResult = collection.deleteOne(doc);
        long deletedCount = deleteOneResult.getDeletedCount();
        System.out.println("删除数量: " + deletedCount);
        if (deletedCount == 1) {
            logger.debug("文档删除成功,影响条数:{},db:{},table:{},doc:{} ", deletedCount, db.getName(), table, doc.toJson());
            return true;
        } else {
            logger.debug("文档删除失败,影响条数:{},db:{},table:{},doc:{} ", 0, db.getName(), table, doc.toJson());
            return false;
        }
    }

    public boolean update(MongoDatabase db, String table, BasicDBObject whereDoc, BasicDBObject updateDoc)
            throws Exception {
        MongoCollection<Document> collection = db.getCollection(table);
        UpdateResult updateManyResult = collection.updateMany(whereDoc, new Document("$set", updateDoc));
        long modifiedCount = updateManyResult.getModifiedCount();
        System.out.println("修改数量: " + modifiedCount);

        if (modifiedCount > 0) {
            logger.debug("文档更新成功,影响条数:{},db:{},table:{},whereDoc:{},updateDoc:{} ", modifiedCount, db.getName(), table,
                    whereDoc.toJson(), updateDoc.toJson());
            return true;
        } else {
            logger.debug("文档更新成功,影响条数:{},db:{},table:{},whereDoc:{},updateDoc:{} ", 0, db.getName(), table,
                    whereDoc.toJson(), updateDoc.toJson());
            return false;
        }
    }

    public boolean updateOne(MongoDatabase db, String table, BasicDBObject whereDoc, BasicDBObject updateDoc)
            throws Exception {
        MongoCollection<Document> collection = db.getCollection(table);
        UpdateResult updateOneResult = collection.updateOne(whereDoc, new Document("$set", updateDoc));
        long modifiedCount = updateOneResult.getModifiedCount();
        System.out.println("修改数量: " + modifiedCount);
        if (modifiedCount == 1) {
            logger.debug("文档更新成功,影响条数:{},db:{},table:{},whereDoc:{},updateDoc:{} ", 1, db.getName(), table,
                    whereDoc.toJson(), updateDoc.toJson());
            return true;
        } else {
            logger.debug("文档更新成功,影响条数:{},db:{},table:{},whereDoc:{},updateDoc:{} ", 0, db.getName(), table,
                    whereDoc.toJson(), updateDoc.toJson());
            return false;
        }
    }

    public void createCol(MongoDatabase db, String table) throws Exception {
        db.createCollection(table);
        logger.debug("集合创建成功,db:{},table:{}", db.getName(), table);
    }

    public void dropCol(MongoDatabase db, String table) throws Exception {
        db.getCollection(table).drop();
        logger.debug("集合删除成功,db:{},table:{}", db.getName(), table);

    }

}

 

package Utils;

import java.util.HashMap;
import java.util.Map;

import com.mongodb.util.JSON;

public class JsonStrToMap {

    /**
     * json 字符串转化为map格式
     * 
     * @param jsonString
     * @return
     */

    public static Map<String, Object> jsonStrToMap(String jsonString) {
        Object parSEObj = JSON.parse(jsonString); // 反序列化 把json 转化为对象
        Map<String, Object> map = (HashMap<String, Object>) parSEObj; // 把对象转化为map
        return map;
    }

}

 

测试类:

package mongodb;

import java.util.List;
import java.util.Map;

import org.bson.Document;

import com.mongodb.client.MongoDatabase;

import Utils.MongoDao;
import Utils.MongoDaoImpl;
import Utils.MongoHelper;

public class UtilsTest {
    public static void main(String[] args) throws Exception {
        // 增加文档

        MongoDao mongoDao = new MongoDaoImpl();
        String table = "test";
        for (int i = 0; i < 6; i++) {
            MongoDatabase db = MongoHelper.getMongoDataBase();
            List<Map<String, Object>> queryAll = mongoDao.queryAll(db, "test");
            System.out.println(queryAll);
        }

    }

    private static void insertDoc(MongoDatabase db, MongoDao mongoDao, String table) throws Exception {
        Document document = new Document("title222", "MongoDB222").append("description", "database")
                .append("likes", 100).append("by", "Fly");
        mongoDao.insert(db, table, document);
    }
}

结果会打印五次结果,并且阻塞一次。

上面代码改为下面即可正常打印6次:

        for (int i = 0; i < 6; i++) {
            MongoDatabase db = MongoHelper.getMongoDataBase();
            List<Map<String, Object>> queryAll = mongoDao.queryAll(db, "test");
            System.out.println(queryAll);
            MongoHelper.closeMongoClient(db);
        }

 

3.springboot整合mongoDB的使用

 pom.xml文件引入自动整合的jar包:

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-mongodb -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
            <version>2.1.4.RELEASE</version>
        </dependency>

application.properties文件引入相关的配置

##mongodb相关配置
spring.data.mongodb.host=127.0.0.1
spring.data.mongodb.port=27017
spring.data.mongodb.database=mydb
spring.data.mongodb.username=admin
spring.data.mongodb.password=admin

相关配置的类在   MongoDataAutoConfiguratio  类中。

注意:上面是需要账户和密码的用户的配置方式,其用户和密码是在mongodb的admin数据库中创建的用户(这点有点类似于MysqL),语句如下:

> use admin
switched to db admin
> db.createuser({"user":"root",pwd:"123456",roles:[{role:"root",db:"admin"}]})
Successfully added user: {
        "user" : "root",
        "roles" : [
                {
                        "role" : "root",
                        "db" : "admin"
                }
        ]
}

 

实体类User.java

package cn.qs.bean.user;

import org.springframework.data.annotation.Id;

import java.util.Date;

public class User {
    @Id
    private Integer id;

    private String username;

    private String password;

    private String fullname;

    private String sex;

    private String phone;

    private String email;

    private Date createtime;

    private Date updatetime;

    public Integer getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username == null ? null : username.trim();
    }

    public String getpassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname == null ? null : fullname.trim();
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex == null ? null : sex.trim();
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone == null ? null : phone.trim();
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email == null ? null : email.trim();
    }

    public Date getCreatetime() {
        return createtime;
    }

    public void setCreatetime(Date createtime) {
        this.createtime = createtime;
    }

    public Date getUpdatetime() {
        return updatetime;
    }

    public void setUpdatetime(Date updatetime) {
        this.updatetime = updatetime;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", fullname='" + fullname + '\'' +
                ", sex='" + sex + '\'' +
                ", phone='" + phone + '\'' +
                ", email='" + email + '\'' +
                ", createtime=" + createtime +
                ", updatetime=" + updatetime +
                '}';
    }
}

 

UserDao.java

package cn.qs.dao;

import cn.qs.bean.user.User;
import cn.qs.bean.user.UserExample;
import org.springframework.data.mongodb.repository.MongoRepository;

import java.util.List;

/**
 * @Author: qlq
 * @Description
 * @Date: 22:06 2019/4/8
 */
public interface UserDao {
    void insert(User record);
    void deleteByPrimaryKey(Integer id);
    void updateByPrimaryKeySelective(User record);
    void updateByPrimaryKey(User record);
    List<User> selectUsers();
}


UserDaoImpl.java(  mongoTemplate 封装了好多方法,其中还包括查询删除方法  )

package cn.qs.dao;

import cn.qs.bean.user.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.Component;

import java.util.List;

/**
 * @Author: qlq
 * @Description
 * @Date: 22:18 2019/4/8
 */

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

    @Override
    public void insert(User record) {
        mongoTemplate.save(record);
    }

    @Override
    public void deleteByPrimaryKey(Integer id) {
        Query query=new Query(Criteria.where("id").is(id));
        mongoTemplate.remove(query,User.class);
    }

    @Override
    public void updateByPrimaryKeySelective(User record) {
        Query query=new Query(Criteria.where("id").is(record.getId()));
        Update update= new Update().set("fullname", record.getFullname()).set("username", record.getUsername());
        //更新查询返回结果集的第一条
        mongoTemplate.updateFirst(query,update,User.class);
        //更新查询返回结果集的所有
        // mongoTemplate.updateMulti(query,update,User.class);
    }

    @Override
    public void updateByPrimaryKey(User record) {
    }

    @Override
    public List<User> selectUsers() {
        Query query=new Query(Criteria.where("fullname").is("张三"));
        //查询单个
//        User user =  mongoTemplate.findOne(query , User.class);
        List<User> users =  mongoTemplate.find(query,User.class);
        return users;
    }
}

 

UserMongoDbController.java(这里测试直接注入dao)

package cn.qs.controller.user;

import cn.qs.bean.user.User;
import cn.qs.dao.UserDao;
import cn.qs.service.user.UserService;
import cn.qs.utils.DefaultValue;
import cn.qs.utils.JSONResultUtil;
import cn.qs.utils.MD5Util;
import cn.qs.utils.ValidateCheck;
import com.github.pageHelper.pageHelper;
import com.github.pageHelper.PageInfo;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController /** 自动返回的是json格式数据 ***/
@RequestMapping("userMon")
public class UserMongoDbController {

    private static final Logger logger = LoggerFactory.getLogger(UserMongoDbController.class);

    @Autowired
    private UserDao userDao;

    @RequestMapping("/insert")
    public String insert(User record){
        for(int i=0;i<10;i++){
            User user = new User();
            user.setId(i+1);
            user.setFullname("张三");
            user.setUsername("zhangsan");
            userDao.insert(user);
        }

        return "s";
    }

    @RequestMapping("/deleteByPrimaryKey")
    public String deleteByPrimaryKey(Integer id){
        return "s";
    }

    @RequestMapping("/updateByPrimaryKeySelective")
    public String updateByPrimaryKeySelective(User record){
        return "s";
    }

    @RequestMapping("/updateByPrimaryKey")
    public String updateByPrimaryKey(User record){
        return "s";
    }

    @RequestMapping("/selectUsers")
    public List<User> selectUsers(){
        return userDao.selectUsers();
    }
}

 

  启动之后调用增加方法然后查看mongodb的数据信息:(User的ID做为mongodb的docid)

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐