How to configure MyBatis in Spring Boot?
To configure MyBatis in Spring Boot, you need to complete the following steps:
1. Add MyBatis and necessary dependencies: Start by adding the required dependencies in your `pom.xml` file. Normally, this includes `mybatis-spring-boot-starter` and the appropriate database driver dependencies.
Example snippet of `pom.xml`:
<dependencies><!-- MyBatis -->
<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.23</version>
</dependency>
</dependencies>
2. Set up database connection information: Provide the relevant connection information for your database in the `application.properties` file, such as the URL, username, and password.
Example of the `application.properties` file:
# 数据库连接信息spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase
spring.datasource.username=root
spring.datasource.password=your_password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# MyBatis配置
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.model
Please make sure to replace `mydatabase` in the above example with the actual name of your database, and use the correct database connection URL, username, and password.
3. Create a data source and SqlSessionFactory Bean: In the configuration class of your Spring Boot application, set up a data source and SqlSessionFactory Bean.
Sample configuration class:
@Configuration@MapperScan("com.example.mapper") // 指定MyBatis Mapper接口的扫描路径
public class MyBatisConfig {
@Autowired
private DataSource dataSource;
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
return sessionFactory.getObject();
}
}
Please make sure to replace `com.example.mapper` in the above example with the actual package name where your Mapper interface is located.
4. Create a Mapper interface and its corresponding XML mapping file, where you define the SQL query statements and result mapping rules.
Example of Mapper interface:
@Mapperpublic interface UserMapper {
List<User> getAllUsers();
User getUserById(Long id);
void insertUser(User user);
void updateUser(User user);
void deleteUser(Long id);
}
Example XML mapping file (UserMapper.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.mapper.UserMapper">
<select id="getAllUsers" resultType="com.example.model.User">
SELECT * FROM users
</select>
<!-- 其他SQL语句... -->
</mapper>
Please make sure to replace `com.example.mapper.UserMapper` and `com.example.model.User` in the above example with your actual Mapper interface and model class.
5. Inject and use Mapper where MyBatis is needed: In your service class or elsewhere, inject the Mapper interface using the @Autowired annotation and use the methods defined within it to perform database operations.
Example of Service class:
@Servicepublic class UserService {
@Autowired
private UserMapper userMapper;
public List<User> getAllUsers() {
return userMapper.getAllUsers();
}
// 其他方法...
}
By doing this, you have completed the process of configuring MyBatis in Spring Boot. You can expand and adjust the configuration as needed to meet your specific requirements.