FastMyBatis: A Simple, Fast, and Efficient MyBatis Extension for Spring Boot
This article introduces FastMyBatis, a zero‑configuration MyBatis extension that simplifies CRUD operations in Spring Boot projects, provides code examples for quick start, outlines service and mapper implementations, lists available mapper methods, and shows how to build query objects for flexible data access.
FastMyBatis is a MyBatis‑based development framework that aims for simplicity, speed, and efficiency, allowing developers to perform CRUD operations without writing XML files and supporting multiple databases such as MySQL, SQL Server, Oracle, PostgreSQL, and SQLite.
The framework integrates seamlessly with Spring Boot via the fastmybatis-spring-boot-starter dependency. Adding the following Maven snippet to pom.xml enables the starter:
<dependency>
<groupId>net.oschina.durcframework</groupId>
<artifactId>fastmybatis-spring-boot-starter</artifactId>
<version>latest version</version>
</dependency>After creating a Spring Boot project, you can define an entity class (e.g., TUser.java) and a corresponding mapper interface ( TUserMapper.java) that extends CrudMapper. The service layer implements IService<TUser, Integer> to inherit common data‑access methods.
A sample controller demonstrates typical CRUD endpoints using the service:
@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<?> update(TUser user) {
userService.updateIgnoreNull(user);
return Result.ok();
}
@GetMapping("/user/delete")
public Result<?> delete(Integer id) {
userService.deleteById(id);
return Result.ok();
}
}The generated mapper provides a rich set of methods, such as getById, list, page, saveIgnoreNull, updateByMap, and various batch operations. These methods cover basic queries, conditional updates, logical deletes, and forced deletes.
FastMyBatis also offers a Query builder for constructing flexible query conditions. Example usages include:
// Find a user named "张三" aged 22
Query query = new Query().eq("username", "张三").eq("age", 22);
List<User> users = mapper.list(query);
// Find users whose age is in [10, 20, 30]
Query query = new Query().in("age", Arrays.asList(10, 20, 30));
List<User> users = mapper.list(query);
// Paginated query for age 10, first page, 10 records per page
Query query = new Query().eq("age", 10).page(1, 10);
List<User> users = mapper.list(query);
long total = mapper.getCount(query);The project is open‑source (https://gitee.com/durcframework/fastmybatis) with documentation available at https://durcframework.gitee.io/fastmybatis/#/. The article concludes with a call to join the author’s community and share the content.
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.
