Master FastMyBatis: Quick Spring Boot Integration and CRUD Guide
This article introduces FastMyBatis—a lightweight, zero‑configuration MyBatis extension for Spring Boot—covering its core principles, Maven setup, sample CRUD controller, service and mapper implementations, a comprehensive list of mapper methods, Query object usage examples, and links to the open‑source repository.
Overview
FastMyBatis is a MyBatis‑based development framework that emphasizes simplicity, speed and zero‑configuration. It supports MySQL, SQLServer, Oracle, PostgreSQL and SQLite. CRUD operations can be performed without XML mapping files.
Quick start with Spring Boot
Add the starter dependency to your Maven project:
<dependency>
<groupId>net.oschina.durcframework</groupId>
<artifactId>fastmybatis-spring-boot-starter</artifactId>
<version>latest</version>
</dependency>After the starter is on the classpath, FastMyBatis automatically scans mapper interfaces and creates a generic IService implementation.
Entity, Mapper and Service
Assume a table t_user with primary key id. Create a POJO and a mapper interface:
public class TUser {
private Integer id;
private String username;
private Integer age;
// getters & setters
} public interface TUserMapper extends CrudMapper<TUser, Integer> {
// inherits all CRUD methods
}The service can be declared as:
@Service
public class UserService implements IService<TUser, Integer> {
// implementation is provided by FastMyBatis at runtime
}Typical CRUD controller
@RestController
public class CrudController {
@Autowired
private UserService userService;
@GetMapping("/user/page")
public Result<PageInfo<TUser>> page(UserParam param) {
Query query = param.toQuery();
PageInfo<TUser> pageInfo = userService.page(query);
return Result.ok(pageInfo);
}
@GetMapping("/user/save")
public Result<Integer> save(TUser user) {
userService.saveIgnoreNull(user);
return Result.ok(user.getId());
}
@GetMapping("/user/update")
public Result<Void> update(TUser user) {
userService.updateIgnoreNull(user);
return Result.ok();
}
@GetMapping("/user/delete")
public Result<Void> delete(Integer id) {
userService.deleteById(id);
return Result.ok();
}
}Key mapper methods
E getById(I id)– retrieve a record by primary key. int save(E entity) – insert all fields. int saveIgnoreNull(E entity) – insert non‑null fields only. int update(E entity) – update all fields. int updateIgnoreNull(E entity) – update non‑null fields only. int deleteById(I id) – logical delete (sets delete flag). int forceDeleteById(I id) – physical delete. List<E> list(Query query) – query list. long getCount(Query query) – total count. PageInfo<E> page(Query query) – paginated query.
Using the Query object
The Query class provides a fluent API for building conditions.
// Example 1: exact match
Query q1 = new Query().eq("username", "Zhang San").eq("age", 22);
List<TUser> users1 = userService.list(q1);
// Example 2: IN clause
Query q2 = new Query().in("age", Arrays.asList(10, 20, 30));
List<TUser> users2 = userService.list(q2);
// Example 3: pagination (page index starts at 1)
Query q3 = new Query().eq("age", 10).page(1, 10);
PageInfo<TUser> page = userService.page(q3);
long total = userService.getCount(q3);Advanced query examples
// Greater‑than condition
Query q4 = new Query().gt("reg_date", someDate);
// Range and ordering
Query q5 = new Query()
.eq("gender", 1)
.ge("age", 20)
.orderby("age", Sort.DESC);
List<TUser> list = userService.list(q5);Open‑source repository
FastMyBatis source code and documentation are hosted on Gitee:
Repository: https://gitee.com/durcframework/fastmybatis
Documentation: https://durcframework.gitee.io/fastmybatis/#/
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
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.
