项目作者: biggates

项目描述 :
datatables binding for spring-data-mongodb
高级语言: Java
项目地址: git://github.com/biggates/spring-data-mongodb-datatables.git
创建时间: 2016-08-04T06:39:45Z
项目社区:https://github.com/biggates/spring-data-mongodb-datatables

开源协议:

下载


spring-data-mongodb-datatables

Datatables binding for spring-data-mongodb.

This is a sample project showing how it works.

This project is inspired from darrachequesne/spring-data-jpa-datatables, which works with spring-data-jpa.

Deprecation

Because Damien now has darrachequesne/spring-data-mongodb-datatables released, and this project has its own modification on DataTablesInput, I think it is time to mark it as @Deprecated now.

As Damien’s implementation does not implement Aggregation, this repository will remain open , but will NOT be maintained anymore.

If you need similar features (customizable query with spring-data) but don’t use DataTables, my new project eaphone-spring-data-query will be another choice.

Usage

Basic usage is the same with darrachequesne/spring-data-jpa-datatables

Introduce into project

TODO Not uploaded to any public Maven Repository yet.

  1. <dependency>
  2. <groupId>com.eaphone</groupId>
  3. <artifactId>spring-data-mongodb-datatables</artifactId>
  4. <version>0.3.2-SNAPSHOT</version>
  5. </dependency>

Initialization

In any @Configuration class, add:

  1. @EnableMongoRepositories(repositoryFactoryBeanClass = DataTablesRepositoryFactoryBean.class)

Write new Repo

Just as spring-data-mongodb does:

  1. @Repository
  2. public interface UserRepository extends DataTablesRepository<Order, String> {
  3. }

Note that DataTablesRepository extends PagingAndSortingRepository so it already contains functionalities like findAll(Pageable) and save().

Expose fields on view

  1. @Data
  2. @Document(collection = "order")
  3. public class Order {
  4. @Id
  5. @JsonView(DataTablesOutput.View.class)
  6. private String id;
  7. @JsonFormat(pattern = "yyyy-MM-dd")
  8. @JsonView(DataTablesOutput.View.class)
  9. private Date date;
  10. @JsonView(DataTablesOutput.View.class)
  11. private String orderNumber;
  12. @JsonView(DataTablesOutput.View.class)
  13. private boolean isValid;
  14. @JsonView(DataTablesOutput.View.class)
  15. private int amount;
  16. @JsonView(DataTablesOutput.View.class)
  17. private double price;
  18. }

On the browser side

Include jquery.spring-friendly.js so column[0][data] is changed to column[0].data and is correctly parsed by SpringMVC.

On the Server Side

The repository has the following methods:

  • Using Query
    • DataTablesOutput<T> findAll(DataTablesInput input);
    • DataTablesOutput<T> findAll(DataTablesInput input, Criteria additionalCriteria);
    • DataTablesOutput<T> findAll(DataTablesInput input, Criteria additionalCriteria, Criteria preFilteringCriteria);
  • Using Aggregation
    • <View> DataTablesOutput<View> findAll(Class<View> classOfView, DataTablesInput input, AggregationOperation... operations);
    • <View> DataTablesOutput<View> findAll(Class<View> classOfView, DataTablesInput input, Collection<? extends AggregationOperation> operations);

Examples

  1. @GetMapping("/data/orders")
  2. public DataTablesOutput<Order> getOrders(@Valid DataTablesInput input) {
  3. return repo.findAll(input);
  4. }

Or:

  1. import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
  2. @GetMapping("/")
  3. public DataTablesOutput<DataView> getAll(@Valid DataTablesInput input) {
  4. return repo.findAll(DataView.class,
  5. input,
  6. // just provide your aggregation pipeline here
  7. lookup("user", "userId", "id", "user"),
  8. unwind("user"),
  9. project()
  10. .and("user.sex").as("user.gender")
  11. .andInclude(
  12. "timestamp", "createTime", "sensorType",
  13. "batchId", "source", "beginTime",
  14. "endTime", "text", "url", "value")
  15. );
  16. }

Filter

In addition to DataTables’ columns[x].search parameters, columns[x].filter is a new way to define more complex queries.

A more detailed document in Simplified Chinese (zh_CN) is provided here.

Future Plans

In the near future:

  • In Criteria converting, more types (Date) must be handled, as currently only String and Boolean are handled
  • More tests (and verifications)

Known Issues

  • $match, $sum: 1, $limit and $skip are attached to given aggregation pipeline so in some cases the logic may be broken.
  • Text search is simply converted to Regular Expressions with Literal flag and may contain some logical flaws.
  • Global search is NOT implementd yet (as discussed in #1).
  • Querydsl support is REMOVED, as my own project does not use it.