在Spring Boot中整合MyBatis的配置流程分为以下几个步骤:
首先,您需要在项目的`pom.xml`文件中添加MyBatis和数据库驱动的依赖。通常,您会使用MyBatis的Spring Boot Starter依赖来简化配置。例如,如果您使用MySQL数据库,可以添加以下依赖:
```xml
<dependencies> <dependency> <groupId>org.mybatis.spring.bootgroupId> <artifactId>mybatis-spring-boot-starterartifactId> <version>2.2.0version> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>8.0.27version> dependency> dependencies>
```
在`application.properties`或`application.yml`文件中配置数据库连接信息,包括URL、用户名和密码。例如:
```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
```
```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 # 初始连接池大小 min-idle: 5 # 最小空闲连接数 max-active: 20 # 最大活跃连接数 max-wait: 60000 # 获取连接的最大等待时间(毫秒) mybatis: 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 ```
创建DAO(数据访问对象)接口,这些接口用于定义数据库操作方法。通常,您会在接口上使用`@Mapper`注解来告诉MyBatis将其实现为Mapper接口。例如:
```java
import org.apache.ibatis.annotations.Mapper; @Mapper public interface UserMapper { User getUserById(Long id); ListgetAllUsers(); void insertUser(User user); void updateUser(User user); void deleteUser(Long id); }
```
为每个Mapper接口编写对应的Mapper XML文件,这些文件包含SQL语句和映射规则。Mapper XML文件通常存放在`resources`目录下的`mapper`子目录中。例如,`UserMapper.xml`文件:
```xml
"1.0" encoding="UTF-8"?> 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> mapper>
```
在Spring Boot的主应用程序类上添加`@MapperScan`注解,以告诉Spring Boot扫描Mapper接口的包路径。例如:
```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); } }
```
在您的服务或控制器中注入Mapper接口,然后使用它们来访问数据库。例如:
```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); } // 其他数据库操作方法 }其余根据你的项目需求和数据库配置进行相应的调整和扩展。
```