Switch from MyBatis Annotations to XML in Spring Boot: Step‑by‑Step Guide
This tutorial walks you through converting a Spring Boot MyBatis integration from annotation‑based configuration to XML‑based mapper files, covering scanner setup, interface definition, XML mapper creation, property configuration, and a unit test to verify database read/write operations.
In the previous article we introduced integrating MyBatis with Spring Boot using annotations. This tutorial shows how to switch to the XML configuration style.
Hands‑on
The article assumes you already know the basics of MyBatis integration; you can refer to the previous tutorial for that.
The sample project can be obtained from the chapter3-5 directory in the repository linked at the end.
Step 1 : Add a mapper‑scan annotation to the main application class.
@MapperScan("com.didispace.chapter36.mapper")
@SpringBootApplication
public class Chapter36Application {
public static void main(String[] args) {
SpringApplication.run(Chapter36Application.class, args);
}
}Step 2 : Create a UserMapper interface in the scanned package.
public interface UserMapper {
User findByName(@Param("name") String name);
int insert(@Param("name") String name, @Param("age") Integer age);
}Step 3 : Specify the location of XML mapper files in application.properties (or yaml) using the mybatis.mapper-locations property.
mybatis.mapper-locations=classpath:mapper/*.xmlStep 4 : Create the XML mapper file UserMapper.xml under src/main/resources/mapper:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.didispace.chapter36.mapper.UserMapper">
<select id="findByName" resultType="com.didispace.chapter36.entity.User">
SELECT * FROM USER WHERE NAME = #{name}
</select>
<insert id="insert">
INSERT INTO USER(NAME, AGE) VALUES(#{name}, #{age})
</insert>
</mapper>To verify the configuration, run the following unit test which inserts a record and queries it back.
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class Chapter36ApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
@Rollback
public void test() throws Exception {
userMapper.insert("AAA", 20);
User u = userMapper.findByName("AAA");
Assert.assertEquals(20, u.getAge().intValue());
}
}If the test fails, compare your code with the complete example in the repository.
Resources:
GitHub: https://github.com/dyc87112/SpringBoot-Learning/
Gitee: https://gitee.com/didispace/SpringBoot-Learning/
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.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
