提示信息

手动整合MyBatis与Spring框架的实践方法

时光找茬 已关注

手动整合MyBatis与Spring框架需要进行多个步骤,包括配置依赖、配置数据源、配置MyBatis、以及整合Spring配置。以下是一个逐步指南:

1. 添加Maven依赖

首先,在你的pom.xml中添加必要的依赖,包括Spring、MyBatis和MyBatis-Spring整合包:

  1. <dependencies>
  2. <!-- Spring Core -->
  3. <dependency>
  4. <groupId>org.springframework</groupId>
  5. <artifactId>spring-context</artifactId>
  6. <version>5.3.10</version>
  7. </dependency>
  8. <!-- MyBatis -->
  9. <dependency>
  10. <groupId>org.mybatis</groupId>
  11. <artifactId>mybatis</artifactId>
  12. <version>3.5.7</version>
  13. </dependency>
  14. <!-- MyBatis-Spring -->
  15. <dependency>
  16. <groupId>org.mybatis.spring</groupId>
  17. <artifactId>mybatis-spring</artifactId>
  18. <version>2.0.6</version>
  19. </dependency>
  20. <!-- Database Connection Pool (e.g., HikariCP) -->
  21. <dependency>
  22. <groupId>com.zaxxer</groupId>
  23. <artifactId>HikariCP</artifactId>
  24. <version>4.0.3</version>
  25. </dependency>
  26. <!-- Your Database Driver (e.g., MySQL) -->
  27. <dependency>
  28. <groupId>mysql</groupId>
  29. <artifactId>mysql-connector-java</artifactId>
  30. <version>8.0.26</version>
  31. </dependency>
  32. </dependencies>

2. 配置数据源

创建一个Spring配置文件,定义数据源Bean,例如使用HikariCP:

  1. import com.zaxxer.hikari.HikariConfig;
  2. import com.zaxxer.hikari.HikariDataSource;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import javax.sql.DataSource;
  6. @Configuration
  7. public class DataSourceConfig {
  8. @Bean
  9. public DataSource dataSource() {
  10. HikariConfig config = new HikariConfig();
  11. config.setDriverClassName("com.mysql.cj.jdbc.Driver");
  12. config.setJdbcUrl("jdbc:mysql://localhost:3306/your_database");
  13. config.setUsername("your_username");
  14. config.setPassword("your_password");
  15. return new HikariDataSource(config);
  16. }
  17. }

3. 配置MyBatis

创建一个MyBatis配置文件mybatis-config.xml(可选,若有全局配置需求)并配置MyBatis的SQL Session Factory:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <!-- MyBatis全局配置 -->
  7. <settings>
  8. <setting name="cacheEnabled" value="true"/>
  9. </settings>
  10. </configuration>

接着,创建一个Spring配置类,配置SQL Session Factory:

  1. import org.apache.ibatis.session.SqlSessionFactory;
  2. import org.mybatis.spring.SqlSessionFactoryBean;
  3. import org.mybatis.spring.annotation.MapperScan;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  7. import javax.sql.DataSource;
  8. @Configuration
  9. @MapperScan("com.example.mapper") // 替换为你的Mapper接口所在包
  10. public class MyBatisConfig {
  11. @Bean
  12. public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
  13. SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
  14. factoryBean.setDataSource(dataSource);
  15. factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
  16. .getResources("classpath*:mapper/*.xml"));
  17. return factoryBean.getObject();
  18. }
  19. }

4. 配置Spring

创建一个Java配置类,将数据源和MyBatis配置整合到Spring上下文中:

  1. import org.springframework.context.annotation.ComponentScan;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.context.annotation.Import;
  4. @Configuration
  5. @ComponentScan(basePackages = "com.example")
  6. @Import({DataSourceConfig.class, MyBatisConfig.class})
  7. public class AppConfig {
  8. // 其他需要的Bean配置
  9. }

5. 编写Mapper接口和XML

src/main/resources/mapper/目录中创建Mapper XML文件,并编写Mapper接口。

  1. // SampleMapper.java
  2. package com.example.mapper;
  3. import com.example.model.SomeModel;
  4. import org.apache.ibatis.annotations.Select;
  5. public interface SampleMapper {
  6. @Select("SELECT * FROM some_table WHERE id = #{id}")
  7. SomeModel selectById(int id);
  8. }
  1. <!-- SampleMapper.xml -->
  2. <?xml version="1.0" encoding="UTF-8" ?>
  3. <!DOCTYPE mapper
  4. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  5. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  6. <mapper namespace="com.example.mapper.SampleMapper">
  7. <select id="selectById" parameterType="int" resultType="com.example.model.SomeModel">
  8. SELECT * FROM some_table WHERE id = #{id}
  9. </select>
  10. </mapper>

6. 使用MyBatis与Spring

最后,在你的Spring应用中注入Mapper接口,并调用接口方法:

  1. import com.example.mapper.SampleMapper;
  2. import com.example.model.SomeModel;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Service;
  5. @Service
  6. public class SampleService {
  7. private final SampleMapper sampleMapper;
  8. @Autowired
  9. public SampleService(SampleMapper sampleMapper) {
  10. this.sampleMapper = sampleMapper;
  11. }
  12. public SomeModel getById(int id) {
  13. return sampleMapper.selectById(id);
  14. }
  15. }

通过这些步骤,你就可以手动整合MyBatis与Spring,并在你的项目中使用。为了更好的配置管理和资源加载,请确保路径和配置的正确性。

时光找茬 关注 已关注

最近一次登录:2024-11-21 09:48:10   

暂时还没有签名,请关注我或评论我的文章
×
分享到朋友圈