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<String> ITERABLE_CLASSES;
   private final MongoConverter mongoConverter;
   private final MappingContext<? extends MongoPersistentEntity<?>, 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<? extends Object> batchToSave, Class<?> entityClass)

void insert(Collection<? extends Object> batchToSave, String collectionName)

void insert(Object objectToSave)

void insert(Object objectToSave, String collectionName)

void insertAll(Collection<? extends Object> objectsToSave)

void save(Object objectToSave)

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

<T> T findAndRemove(Query query, Class<T> 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> T findAndModify(Query query, Update update, Class<T> entityClass)

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

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

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

<T> List<T> find(Query query, Class<T> entityClass, String collectionName)

<T> List<T> findAll(Class<T> entityClass)

<T> List<T> findAll(Class<T> entityClass, String collectionName)

<T> List<T> findAllAndRemove(Query query, Class<T> entityClass)

<T> List<T> findAllAndRemove(Query query, Class<T> entityClass, String collectionName)

<T> List<T> findAllAndRemove(Query query, String collectionName)

<T> T findById(Object id, Class<T> entityClass)

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

<T> T findOne(Query query, Class<T> entityClass)

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

二、Example

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

public void saveAll(List<PhysicalTestLog> 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<PhysicalTestLog> 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<PhysicalTestLog> list = mongoTemplate.find(query.with(pageable), PhysicalTestLog.class);
    return list;
}
复制代码

官方文档传送门

第一章:Document定义章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值