手动整合MyBatis与Spring框架的实践方法
手动整合MyBatis与Spring框架需要进行多个步骤,包括配置依赖、配置数据源、配置MyBatis、以及整合Spring配置。以下是一个逐步指南:
1. 添加Maven依赖
首先,在你的pom.xml
中添加必要的依赖,包括Spring、MyBatis和MyBatis-Spring整合包:
<dependencies>
<!-- Spring Core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.10</version>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!-- MyBatis-Spring -->
<dependency>
<groupId>org.mybatis.spring</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<!-- Database Connection Pool (e.g., HikariCP) -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>4.0.3</version>
</dependency>
<!-- Your Database Driver (e.g., MySQL) -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
</dependencies>
2. 配置数据源
创建一个Spring配置文件,定义数据源Bean,例如使用HikariCP:
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class DataSourceConfig {
@Bean
public DataSource dataSource() {
HikariConfig config = new HikariConfig();
config.setDriverClassName("com.mysql.cj.jdbc.Driver");
config.setJdbcUrl("jdbc:mysql://localhost:3306/your_database");
config.setUsername("your_username");
config.setPassword("your_password");
return new HikariDataSource(config);
}
}
3. 配置MyBatis
创建一个MyBatis配置文件mybatis-config.xml
(可选,若有全局配置需求)并配置MyBatis的SQL Session Factory:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- MyBatis全局配置 -->
<settings>
<setting name="cacheEnabled" value="true"/>
</settings>
</configuration>
接着,创建一个Spring配置类,配置SQL Session Factory:
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
@Configuration
@MapperScan("com.example.mapper") // 替换为你的Mapper接口所在包
public class MyBatisConfig {
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath*:mapper/*.xml"));
return factoryBean.getObject();
}
}
4. 配置Spring
创建一个Java配置类,将数据源和MyBatis配置整合到Spring上下文中:
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@ComponentScan(basePackages = "com.example")
@Import({DataSourceConfig.class, MyBatisConfig.class})
public class AppConfig {
// 其他需要的Bean配置
}
5. 编写Mapper接口和XML
在src/main/resources/mapper/
目录中创建Mapper XML文件,并编写Mapper接口。
// SampleMapper.java
package com.example.mapper;
import com.example.model.SomeModel;
import org.apache.ibatis.annotations.Select;
public interface SampleMapper {
@Select("SELECT * FROM some_table WHERE id = #{id}")
SomeModel selectById(int id);
}
<!-- SampleMapper.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.SampleMapper">
<select id="selectById" parameterType="int" resultType="com.example.model.SomeModel">
SELECT * FROM some_table WHERE id = #{id}
</select>
</mapper>
6. 使用MyBatis与Spring
最后,在你的Spring应用中注入Mapper接口,并调用接口方法:
import com.example.mapper.SampleMapper;
import com.example.model.SomeModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class SampleService {
private final SampleMapper sampleMapper;
@Autowired
public SampleService(SampleMapper sampleMapper) {
this.sampleMapper = sampleMapper;
}
public SomeModel getById(int id) {
return sampleMapper.selectById(id);
}
}
通过这些步骤,你就可以手动整合MyBatis与Spring,并在你的项目中使用。为了更好的配置管理和资源加载,请确保路径和配置的正确性。