com.feingto.cloud.data.mongodb.BaseService Maven / Gradle / Ivy
package com.feingto.cloud.data.mongodb;
import com.feingto.cloud.data.bean.OrderSort;
import com.feingto.cloud.data.bean.Page;
import com.feingto.cloud.data.mongodb.bean.Condition;
import com.feingto.cloud.kit.reflection.BeanConvertKit;
import com.feingto.cloud.kit.reflection.ReflectionKit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import java.io.Serializable;
import java.util.List;
import java.util.stream.Stream;
/**
* MongoDB Crud 通用接口实现
*
* @author longfei
*/
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public abstract class BaseService implements IBase {
private final Class clazz;
@Autowired
private MongoRepository repository;
@Autowired
private MongoTemplate mongoTemplate;
public BaseService() {
this.clazz = ReflectionKit.getClassGenricType(getClass());
}
@Override
@Transactional(rollbackFor = Exception.class)
public T save(T entity) {
return repository.save(entity);
}
@Override
@Transactional(rollbackFor = Exception.class)
public T update(ID id, T entity) {
return repository.findById(id)
.map(t -> {
BeanConvertKit.copyProperties(entity, t);
return repository.save(t);
})
.orElse(entity);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void updateByProperty(ID id, String property, Object value) {
Assert.notNull(id, "Id is required");
Assert.notNull(property, "Property is required");
repository.findById(id)
.ifPresent(optional -> {
ReflectionKit.setFieldValue(optional, property, value);
repository.save(optional);
});
}
@Override
@Transactional(rollbackFor = Exception.class)
public void delete(ID id) {
Assert.notNull(id, "Id is required");
repository.deleteById(id);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void delete(ID[] ids) {
Stream.of(ids).forEach(this::delete);
}
@Override
public long count() {
return repository.count();
}
@Override
public long count(Example example) {
return repository.count(example);
}
@Override
public T findById(ID id) {
Assert.notNull(id, "Id is required");
return repository.findById(id).orElse(null);
}
@Override
public List findAll() {
return repository.findAll();
}
@Override
public List findAll(OrderSort sort) {
return repository.findAll(Page.createSort(sort));
}
@Override
public List findAll(Example example) {
return repository.findAll(example);
}
@Override
public List findAll(Example example, OrderSort sort) {
return repository.findAll(example, Page.createSort(sort));
}
@Override
public List findAll(Condition condition) {
return mongoTemplate.find(condition.getQuery(), clazz);
}
@Override
public List findAll(Condition condition, OrderSort sort) {
Query query = condition.getQuery();
query.with(Page.createSort(sort));
return mongoTemplate.find(query, clazz);
}
@Override
public Page findByPage(Page page) {
Page.injectPageProperties(page, repository.findAll(Page.createPageable(page)));
return page;
}
@Override
public Page findByPage(Example example, Page page) {
Page.injectPageProperties(page, repository.findAll(example, Page.createPageable(page)));
return page;
}
@Override
public Page findByPage(Condition condition, Page page) {
Query query = condition.getQuery();
query.with(Page.createPageable(page));
return page
.setTotalElements(mongoTemplate.count(query, clazz))
.setContent(mongoTemplate.find(query, clazz));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy