Practical Guide to Using MyBatis with Spring Boot: Configuration, CRUD Operations, Dynamic SQL, and Advanced Mapping
This tutorial explains how to integrate MyBatis into a Spring Boot project, covering setup, basic CRUD examples, dynamic SQL techniques, one‑to‑one and one‑to‑many mappings, pagination support, and provides complete code snippets for each step.
MyBatis is a popular open‑source ORM framework that allows developers to write SQL in XML and map it to Java methods, simplifying JDBC operations.
Integration with Spring Boot is achieved by adding the mybatis-spring-boot-starter dependency to pom.xml, configuring mapper locations in application.yml, and enabling DAO scanning with @MapperScan.
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>Basic CRUD examples show how to define entity classes, DAO interfaces, and corresponding XML statements for select, insert (including selectKey for generated keys), update (using set for selective updates), and delete. The guide also explains handling underscore‑to‑camel‑case mapping.
public class UmsAdmin implements Serializable {
private Long id;
private String username;
private String password;
// other fields with @ApiModelProperty annotations
} @Repository
public interface UmsAdminDao {
UmsAdmin selectByIdSimple(Long id);
int insert(UmsAdmin entity);
int updateById(UmsAdmin entity);
int deleteById(Long id);
} <select id="selectByIdSimple" resultType="com.macro.mall.tiny.model.UmsAdmin">
select * from ums_admin where id = #{id}
</select> <insert id="insert">
insert into ums_admin(username, password, ...) values (#{username}, #{password}, ...)
<selectKey keyProperty="id" resultType="long" order="AFTER">
SELECT LAST_INSERT_ID()
</selectKey>
</insert> <update id="updateById">
update ums_admin set username = #{username}, password = #{password}, ... where id = #{id}
</update>Dynamic SQL features such as if, choose, where, set, and foreach are demonstrated to build flexible queries, batch inserts, and conditional clauses without manual string concatenation.
<select id="selectByUsernameAndEmailLike" resultType="com.macro.mall.tiny.model.UmsAdmin">
select username, password, ... from ums_admin
where 1=1
<if test="username!=null and username!=''">
and username like concat('%', #{username}, '%')
</if>
<if test="email!=null and email!=''">
and email like concat('%', #{email}, '%')
</if>
</select> <insert id="insertBatch">
insert into ums_admin(username, password, ...) values
<foreach collection="entityList" item="item" separator=",">
(#{item.username}, #{item.password}, ...)
</foreach>
</insert>Advanced mapping techniques cover one‑to‑one and one‑to‑many relationships using ResultMap, association, and collection tags, as well as pagination with the PageHelper plugin.
<resultMap id="ResourceWithCategoryMap" type="com.macro.mall.tiny.domain.UmsResourceExt" extends="BaseResultMap">
<association property="category" resultMap="com.macro.mall.tiny.dao.UmsResourceCategoryDao.BaseResultMap" columnPrefix="category_"/>
</resultMap> @Service
public class UmsResourceServiceImpl implements UmsResourceService {
@Autowired
private UmsResourceDao umsResourceDao;
public PageInfo<UmsResource> page(Integer pageNum, Integer pageSize, Long categoryId) {
PageHelper.startPage(pageNum, pageSize);
List<UmsResource> list = umsResourceDao.selectListByCategoryId(categoryId);
return new PageInfo<>(list);
}
}The article concludes with a summary of the covered topics and provides a link to the full source code repository for further reference.
https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-mybatisSigned-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.
IT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
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.
