Last one We introduced how in Spring Boot The most commonly used MyBatis To access the relational database . But in the previous article, we used annotation to implement , And for a lot of MyBatis Old users are still used to XML Development mode , So this one , Let’s see how to use XML The way to develop .
Try it
This article will not specifically introduce Integration MyBatis Basic content of , Readers can read Last one :Spring Boot 2.x Basic course : Use MyBatis visit MySQL To understand this part .
The following part of the implementation will be based on the example in the previous article , Basic engineering can be done through chapter3-5
Directory get .
First step : Add… To the application main class mapper Configuration of the scanning package :
@MapperScan("com.didispace.chapter36.mapper")
@SpringBootApplication
public class Chapter36Application {
public static void main(String[] args) {
SpringApplication.run(Chapter36Application.class, args);
}
}
The second step : Specified in the first step Mapper Package created under User Tabular Mapper Definition :
public interface UserMapper {
User findByName(@Param("name") String name);
int insert(@Param("name") String name, @Param("age") Integer age);
}
The third step : Pass… In the configuration file mybatis.mapper-locations
Parameter assignment xml Location of configuration :
mybatis.mapper-locations=classpath:mapper/*.xml
Step four : Specified in step 3 xml Create… In configuration directory User Tabular mapper To configure :
<?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>
Come here from the way of annotation MyBatis The way of use is changed to XML The configuration of , In order to verify whether it works properly , You can try to write and read the database through the following unit tests :
@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 you are not successful in trying , It is recommended to check the completion code through the warehouse at the end of the article , Compare whether there are omissions and omissions .
More free tutorials in this series 「 Click to enter the summary Directory 」
Code example
For an example of this article, see the following in the warehouse chapter3-6
Catalog :
- Github:https://github.com/dyc87112/SpringBoot-Learning/
- Gitee:https://gitee.com/didispace/SpringBoot-Learning/
If you think this article is good , welcome Star
Support , Your concern is the driving force of my persistence !
Welcome to my official account. : Program the ape DD, Get exclusive learning resources and daily dry goods push . If you are interested in my topic content , You can also follow my blog :didispace.com