applicationContext.xml

本文详细介绍了如何在Spring框架中整合MyBatis,包括配置数据源、事务管理及Mapper接口扫描等内容,并展示了多种组件扫描的方法。

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

这是applicationContext.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.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/context 
         http://www.springframework.org/schema/context/spring-context-4.0.xsd
         http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "
         >

<!-- ............................................华丽的分割线................................................ -->
	<!-- Spring希望管理所有的业务逻辑组件,等等.....
		另外,如果有下面这条配置用来扫描包路径选项,则不再需要撰写这条<context:annotation-config />来提供注解支持了
	 	另外另外,请注意!!!尽量不要用方法3,此为错误方法,会出现各种让你欲哭无泪的404、500等错误提示
	 -->
	 
<!-- 	 方法1(笨方法):
	<context:component-scan base-package="com.sundi.hrm.dao" />
	<context:component-scan base-package="com.sundi.hrm.pojo" />
	<context:component-scan base-package="com.sundi.hrm.service" />
 -->

<!-- 	方法2(简便方法): -->
	<context:component-scan base-package="com.sundi.hrm">
		<!-- 不希望用spring管理springMVC的内容,把Controller剔除掉 -->
		<context:exclude-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
	</context:component-scan>
	
	
<!-- 	方法3:(错误方法) 搞成下面这种会出现500错误 ,不知道为何!
		<context:component-scan base-package="dao,pojo,service" /> 
-->
 <!-- ............................................华丽的分割线............................................... -->        
         

         
         
<!-- ............................................华丽的分割线............................................... -->       
	<!-- 引入数据库的配置文件 -->
	<context:property-placeholder location="classpath:db.properties" />            
         
    <!-- Spring用来控制业务逻辑。数据源、事务控制、aop -->         
     <!-- 使用数据源c3p0 类型   -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">    
        <property name="driverClass" value="${jdbc.driver}" />    
        <property name="jdbcUrl" value="${jdbc.url}" />    
        <property name="user" value="${jdbc.username}" />    
        <property name="password" value="${jdbc.password}" />  
    </bean>  
			       
     <!-- spring事务管理 -->
	<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- 开启基于注解的事务 -->
	<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
<!-- ............................................华丽的分割线............................................... -->       
         
         
         
         
         
        
 <!-- ............................................华丽的分割线............................................... -->     
        
 	<!-- 整合mybatis !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
			目的:1、spring管理所有组件 mapper的实现类。
					 service==>Dao   @Autowired:自动注入mapper;
				  2、spring用来管理事务,spring声明式事务
	-->       
	
	<!--创建出SqlSessionFactory对象  -->
	<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		
		<!-- configLocation指定全局配置文件的位置 -->
		<property name="configLocation" value="classpath:mybatis-config.xml" />
		
		<!--mapperLocations: 指定mapper文件的位置-->
		<property name="mapperLocations" value="classpath:mybatis/mapper/*.xml" />
		
		<!-- 扫描POJO包 自动提供别名,以后POJO里面的类就不用写全类名了 -->
        <property name="typeAliasesPackage" value="com.sundi.hrm.pojo" />
	</bean>
	
        
     <!-- 扫描所有的mapper接口的实现,让这些mapper能够自动注入;base-package:指定mapper接口的包名-->
	 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.sundi.hrm.dao"></property>
	</bean> 
	<!--此条和上面这个功能一样 <mybatis-spring:scan base-package="com.atguigu.mybatis.dao"/> -->
 
   <!-- ............................................华丽的分割线............................................... -->      
                   
</beans>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值