MapperScannerConfigurer中sqlSessionFactoryBeanName用value而不用ref的原因

在mybatis-spring早期版本中,SqlSessionFactory初始化可能因jdbc.properties未加载导致属性设置失败。从1.1.0开始,MapperScannerConfigurer引入sqlSessionFactoryBeanName,允许在Spring初始化完成后构建SqlSessionFactory,解决了这个问题。此改变确保了数据源配置的正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在mybatis-spring1.1.0以前,是通过将SqlSessionFactory对象注入到sqlSessionFactory,这样做可能会有一个问题,就是在初始化MyBatis时,jdbc.properties文件还没被加载进来,dataSource的属性值没有被替换,就开始构造sqlSessionFactory类,属性值就会加载失败。

在1.1.0以后,MapperScannerConfigure提供了String类型的sqlSessionFactoryBeanName,通过,这样先将bean name注入到sqlSessionFactoryBeanName,等到spring初始化完成后,再通过bean name构建sqlSessionFactory。

注:单数据源时可以不用sqlSessionFactoryBeanName,猜想spring会自己到容器里寻找,之后会看源码验证

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 缩小扫描范围到 com.itheima.SpringMVC --> <context:component-scan base-package="com.itheima.SpringMVC"/> <bean id="User" class="com.itheima.SpringMVC.pojo.User"> <property name="username" value="涂大钧"></property> <property name="password" value="123"></property> </bean> <!--数据源--> <context:property-placeholder location="classpath*:jdbc.properties"/> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--配置sql--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!--别名扫包--> <property name="typeAliasesPackage" value="com.itheima.SpringMVC.pojo"></property> <!--指定XML文件位置--> <!--mybatis-config.xml--> <property name="configLocation" value="classpath:/mybatis-mvc-config.xml" /> </bean> <!--指定Mapper XML文件位置--> <!--扫描Mapper--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> <property name="basePackage" value="com.itheima.SpringMVC.Mapper"></property> </bean> <!-- 事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--开启注解驱动的事务管理--> <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven> </beans><?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> <!-- 引入外部配置文件 --> <properties resource="jdbc.properties"> <!-- 可以在这里添加默认属性值 --> </properties> <!-- 设置 MyBatis 核心配置 --> <settings> <!-- 开启驼峰命名自动映射,如将数据库字段 user_name 映射到 Java 属性 userName --> <setting name="mapUnderscoreToCamelCase" value="true"/> <!-- 开启二级缓存 --> <setting name="cacheEnabled" value="true"/> <!-- 开启延迟加载 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 开启 aggressiveLazyLoading 会导致所有关联对象在被访问时立即加载 --> <setting name="aggressiveLazyLoading" value="true"/> <!-- 配置日志实现 --> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> <!-- 配置类型别名,简化映射 --> <typeAliases> <package name="com.itheima.SpringMVC.pojo"/> </typeAliases> <!-- 配置插件 --> <plugins> <!-- PageHelper 分页插件 --> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <!-- 可选配置 --> <property name="reasonable" value="true"/> <property name="supportMethodsArguments" value="true"/> <property name="params" value="count=countSql"/> </plugin> </plugins> <!-- 配置数据库环境 --> <environments default="development"> <environment id="development"> <!-- 使用 JDBC 事务管理器 --> <transactionManager type="JDBC"/> <!-- 配置数据源,使用 POOLED 连接池 --> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <!-- 可选配置:连接池参数 --> <property name="poolMaximumActiveConnections" value="10"/> <property name="poolMaximumIdleConnections" value="5"/> </dataSource> </environment> </environments> <!-- 注册映射器 --> <mappers> <package name="com.itheima.SpringMVC.Mapper" /> <!-- <mapper resource="SpringMVC/Mapper/UsersMapper.xml"/>--> <!-- <mapper resource="SpringMVC/Mapper/UserMapper.xml"/>--> <!-- <mapper resource="SpringMVC/Mapper/IdCardMapper.xml"/>--> <!-- <mapper resource="SpringMVC/Mapper/GoodsMapper.xml"/>--> <!-- <mapper resource="SpringMVC/Mapper/GoodsTypeMapper.xml"/>--> <!-- <mapper resource="SpringMVC/Mapper/OrdersMapper.xml"/>--> <!-- <mapper resource="SpringMVC/Mapper/OrderItemsMapper.xml"/>--> </mappers> </configuration>有问题吗
最新发布
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值