Quick Start
1. Define Your Entity
Simple approach (no annotations needed):
import java.time.LocalDate;
// Works without any annotations!
// Java camelCase is automatically converted to database snake_case
public class Employee {
private Long id; // Maps to: id
private String firstName; // Maps to: first_name
private String lastName; // Maps to: last_name
private String email; // Maps to: email
private Double salary; // Maps to: salary
private LocalDate hireDate; // Maps to: hire_date
private Boolean active; // Maps to: active
// Getters and setters...
}
// Queries table: "employee" (converted to snake_case)
// Columns: id, first_name, last_name, email, salary, hire_date, activeWith annotations (only when you need to override defaults):
import com.dev10.picnic.annotations.*;
import java.time.LocalDate;
// Use annotations only when your table/column names differ from defaults
@Table("employees") // Table name is "employees", not "employee"
public class Employee {
@Id
private Long id;
@Column("firstname")
private String firstName;
@Column("LAST_NAME")
private String lastName;
private String email; // No annotation needed - uses "email" as column name
private Double salary; // No annotation needed - uses "salary" as column name
private LocalDate hireDate; // No annotation needed - uses "hire_date" as column name
private Boolean active;
// Getters and setters...
}2. Create GridDataProvider Instance
import com.dev10.picnic.GridDataProvider;
import javax.sql.DataSource;
import java.sql.Connection;
// Option 1: Using DataSource (recommended for production)
DataSource dataSource = // ... obtain from Spring or connection pool
GridDataProvider gridDataProvider = new GridDataProvider(dataSource);
// Option 2: Using Connection (caller manages lifecycle)
try (Connection conn = dataSource.getConnection()) {
GridDataProvider gridDataProvider = new GridDataProvider(conn);
// Execute queries...
}3. Execute Query with Fluent API (Recommended)
import com.dev10.picnic.beans.DevExtremeResult;
import com.dev10.picnic.beans.devextreme.DevExtremeLoadOptions;
// Parse client request
DevExtremeLoadOptions loadOptions = DevExtremeLoadOptionsParser.fromJson(jsonRequest);
// Execute query using fluent API
DevExtremeResult<Employee> result = gridDataProvider
.query(Employee.class)
.loadOptions(loadOptions)
.executeDevExtreme();
// Access results
List<Employee> employees = (List<Employee>) result.getData();
Integer totalCount = result.getTotalCount();4. REST Controller Example (Spring Boot)
@RestController
@RequestMapping("/api/employees")
public class EmployeeController {
@Autowired
private GridDataProvider gridDataProvider; // Auto-configured bean
@PostMapping("/data")
public DevExtremeResult<Employee> getEmployees(@RequestBody DevExtremeLoadOptions loadOptions) {
return gridDataProvider
.query(Employee.class)
.loadOptions(loadOptions)
.executeDevExtreme();
}
}Fluent Query Builder API
The fluent Query Builder API provides a readable, chainable interface for building and executing queries.
Basic Usage
DevExtremeResult<Employee> result = gridDataProvider
.query(Employee.class)
.loadOptions(loadOptions)
.executeDevExtreme();With Optional Configuration
DevExtremeResult<Employee> result = gridDataProvider
.query(Employee.class)
.loadOptions(loadOptions)
.schema("hr") // Database schema
.withParameter("departmentId", 5) // WITH clause parameter
.withParameter("startDate", LocalDate.now()) // Multiple parameters
.customFilter(() -> "active = true") // Custom SQL filter
.executeDevExtreme();With Row Processing
DevExtremeResult<Sale> result = gridDataProvider
.query(Sale.class)
.loadOptions(loadOptions)
.rowProcessor(new RunningSumProcessor())
.contextMetadata("userId", currentUser.getId())
.executeDevExtreme();Available Builder Methods
| Method | Description |
|---|---|
loadOptions(LoadOptions) | Set the load options (required) |
schema(String) | Specify database schema |
withParameter(String, Object) | Add a WITH clause parameter |
withParameters(Map) | Add multiple WITH clause parameters |
customFilter(CustomFilter) | Add a custom SQL filter |
customFilters(CustomFilter...) | Add multiple custom filters |
rowProcessor(RowProcessor) | Add entity transformation processor |
contextMetadata(String, Object) | Add metadata for row processor |
contextMetadata(Map) | Add multiple metadata entries |
pivotResultFieldSeparator(String) | Set separator for AG Grid pivot columns (experimental) |
executeDevExtreme() | Execute and return DevExtremeResult |
executeAGGrid() | Execute and return AGGridResult (experimental) |
executeGeneric() | Execute and return Object (requires casting) |
Last updated on