配置mybatis数据库连接池,使用context:property-placeholder载不进属性

本文详细介绍了MyBatis配置文件中的两种不同配置方式:一种是使用属性名sqlSessionFactoryBeanName设置值为SqlSessionFactory;另一种是注释掉的使用ref引用的方式。通过对比这两种配置方法,帮助读者更好地理解MyBatis配置的细节。

mybatis配置中,使用:

<property name="sqlSessionFactoryBeanName" value="SqlSessionFactory" />

不使用:

<!-- <property name="sqlSessionFactory" ref="SqlSessionFactory"></property> -->
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--<context:component-scan base-package="com.tjise.bean"/>--> <!--数据库连接信息的属性文件 --> <context:property-placeholder location="classpath:jdbc-config.properties"/> <!-- 配置Druid数据源的Bean --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- 配置SessionFactory的Bean --> <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入数据源 --> <property name="dataSource" ref="dataSource"/> <!-- 指定MyBatis配置文件的位置 --> <property name="configLocation" value="classpath:mybatis.xml"/> <!-- 给实体类起别名 --> <property name="typeAliasesPackage" value="com.tjise.entity"/> </bean> <!-- 配置mapper接口的扫描器,将Mapper接口的实现类自动注入到IoC容器中 实现类Bean的名称默认为接口类名的首字母小写 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- basePackage属性指定自动扫描mapper接口所在的包 --> <property name="basePackage" value="com.tjise.mapper"/> </bean> </beans>
03-23
<?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 http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 1. 加数据库配置文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 2. 扫描 Service 层注解(com.example.service 包下) --> <context:component-scan base-package="com.example.service"/> <!-- 3. 配置 Druid 连接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="initialSize" value="${jdbc.initialSize}"/> <property name="maxActive" value="${jdbc.maxActive}"/> </bean> <!-- 4. 配置 SqlSessionFactory(MyBatis 核心) --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 扫描 MyBatis 全局配置文件 --> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/> <!-- 扫描 MyBatis 映射文件(com.example.mapper 包下的 XML 文件) --> <property name="mapperLocations" value="classpath:com/example/mapper/*.xml"/> <!-- 配置实体类别名扫描(com.example.pojo 包下) --> <property name="typeAliasesPackage" value="com.example.pojo"/> </bean> <!-- 5. 扫描 MyBatis Mapper 接口(com.example.mapper 包下) --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.example.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!-- 6. 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 7. 开启事务注解驱动 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
最新发布
11-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值