Home  >  Article  >  Java  >  A practical guide to configuring MyBatis in Spring Boot

A practical guide to configuring MyBatis in Spring Boot

WBOY
WBOYOriginal
2024-02-25 16:03:06852browse

教你如何在Spring Boot中使用MyBatis进行配置

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.

  1. Add dependencies

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>
  1. Configuring the data source

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
  1. Create database mapping class

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方法
}
  1. Create Mapper interface

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();
}
  1. Create Mapper XML file

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>
  1. Configuring MyBatis

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);
    }
}
  1. Test database operation

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!

Statement:
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