Master FastMybatis: Zero‑Config CRUD for Spring Boot
This article introduces FastMybatis, a lightweight Java MyBatis extension that enables zero‑configuration CRUD operations across multiple databases, shows how to quickly set up a Spring Boot project with the starter, provides full code examples for entities, mappers, services, a comprehensive mapper method list, and demonstrates Query object usage for flexible data retrieval.
Fast Mybatis is a MyBatis development framework that aims for simplicity, speed, and efficiency. It supports zero‑configuration CRUD without XML files and works with MySQL, SQL Server, Oracle, PostgreSQL, SQLite, and integrates with Spring Boot via a starter.
Quick Start (Spring Boot)
Create a Spring Boot project.
Add fastmybatis-spring-boot-starter dependency to pom.xml.
<dependency>
<groupId>net.oschina.durcframework</groupId>
<artifactId>fastmybatis-spring-boot-starter</artifactId>
<version>latest version</version>
</dependency>CRUD Example
Assume a t_user table and create TUser.java entity and TUserMapper.java mapper.
@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();
}
}Service implements the generic interface:
@Service
public class UserService implements IService<TUser, Integer> {
// implementation omitted
}Mapper extends the generic CRUD mapper:
public interface TUserMapper extends CrudMapper<TUser, Integer> {
// additional methods if needed
}Mapper Method List
E getByColumn(String column, Object value)– query a single record by a column. E getById(I id) – query by primary key. E getByQuery(Query query) – query a single record by conditions. List<E> list(Query query) – retrieve a result set. int save(E entity) – insert all fields. int saveIgnoreNull(E entity) – insert while ignoring null fields. int update(E entity) – update all fields. int updateIgnoreNull(E entity) – update while ignoring null fields. int deleteById(I id) – logical delete by primary key. int forceDeleteById(I id) – physical delete by primary key.
Query Object Usage
// Query users named "张三" aged 22
Query query = new Query().eq("username", "张三").eq("age", 22);
List<User> users = mapper.list(query);
// Query users with age in 10,20,30
query = new Query().in("age", Arrays.asList(10,20,30));
users = mapper.list(query);
// Query users registered after a specific date
Date regDate = ...;
query = new Query().gt("reg_date", regDate);
users = mapper.list(query);
// Paginated query: first page, 10 records per page
query = new Query().eq("age", 10).page(1, 10);
List<User> pageUsers = mapper.list(query);
// Get total record count for a condition
long total = mapper.getCount(query);Open‑source repository: https://gitee.com/durcframework/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 High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
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.
