欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > 4. MyBatis如何与Spring集成?有哪些常见的配置方式?

4. MyBatis如何与Spring集成?有哪些常见的配置方式?

2024/10/24 16:31:48 来源:https://blog.csdn.net/zhzjn/article/details/141830193  浏览:    关键词:4. MyBatis如何与Spring集成?有哪些常见的配置方式?

MyBatis 可以通过多种方式与 Spring 集成,通常通过配置 Spring 来管理 MyBatis 的 SqlSessionFactoryMapper,并使用 Spring 的事务管理功能来管理数据库事务。以下是 MyBatis 与 Spring 集成的常见配置方式:

1. 基于 XML 配置的集成

这是传统的方式,通过 Spring 的 XML 配置文件来集成 MyBatis。

关键步骤:

  1. 配置数据源: 配置数据库连接池,并将其注入到 MyBatis 中。

    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"><property name="driverClassName" value="com.mysql.cj.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/mydb" /><property name="username" value="root" /><property name="password" value="password" />
    </bean>
  2. 配置 SqlSessionFactory: 通过 SqlSessionFactoryBean 来配置 MyBatis。

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="mapperLocations" value="classpath*:mapper/*.xml" /><property name="configLocation" value="classpath:mybatis-config.xml" />
    </bean>
  3. 配置事务管理器: 使用 Spring 的 DataSourceTransactionManager 来管理事务。

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" />
    </bean>
  4. 扫描 Mapper 接口: 通过 MapperScannerConfigurer 扫描 MyBatis 的 Mapper 接口。

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.example.mapper" /><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>

2. 基于 Java 配置(Java Config)的集成

使用 Java 配置的方式更符合 Spring Boot 的风格,也更现代化。

关键步骤:

  1. 配置数据源: 使用 Spring 的 DataSource 配置数据库连接池。

    @Configuration
    public class DataSourceConfig {
    ​@Beanpublic DataSource dataSource() {BasicDataSource dataSource = new BasicDataSource();dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");dataSource.setUrl("jdbc:mysql://localhost:3306/mydb");dataSource.setUsername("root");dataSource.setPassword("password");return dataSource;}
    }
  2. 配置 SqlSessionFactory: 使用 SqlSessionFactoryBean 来创建 SqlSessionFactory

    @Configuration
    public class MyBatisConfig {
    ​@Beanpublic SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();sessionFactory.setDataSource(dataSource);sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml"));return sessionFactory.getObject();}
    }
  3. 配置事务管理器: 使用 DataSourceTransactionManager 来管理事务。

    @Configuration
    public class TransactionConfig {
    ​@Beanpublic DataSourceTransactionManager transactionManager(DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}
    }
  4. 扫描 Mapper 接口: 使用 @MapperScan 注解来扫描 MyBatis 的 Mapper 接口。

    @Configuration
    @MapperScan("com.example.mapper")
    public class MyBatisMapperScannerConfig {
    }

3. Spring Boot 集成 MyBatis

Spring Boot 中 MyBatis 的集成更加简单,通常只需要通过添加依赖和少量配置即可完成。

关键步骤:

  1. 添加依赖: 在 pom.xml 中添加 MyBatis 和 Spring Boot Starter 的依赖。

    <dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.0</version>
    </dependency>
  2. 配置数据源: 在 application.propertiesapplication.yml 中配置数据源。

    spring.datasource.url=jdbc:mysql://localhost:3306/mydb
    spring.datasource.username=root
    spring.datasource.password=password
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

  3. 扫描 Mapper 接口: 使用 @MapperScan 注解来扫描 Mapper 接口。

    @SpringBootApplication
    @MapperScan("com.example.mapper")
    public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
    }

  4. 配置 Mapper 文件路径(可选): 如果使用 XML Mapper 文件,需要在 application.properties 中指定路径。

    mybatis.mapper-locations=classpath:mapper/*.xml

总结

MyBatis 与 Spring 的集成可以通过 XML 配置、Java Config 以及 Spring Boot 的方式实现。XML 配置适合于老项目,Java Config 和 Spring Boot 的配置更加现代化且易于维护。通过这些集成方式,MyBatis 可以充分利用 Spring 的 IOC、AOP 和事务管理等特性,实现更加简洁和高效的数据访问层开发。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com