spring与mybatis三种整合方法详细教程
时间:1970-01-01 | 作者:网络 | 阅读:1501、采用MapperScannerConfigurer,它将会查找类路径下的映射器并自动将它们创建成MapperFactoryBean。
spring-mybatis.xml:
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 自动扫描 --> <context:component-scan base-package="com.hua.saf" /> <!-- 引入配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties" /> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> <!-- 初始化连接大小 --> <property name="initialSize" value="${initialSize}" /> <!-- 连接池最大数量 --> <property name="maxActive" value="${maxActive}" /> <!-- 连接池最大空闲 --> <property name="maxIdle" value="${maxIdle}" /> <!-- 连接池最小空闲 --> <property name="minIdle" value="${minIdle}" /> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="${maxWait}" /> </bean> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自动扫描mapping.xml文件,**表示迭代查找 --> <property name="mapperLocations" value="classpath:com/hua/saf/**/*.xml" /> </bean> <!-- DAO接口所在包名,Spring会自动查找其下的类 ,包下的类需要使用@MapperScan注解,否则容器注入会失败 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.hua.saf.*" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
福利游戏
相关文章
更多大家都在玩
热门话题
大家都在看
更多-
- 使用教程:一文了解贝莱德(BlackRock)ETF已吸纳比特币(BTC)供应量的3.25%?
- 时间:2025-08-17
-
- 使用教程:如何铸造、转移、交易?什么是Tap Protocol?Tap协议TRAC在2025年是什么?
- 时间:2025-08-17
-
- 一次搞懂:从股价暴涨到腰斩,买SOL的上市公司真的赚钱了吗?
- 时间:2025-08-17
-
- 分析:Pump.fun代币PUMP跌破发行价,竞争对手崛起威胁市场地位?
- 时间:2025-08-17
-
- 攻略:一文带你了解一个私钥可以生成多少个地址和密码?
- 时间:2025-08-17
-
- 使用指南:InfoFi发展背景、项目玩法与未来发展趋势?InfoFi是什么意思?
- 时间:2025-08-17
-
- 解读:传统金融机构为何加速布局稳定币市场?
- 时间:2025-08-17
-
- 入门必备:这只是未来牛市浪潮的冰山一角?比特币刷新历史高点 $123,000,赵长鹏?
- 时间:2025-08-17