Home > Java > javaTutorial > Share an example tutorial on connecting Java to MongoDB for addition, deletion, modification and query

Share an example tutorial on connecting Java to MongoDB for addition, deletion, modification and query

零下一度
Release: 2017-05-26 14:11:23
Original
2060 people have browsed it

This article mainly introduces the relevant information about Java connection MongoDB to perform additions, deletions, modifications and queries. Friends in need can refer to

Java connection to MongoDB for additions, deletions, modifications and queries. Operations

1. Create a database connection and perform addition, deletion, modification and query

(interface and implementation class respectively)

package com.dao;

import java.util.List;

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

import com.bean.Company;

@Repository
public class RepositoryImpl implements AbstractRepository {
  @Autowired
  private MongoTemplate mongoTemplate;

  // 查询所有数据
  public List<?> findAll(Class<?> entity) {
    return mongoTemplate.findAll(entity);
  }

  // 更新数据
  public Company findOne(String id, Class<?> entity) {
    return (Company) mongoTemplate.findOne(new Query(Criteria.where("id")
        .is(id)), entity);

  }

  // 添加到数据库
  public void updateEntity(Company company) {
    mongoTemplate.save(company);
  }

  // 删除选中的数据
  public void delete(String id, Class<Company> class1) {
    Criteria criteria = Criteria.where("id").in(id);
    if (criteria != null) {
      Query query = new Query(criteria);
      if (query != null && mongoTemplate.findOne(query, class1) != null)
        mongoTemplate.remove(mongoTemplate.findOne(query, class1));
    }

  }
  //增加到数据库
  public void insert(Company company) {
    mongoTemplate.insert(company);
    
  }

}
Copy after login
package com.dao;

import java.util.List;

import com.bean.Company;

public interface AbstractRepository {
  public List<?> findAll(Class<?> entity);
  
  public Company findOne(String id,Class<?> entity);

  public void updateEntity(Company company);

  public void delete(String id, Class<Company> class1);

  public void insert(Company company);
}
Copy after login

Summary: It is the same as the connection of relational database, there is no difference.

【Related Recommendations】

1. Notes on MongoDB Java connection pool

2. MongoDB (6) java Operate mongodb add, delete, modify and query

3. Share an example tutorial on using Spring Boot to develop Restful programs

4. Detailed explanation of examples of using Elasticsearch in spring Tutorial

The above is the detailed content of Share an example tutorial on connecting Java to MongoDB for addition, deletion, modification and query. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template