MongoTemplate 教程系列(二)

第二章:简单的CRUD

在上一篇文章,讲了spring-data-mongodb对于文档的注解,并定义了PT的Document 。在这一篇文章我们开始针对“PT”进行常用的增删改查(CRUD)操作。

一、mongoTemplate简介

org.springframework.data.mongodb.core.MongoTemplate

部分源码:

public class MongoTemplate implements MongoOperations, ApplicationContextAware {
   private static final Logger LOGGER = LoggerFactory.getLogger(MongoTemplate.class);
   private static final String ID_FIELD = "_id";
   private static final WriteResultChecking DEFAULT_WRITE_RESULT_CHECKING;
   private static final Collection ITERABLE_CLASSES;
   private final MongoConverter mongoConverter;
   private final MappingContext, MongoPersistentProperty> mappingContext;
   private final MongoDbFactory mongoDbFactory;
   private final PersistenceExceptionTranslator exceptionTranslator;
   private final QueryMapper queryMapper;
   private final UpdateMapper updateMapper;
   private WriteConcern writeConcern;
   private WriteConcernResolver writeConcernResolver;
   private WriteResultChecking writeResultChecking;
   private ReadPreference readPreference;
   private ApplicationEventPublisher eventPublisher;
   private ResourceLoader resourceLoader;
   private MongoPersistentEntityIndexCreator indexCreator;
   .....
复制代码

实现接口有: MongoOperations, ApplicationContextAware, IndexOperationsProvider

主要实现接口为: MongoOperations

构造函数:

MongoTemplate(com.mongodb.MongoClient mongoClient, String databaseName)
Constructor used for a basic template configuration

MongoTemplate(MongoDbFactory mongoDbFactory)
Constructor used for a basic template configuration.

MongoTemplate(MongoDbFactory mongoDbFactory, MongoConverter mongoConverter)
Constructor used for a basic template configuration.
复制代码

方法:

在此只列举CRUD相关的部分方法

void insert(Collection batchToSave, Class entityClass)

void insert(Collection batchToSave, String collectionName)

void insert(Object objectToSave)

void insert(Object objectToSave, String collectionName)

void insertAll(Collection objectsToSave)

void save(Object objectToSave)

void save(Object objectToSave, String collectionName)
复制代码
 T findAndRemove(Query query, Class entityClass)

 T findAndRemove(Query query, Class entityClass, String collectionName)

com.mongodb.client.result.DeleteResult	remove(Object object)

com.mongodb.client.result.DeleteResult	remove(Object object, String collectionName)

com.mongodb.client.result.DeleteResult	remove(Query query, Class entityClass)

com.mongodb.client.result.DeleteResult	remove(Query query, Class entityClass, String collectionName)

com.mongodb.client.result.DeleteResult	remove(Query query, String collectionName)
复制代码
 T findAndModify(Query query, Update update, Class entityClass)

 T findAndModify(Query query, Update update, Class entityClass, String collectionName)

 T findAndModify(Query query, Update update, FindAndModifyOptions options, Class entityClass)

 T findAndModify(Query query, Update update, FindAndModifyOptions options, Class entityClass, String collectionName)
复制代码
  • 查询
 List find(Query query, Class entityClass)

 List find(Query query, Class entityClass, String collectionName)

 List findAll(Class entityClass)

 List findAll(Class entityClass, String collectionName)

 List findAllAndRemove(Query query, Class entityClass)

 List findAllAndRemove(Query query, Class entityClass, String collectionName)

 List findAllAndRemove(Query query, String collectionName)

 T findById(Object id, Class entityClass)

 T findById(Object id, Class entityClass, String collectionName)

 T findOne(Query query, Class entityClass)

 T findOne(Query query, Class entityClass, String collectionName)
复制代码

二、Example

public void save(PhysicalTestLog physicalTestLog) {
    mongoTemplate.insert(physicalTestLog);
}

public void saveAll(List physicalTestLogs) {
    mongoTemplate.insertAll(physicalTestLogs);
}

public void delete(String id) {
    Query query = new Query(Criteria.where("id").is(id));
    mongoTemplate.remove(query, PhysicalTestLog.class);
}

public PhysicalTestLog findById(String id) {
    Query query = new Query(Criteria.where("id").is(id));
    return mongoTemplate.findOne(query, PhysicalTestLog.class);
}

public PhysicalTestLog findByName(String name) {
    Query query = new Query(Criteria.where("name").is(name));
    return mongoTemplate.findOne(query, PhysicalTestLog.class);
}

public List findByPage(PhysicalTestLog physicalTestLog, Pageable pageable) {
    Query query = new Query();
    if (physicalTestLog != null && physicalTestLog.getSchool_id() != null) {
        //模糊查询
        query = new Query(Criteria.where("school_id").regex("^" + physicalTestLog.getSchool_id()));
    }
    List list = mongoTemplate.find(query.with(pageable), PhysicalTestLog.class);
    return list;
}
复制代码

官方文档传送门

第一章:Document定义章

你可能感兴趣的:(MongoTemplate 教程系列(二))