第二章:简单的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;
}
复制代码