Home >Java >javaTutorial >A practical guide to configuring MyBatis in Spring Boot
Teach you how to use MyBatis for configuration in Spring Boot
Spring Boot is a very popular Java Web development framework today, and MyBatis is a simplified Java persistence layer development framework. Combining Spring Boot and MyBatis can greatly improve the efficiency and convenience of development. In this article, I will introduce in detail how to use MyBatis for configuration in Spring Boot and give specific code examples.
First, add the dependencies of MyBatis and MyBatis-Spring in the pom.xml file of the Spring Boot project. You can add it according to the following code:
<dependencies> <!-- Spring Boot 父依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> <scope>import</scope> <type>pom</type> </dependency> <!-- Spring Boot Web 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.0.0.RELEASE</version> </dependency> <!-- Mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> </dependencies>
To use MyBatis in Spring Boot, we first need to configure the data source. Add the database connection information in the application.properties or application.yml file, as shown below:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/db_example spring.datasource.username=root spring.datasource.password=admin
Next, we need to create a Database mapping class. In this class, we can use annotations to configure the mapping relationship between database tables and fields.
public class User { private Long id; private String name; private Integer age; // 省略getter和setter方法 }
To use MyBatis in Spring Boot, we need to create a Mapper interface to define methods for database operations.
public interface UserMapper { @Select("SELECT * FROM users") List<User> getAllUsers(); }
Next, we need to create a Mapper XML file to define specific SQL operations. Create a file named UserMapper.xml in the resources/mappers directory.
<?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.example.demo.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.example.demo.model.User"> <id column="id" property="id" /> <result column="name" property="name" /> <result column="age" property="age" /> </resultMap> <select id="getAllUsers" resultMap="BaseResultMap"> SELECT * FROM users </select> </mapper>
Configuring MyBatis in Spring Boot is very simple. You only need to add the @MapperScan annotation to the main configuration class and specify the package where the Mapper interface is located.
@SpringBootApplication @MapperScan("com.example.demo.mapper") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
Finally, inject UserMapper into our Controller class and call the corresponding method for data manipulation.
@RestController public class UserController { @Autowired private UserMapper userMapper; @GetMapping("/users") public List<User> getAllUsers() { return userMapper.getAllUsers(); } }
The above are the detailed steps for configuration using MyBatis in Spring Boot. In this way, we can easily use MyBatis for database operations in Spring Boot projects. Hope this article can be helpful to you!
The above is the detailed content of A practical guide to configuring MyBatis in Spring Boot. For more information, please follow other related articles on the PHP Chinese website!