mybatis-spring 的整合_配置application.xml

spring的配置文件文件

application.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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx
     	   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<!-- 自动扫描包 -->
	<context:component-scan base-package="cn.tiger.smartsign"
		use-default-filters="false">
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Service" />
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Repository" />
	</context:component-scan>
	
	<!-- 数据库配置文件位置 -->
	<context:property-placeholder location="classpath:jdbc.properties" />
	
	<!-- 配置dbcp数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<!-- 队列中的最小等待数 -->
		<property name="minIdle" value="${jdbc.minIdle}"></property>
		<!-- 队列中的最大等待数 -->
		<property name="maxIdle" value="${jdbc.maxIdle}"></property>
		<!-- 最长等待时间,单位毫秒 -->
		<property name="maxWait" value="${jdbc.maxWait}"></property>
		<!-- 最大活跃数 -->
		<property name="maxActive" value="${jdbc.maxActive}"></property>
		<property name="initialSize" value="${jdbc.initialSize}"></property>
	</bean>
	
	<!-- 全局事务配置 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	
	<!-- 使用annotation注解方式配置事务 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
	
	<!-- 配置mybitasSqlSessionFactoryBean -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="mapperLocations" value="classpath*:cn/tiger/smartsign/xml/*.xml" />
		<property name="typeAliasesPackage" value="cn.tiger.smartsign.entity" />
	</bean>
	
	<!-- 自动扫描Mapper,允许自动注入(根据类型匹配) -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="cn.tiger.smartsign.dao" />
	</bean>

</beans>


<!-- 配置mybitasSqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="mapperLocations" value="classpath*:cn/tiger/smartsign/xml/*.xml" />
		<property name="typeAliasesPackage" value="cn.tiger.smartsign.entity" />
</bean>

mapperLocations : 指定mybatis的xml映射器文件的位置 ,加载所在包下的所用mybatis映射器文件

typeAliasesPackage: 别名类型包-->扫描包中的所有类,并批量设置类型别名 例如 :

cn.tiger.smartsign.entity.XXX.java-->alias="XXX"

<!-- 自动扫描Mapper,允许自动注入(根据类型匹配) -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="cn.tiger.smartsign.dao" />
	</bean>

自动扫描映射所对应的接口包

MapperScnnerConfigurer:会将类路径下的映射器并自动将他们创建成MapperFactoybean,并自动装配

basePackage:属性为映射器接口文件设置基本的包路径,可以用分号或逗号作为分隔符设置多个路径






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值