SpringBoot里MyBatis - SQL执行过程

SpringBoot整合MyBatis的环境中,执行SQL的过程可以分为以下几个步骤:

配置数据源(DataSource):
SpringBoot通过application.propertiesapplication.yml配置数据库连接信息,创建并配置DataSource

配置SqlSessionFactory:
使用org.mybatis.spring.SqlSessionFactoryBean来构建SqlSessionFactory

java 复制代码
   import org.apache.ibatis.session.SqlSessionFactory;
   import org.mybatis.spring.SqlSessionFactoryBean;
   import org.mybatis.spring.annotation.MapperScan;
   import org.springframework.beans.factory.annotation.Autowired;
   import org.springframework.context.annotation.Bean;
   import org.springframework.context.annotation.Configuration;
   import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
   import org.springframework.jdbc.datasource.DataSourceTransactionManager;

   @Configuration
   @MapperScan(basePackages = "com.example.myapp.mapper") // 替换为你的Mapper接口所在的包名
   public class MybatisConfig {

       @Autowired
       private DataSource dataSource;

       @Bean
       public SqlSessionFactory sqlSessionFactory() throws Exception {
           SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
           factoryBean.setDataSource(dataSource);
   
           // 设置MyBatis全局配置文件的位置(如果有的话)
           factoryBean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));

           // 设置Mapper XML文件的位置
           factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));

           return factoryBean.getObject();
       }

       @Bean
       public DataSourceTransactionManager transactionManager() {
           return new DataSourceTransactionManager(dataSource);
       }
   }

注入Mapper接口:
Mapper接口@Component或者@Repository注解的方式声明为一个Spring Bean
或者在配置类中使用@MapperScan注解指定扫描包含Mapper接口的包路径,这样Spring容器就能自动发现并创建Mapper接口的代理对象。。

获取Mapper实例并执行方法:
在业务层Service中,通过@Autowired注解注入Mapper接口。
当调用Mapper接口的方法时,由于Spring已经为其生成了动态代理对象,所以实际上是在执行代理对象的方法。
代理方法内部会根据Mapper接口的方法签名和对应的XML映射文件或注解找到SQL语句。

执行SQL:
MyBatis会通过SqlSession执行SQL语句,它是由SqlSessionFactory创建的。
SQL执行过程中,MyBatis会利用StatementHandler对SQL进行预编译处理、通过ParameterHandler设置参数。Executor(执行器)负责执行具体的SQL操作,包括查询、插入、更新和删除等,并可能涉及缓存策略的使用。

结果映射与返回:
执行完SQL后,MyBatis会通过ResultHandlerResultMap将结果集转换成Java对象,并返回给调用者

事务管理:
在Spring Boot中,可以通过@Transactional注解实现声明式事务管理,Spring会自动处理SQL执行过程中的事务边界,确保数据的一致性。

相关推荐
计算机毕业设计小帅12 小时前
【2026计算机毕业设计】基于Springboot的校园电动车短租平台
spring boot·后端·课程设计
superlls13 小时前
(Spring)Spring Boot 中 @Valid 与全局异常处理器的联系详解
java·spring boot·后端
摇滚侠14 小时前
Spring Boot 3零基础教程,WEB 开发 整合 Thymeleaf 笔记36
java·spring boot·笔记
瑞士卷@14 小时前
MyBatis入门到精通(Mybatis学习笔记)
java·数据库·后端·mybatis
optimistic_chen14 小时前
【Java EE进阶 --- SpringBoot】Mybatis - plus 操作数据库
数据库·spring boot·笔记·java-ee·mybatis·mybatis-plus
来旺15 小时前
互联网大厂Java面试全解析及三轮问答专项
java·数据库·spring boot·安全·缓存·微服务·面试
詩句☾⋆᭄南笙15 小时前
Mybatis一对一、一对多
java·mybatis·resulttype·resultmap·一对多·一对一
摇滚侠15 小时前
Spring Boot 3零基础教程,yml文件中配置和类的属性绑定,笔记15
spring boot·redis·笔记
thginWalker15 小时前
使用Spring Boot构建消息通信层
spring boot
lang2015092815 小时前
Spring Boot 外部化配置最佳实践指南
java·spring boot