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执行过程中的事务边界,确保数据的一致性。

相关推荐
侠客行031717 小时前
Mybatis连接池实现及池化模式
java·mybatis·源码阅读
山峰哥19 小时前
数据库工程与SQL调优——从索引策略到查询优化的深度实践
数据库·sql·性能优化·编辑器
老毛肚19 小时前
MyBatis体系结构与工作原理 上篇
java·mybatis
风流倜傥唐伯虎19 小时前
Spring Boot Jar包生产级启停脚本
java·运维·spring boot
fuquxiaoguang20 小时前
深入浅出:使用MDC构建SpringBoot全链路请求追踪系统
java·spring boot·后端·调用链分析
毕设源码_廖学姐21 小时前
计算机毕业设计springboot招聘系统网站 基于SpringBoot的在线人才对接平台 SpringBoot驱动的智能求职与招聘服务网
spring boot·后端·课程设计
顾北1221 小时前
MCP服务端开发:图片搜索助力旅游计划
java·spring boot·dubbo
山岚的运维笔记21 小时前
SQL Server笔记 -- 第18章:Views
数据库·笔记·sql·microsoft·sqlserver
昀贝21 小时前
IDEA启动SpringBoot项目时报错:命令行过长
java·spring boot·intellij-idea
indexsunny1 天前
互联网大厂Java面试实战:Spring Boot微服务在电商场景中的应用与挑战
java·spring boot·redis·微服务·kafka·spring security·电商