POM文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
Demo实体
@Data
// 文档名
@Document("demo_document")
@Accessors(chain = true)
public class DemoDocument implements Serializable{
@Id
private String id;
/* 样例字段 */
// 业务类型
private String bizType;
// 配置项
private JSONObject config;
}
持久层
@Repository
public interface DemoDocumentRepository extends MongoRepository<DemoDocument, String>{
// 自定义查询
@Query({"{'bizType':?0}"})
List<DemoDocument> findAllByBizType(String bizType);
}
Service层使用
这里懒了,,不做接口了
@Service
public class DemoDocumentService {
@Autowired
private DemoDocumentRepository demoDocumentRepository;
// 新增
public DemoDocument insertDemo(DemoDocument demo){
return demoDocumentRepository.insert(demo);
}
// 根据id删除
public void delete(String id){
demoDocumentRepository.deleteById(id);
}
// 根据业务类型获取文档集合
public List<DemoDocument> listByBizType(String bizType){
return demoDocumentRepository.findAllByBizType(bizType);
}
}