上篇文章讲到了如何基于MongoTemplate封装通用Repository,只是解决了数据存储层的代码冗余,但是没有解决业务层的冗余,为了解决业务层的代码冗余,封装一个类似Mybatis plus的通用Service,相信使用过Mybstis plus的同学都知道。
- 添加依赖,使用的Springboot 2.7.10
<!-- Mongodb 依赖 MongoTemplate -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- hutool-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.16</version>
</dependency>
- 构建通用Service
构建通用Service,BaseService.java
,内容如下:
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Optional;
public interface BaseService<T,ID> {
//查询所有
List<T> findAll();
//查询所有,根据条件
List<T> findAll(T example);
//根据ID查询
Optional<T> findById(ID id);
//根据ID集合查询
Iterable<T> findAllById(Iterable<ID> ids);
//保存
T save(T entity);
//批量保存
<S extends T> List<S> saveAll(Iterable<S> entities);
//根据ID删除
void deleteById(ID id);
//根据ID批量删除
void deleteAllById(Iterable<? extends ID> ids);
//分页信息查询,可以自定义分页对象,也可以使用Mybatis plus分页对象
Page<T> findByPage(T entity, Pageable pageable);
}
- 构建通用Service的实现
构建通用Service的实现,此处基于MongoRepository
实现,当然也可以使用之前文档自定义通过Repository
实现,BaseServiceImpl
,内容如下:
import com.learning.mongodb.crud.service.BaseService;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;
import java.util.List;
import java.util.Optional;
public abstract class BaseServiceImpl<T, ID> implements BaseService<T, ID> {
protected final MongoRepository<T, ID> mongoRepository;
public BaseServiceImpl(MongoRepository<T, ID> mongoRepository) {
this.mongoRepository = mongoRepository;
}
@Override
public List<T> findAll() {
return mongoRepository.findAll();
}
@Override
public List<T> findAll(T entity) {
ExampleMatcher matcher = ExampleMatcher.matching()
.withNullHandler(ExampleMatcher.NullHandler.IGNORE)
.withIgnoreCase()
.withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING);
Example<T> query = Example.of(entity, matcher);
return mongoRepository.findAll(query);
}
@Override
public Optional<T> findById(ID id) {
return this.mongoRepository.findById(id);
}
@Override
public Iterable<T> findAllById(Iterable<ID> ids) {
return this.mongoRepository.findAllById(ids);
}
@Override
public T save(T entity) {
return this.mongoRepository.save(entity);
}
@Override
public <S extends T> List<S> saveAll(Iterable<S> entities) {
return this.mongoRepository.saveAll(entities);
}
@Override
public void deleteById(ID id) {
this.mongoRepository.deleteById(id);
}
@Override
public void deleteAllById(Iterable<? extends ID> ids) {
this.mongoRepository.deleteAllById(ids);
}
@Override
public Page<T> findByPage(T entity, Pageable pageable) {
ExampleMatcher matcher = ExampleMatcher.matching()
.withNullHandler(ExampleMatcher.NullHandler.IGNORE)
.withIgnoreCase()
.withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING);
Example<T> query = Example.of(entity, matcher);
return this.mongoRepository.findAll(query, pageable);
}
}
- 使用
定义BookMongoRepository.java
public interface BookMongoRepository extends MongoRepository<Book, ObjectId> {}
定义BookService.java
接口
public interface BookService extends BaseService<Book, ObjectId> {}
定义BookServiceImpl.java
实现类
@Service
public class BookServiceImpl extends BaseServiceImpl<Book, ObjectId> implements BookService {
private final BookMongoRepository bookMongoRepository;
public BookServiceImpl(BookMongoRepository bookMongoRepository) {
super(bookMongoRepository);
this.bookMongoRepository = bookMongoRepository;
}
}
定义Book2Controller.java
@RestController
@RequestMapping(value = "/book2")
public class Book2Controller {
@Resource
private BookService bookService;
@PostMapping
public Book add(@RequestBody Book book) {
return this.bookService.save(book);
}
@GetMapping
public List<Book> books() {
return bookService.findAll();
}
@GetMapping(value = "/{id}")
public Book book(@PathVariable(name = "id") String id) {
return bookService.findById(new ObjectId(id)).orElseThrow(() -> new RuntimeException("未知的书籍:" + id));
}
}
测试
curl --location 'http://localhost:8080/book2' \
--header 'Content-Type: application/json' \
--data '{
"name":"Go语言入门",
"price":39.9
}'
总结文章来源:https://www.toymoban.com/news/detail-519845.html
以上就是基于MongoRepository
封装的通用Service,也可以使用之前文章自定义Repository来实现,如果同学们有更好的方案欢迎讨论。文章来源地址https://www.toymoban.com/news/detail-519845.html
到了这里,关于Springboot MongoDB封装通用Servcie的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!