Home > Java > javaTutorial > body text

How to configure the MyBatis framework in Spring Boot

WBOY
Release: 2024-02-18 17:51:08
Original
801 people have browsed it

Spring Boot中如何配置MyBatis框架

How to configure the MyBatis framework in Spring Boot

Overview:
MyBatis is an open source Java persistence framework used to access databases in applications. It provides a simple and flexible way to map Java objects to database tables, as well as perform SQL queries and update operations. Spring Boot is a framework for creating standalone, Spring-based applications that simplifies the development process of MVC and other configurations. By combining the two, we can configure and use the MyBatis framework more conveniently.

The following are the specific steps to configure the MyBatis framework in Spring Boot:

Step 1: Add dependencies
To use the MyBatis framework in Spring Boot, you first need to add it to the project's pom. Add the corresponding dependencies to the xml file. Add the following content in the dependency management section:

<dependencies>
    <!-- Spring Boot依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- MyBatis依赖 -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.2.0</version>
    </dependency>

    <!-- 其他依赖省略 -->
</dependencies>
Copy after login

This adds the dependencies of Spring Boot and MyBatis to the project.

Step 2: Configure the data source
In Spring Boot, we can use application.properties or application.yml files to configure the data source. First, create an application.yml file in the src/main/resources directory and add the following content:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/testdb
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
Copy after login

Here, we configured a MySQL data source, using the local testdb database, username and The password is root. If you are using another database, you can modify the above configuration accordingly.

Step 3: Configure MyBatis
Create a Java class to configure MyBatis. Use the @MapperScan annotation on this class to specify the package where MyBatis' mapping interface is located. For example:

@Configuration
@MapperScan("com.example.demo.mapper")
public class MyBatisConfig {
}
Copy after login

Here, we set the package where MyBatis’ mapping interface is located to com.example.demo.mapper. You can modify this value according to the actual situation of the project.

Step 4: Create mapping files and mapping interfaces
After creating the above configuration class, you can start creating mapping files and mapping interfaces. Mapping files use XML format to define SQL statements and mapping rules. Create a mapper folder in the src/main/resources directory, and create a mybatis-mapper.xml file in the folder with the following content:

<?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">
    <select id="getUserById" resultType="com.example.demo.model.User">
        SELECT * FROM user WHERE id = #{id}
    </select>
</mapper>
Copy after login

Here, we define a file named getUserById Query statement, used to query user information based on id from the user table.

Next, create a mapping interface corresponding to the mapping file. Create a UserMapper interface in the com.example.demo.mapper package. The code is as follows:

public interface UserMapper {
    User getUserById(Long id);
}
Copy after login

Here, we define a getUserById method to call the getUserById query statement in the mapping file.

Step 5: Use MyBatis
After configuring MyBatis, we can use the MyBatis framework in the Service or Controller layer of Spring Boot to perform database operations. First, use the @Autowired annotation to introduce an instance of the UserMapper interface in the class that needs to use MyBatis.

@Autowired
private UserMapper userMapper;
Copy after login

Then you can operate the database by calling the methods defined in the UserMapper interface. For example, this can be used in the Controller layer:

@GetMapping("/user/{id}")
public User getUserById(@PathVariable("id") Long id) {
   return userMapper.getUserById(id);
}
Copy after login

In this way, the user information with id 1 can be obtained by accessing http://localhost:8080/user/1.

Summary:
Through the above steps, we successfully configured the MyBatis framework in Spring Boot and implemented basic database query operations. In actual projects, we can implement more complex data operations by extending this configuration.

I hope this article will help you understand how to configure the MyBatis framework for use in Spring Boot projects. Wish you happy using it!

The above is the detailed content of How to configure the MyBatis framework in Spring Boot. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template