spring.xml

本文介绍了一个使用 Spring MVC 框架的 Java Web 应用程序配置示例,并展示了如何整合 MyBatis ORM 工具进行数据库操作。详细配置了数据源、事务管理、MyBatis 的 Session 工厂等关键组件。

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

<?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:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
	  	http://www.springframework.org/schema/beans 
	  	http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.2.xsd
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
	">
	<!-- spring 是 bean的容器(service+repository)
	 -->
	<context:component-scan base-package="cn">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
	<!-- 所有数据库操作的源头 实现 自接口DataSource
		DriverManagerDataSource(请求产生一个链接  用完关闭 尽量连接重用   连接池)
		
	 -->
	 <context:property-placeholder location="classpath:jdbc.properties"/>
	 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
	 	<property name="url" value="${url}"></property>
	 	<property name="driverClassName" value="${driverClass}"></property>
	 	<property name="username" value="${username1}"></property>
	 	<property name="password" value="${password}"></property>
	 	<!-- 默认初始化的连接个数 -->
	 	<property name="initialSize" value="1"></property>
	 	<!-- 最大允许的链接个数 -->
	 	<property name="maxActive" value="200"></property>
	 	<!-- 最大的等待人数 -->
	 	<property name="maxIdle" value="100"></property>
	 	<!-- 开启sql统计功能 -->
	 	<property name="filters" value="stat"></property>
	 </bean>
	 
	 <!-- 配置mybatis -->
	 <!-- 配置session工厂 -->
	 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	 	<property name="dataSource" ref="dataSource"></property>
	 </bean>
	 
	 <bean id="sessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
	 	<constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
	 </bean>
	 
	 <!-- 扫描mybatis的接口映射 -->
	 <bean id="scannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	 	<property name="basePackage" value="cn.*..*.dao"></property>
	 </bean>
	 
	 <!-- 配置管理器 -->
	 <bean id = "tm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	 	<property name="dataSource" ref="dataSource"></property>
	 </bean>
	 <tx:advice id="txAdvise" transaction-manager="tm">
	 	<tx:attributes>
	 		<tx:method name="save*" propagation="REQUIRED"/>
	 		<tx:method name="update*" propagation="REQUIRED"/>
	 		<tx:method name="delete*" propagation="REQUIRED"/>
	 		<tx:method name="*" read-only="true"/>
	 	</tx:attributes>
	 </tx:advice>
	 
	 <aop:config>
	 	<aop:pointcut expression="execution(* cn.*..*.service.*..*(..))" id="myPointCut"/>
	 	<!-- 关联切点和事务管理器 -->
	 	<aop:advisor advice-ref="txAdvise" pointcut-ref="myPointCut"/>
	 </aop:config>
	 
	 
	 
</beans>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值