How to Build Complex Search APIs with Bean Searcher in One Line of Code
Learn how Bean Searcher enables Java backend developers to replace dozens of lines of ORM code with a single, flexible query method, supporting multi-table joins, dynamic filters, pagination, sorting, and custom operators, dramatically boosting development efficiency for complex list retrieval scenarios.
Two Tables
Define a user table (id bigint, name varchar(45), age int, role_id int) and a role table (id int, name varchar(45)).
User Query Requirements
Filter by username with exact, full fuzzy, prefix, suffix matching and optional case‑insensitivity.
Filter by age with exact, greater‑than, less‑than, and range matching.
Filter by role ID (exact) and user ID (exact, same as age).
Select specific columns (e.g., id, name, role).
Support pagination, sorting by any field, and custom operators for each parameter.
Backend Interface Implementation
Using Bean Searcher the entire endpoint can be written in a single line of code, handling all the above requirements automatically.
Add Dependency
<code><dependency>
<groupId>com.ejlchina</groupId>
<artifactId>bean-searcher-boot-starter</artifactId>
<version>3.1.2</version>
</dependency></code>Entity Class
<code>@SearchBean(tables="user u, role r", joinCond="u.role_id = r.id", autoMapTo="u")
public class User {
private Long id; // user ID
private String name; // username
private int age; // age
private int roleId; // role ID
@DbField("r.name")
private String role; // role name
// getters and setters ...
}</code>Controller
<code>@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private MapSearcher mapSearcher;
@GetMapping("/index")
public SearchResult<Map<String, Object>> index(HttpServletRequest request) {
// one line of code
return mapSearcher.search(User.class, MapUtils.flat(request.getParameterMap()));
}
}</code>The
MapSearcherinterprets request parameters such as
name,
name-op,
age,
age-op,
size,
page,
sort,
order,
onlySelect,
selectExclude, etc., applying the appropriate operators (
eq,
ne,
gt,
lt,
ge,
le,
bt,
mv,
in,
sw,
ew,
ey,
ny) and case‑insensitivity flags.
Examples:
GET /user/index – returns the first page with default size (15).
GET /user/index?size=10&page=2 – returns page 2 with 10 records per page.
GET /user/index?sort=ageℴ=desc – sorts results by age descending.
GET /user/index?onlySelect=id,name,role – returns only the specified columns.
GET /user/index?age=20&age-op=ge – filters users with age ≥ 20.
GET /user/index?name=Jack&name-op=sw&name-ic=true – case‑insensitive prefix match.
Bean Searcher also supports operator constraints (e.g., limiting a field to only
Equaland
StartWith), conditional exclusion of fields from WHERE clauses, and global parameter filters for custom preprocessing.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.