MongoTemplate 教程系列(二)

弱冠季首發表於2018-02-09

第二章:簡單的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定義章

相關文章