Integrate mybatis configuration process in springboot!

The configuration process of integrating MyBatis in Spring Boot is divided into the following steps:

1. Add dependencies:

First, you need to add MyBatis and database driver dependencies to the project's `pom.xml` file. Typically, you would use MyBatis' Spring Boot Starter dependency to simplify configuration. For example, if you use a MySQL database, you can add the following dependency:

```xml

<dependencies>
    <!-- MyBatis Spring Boot Starter -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.2.0</version> <!-- 根据您的需求选择合适的版本 -->
    </dependency>
    <!-- MySQL数据库驱动 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.27</version> <!-- 根据您的MySQL版本选择合适的驱动版本 -->
    </dependency>
</dependencies>


```

2. Configure the data source:

Configure database connection information in the `application.properties` or `application.yml` file, including URL, username and password. For example:

Configure using `application.properties`:

```properties
spring.datasource.url=jdbc:mysql://localhost:3306/your_database
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
```

Or use `application.yml` configuration:

```yaml

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://localhost:3306/db_hospital?useUnicode=true&userSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true&autoReconnect=true
    username: root
    password: 666666
    driver-class-name: com.mysql.cj.jdbc.Driver
    initial-size: 5 #Initial connection pool size
    min-idle: 5 # Minimum number of idle connections
    max-active: 20 # Maximum number of active connections
    max-wait: 60000 # Get the maximum waiting time for the connection (milliseconds)

my shoe:
  mapper-locations: classpath:com/ekgc/*/dao/*.xml
  configuration:
    autoMappingBehavior: PARTIAL
    mapUnderscoreToCamelCase: true
    logImpl: org.apache.ibatis.logging.log4j.Log4jImpl
  type-aliases-package: com.ekgc.qy.pojo

server:
  port: 8090
  servlet:
    context-path: /qy
```

3. Create Mapper interface:

Create DAO (Data Access Object) interfaces, which are used to define database operation methods. Typically, you would use the `@Mapper` annotation on an interface to tell MyBatis to implement it as a Mapper interface. For example:

```java

import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper {
    User getUserById(Long id);
    List<User> getAllUsers();
    void insertUser(User user);
    void updateUser(User user);
    void deleteUser(Long id);
}


```

4. Create Mapper XML file:

Write corresponding Mapper XML files for each Mapper interface. These files contain SQL statements and mapping rules. Mapper XML files are usually stored in the `mapper` subdirectory of the `resources` directory. For example, the `UserMapper.xml` file:

```xml

<?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.dao.UserMapper">
    <resultMap id="BaseResultMap" type="com.example.demo.entity.User">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="username" property="username" jdbcType="VARCHAR"/>
        <!-- 其他字段映射 -->
    </resultMap>

    <select id="getUserById" resultMap="BaseResultMap">
        SELECT * FROM user WHERE id = #{id}
    </select>

    <!-- 其他SQL语句 -->
</mapper>


```

5. Configure MyBatis scanning:

Add the `@MapperScan` annotation on Spring Boot's main application class to tell Spring Boot to scan the package path of the Mapper interface. For example:

```java

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.example.demo.dao") // 指定Mapper接口所在的包路径
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}


```

6. Use Mapper:

Inject Mapper interfaces in your service or controller and use them to access the database. For example:

```java

@Service
public class UserService {
    private final UserMapper userMapper;

    @Autowired
    public UserService(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    public User getUserById(Long id) {
        return userMapper.getUserById(id);
    }

    // 其他数据库操作方法
}

The rest can be adjusted and expanded accordingly based on your project needs and database configuration.
```

Guess you like

Origin blog.csdn.net/qq_58647634/article/details/133082845
Recommended