SSM配置文件之二

配置文件:SSM

在这里插入图片描述
1.web.xml
(springmvc的web.xml文件< web-app>< /web-app>)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
		  http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd"
         version="3.0">
         
         <!-- 1.配置DiapatcherServlet前端控制器 -->
         <servlet>
         	<servlet-name>springmvc</servlet-name>
         	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	        <init-param> 
	         	<param-name>contextConfigLocation</param-name>
	         	<param-value>classpath:spring/springmvc.xml</param-value>
	        </init-param>
	        <load-on-startup>1</load-on-startup> 
         </servlet>
         <servlet-mapping>
         	<servlet-name>springmvc</servlet-name>
         	<url-pattern>*.action</url-pattern> 
         </servlet-mapping>
         
         <!-- 2.加载启动Spring框架 -->
         <context-param> // 加载spring容器
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/applicationContext-*.xml</param-value>
         </context-param>
         <listener> // 加载监听
             <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
         </listener>

         <welcome-file-list>         	
         </welcome-file-list>
</web-app>

2.springmvc.xml
(springmvc配置文件< beans>< /beans>)

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" 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" xmlns:aop="http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> 
	
	<!-- 3.(开启spring注解扫描)注解开发的Handler -->
	<context:component-scan base-package="com.asd"></context:component-scan>

	<!-- 1.注解映射器 -->  
	<bean name="" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingInfoHandlerMapping">
	</bean>

	<!-- 2.注解适配器 --> 
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>

	<!-- 4.视图解析器 --> 
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>

3.1 applicationContext-dao.xml
(spring配置文件< beans>< /beans>)

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 1. 配置数据源 -->
       <!-- 加载属性文件 -->
       <context:property-placeholder location="classpath:db.properties"/>
       <!-- 数据库连接池 -->
       <bean id="dataSource" class="com.mchange.v2.com.mchange.v2.c3p0.ComboPooledDataSource">
    	<property name="driverClass" value="${jdbc.driver}"></property>
    	<property name="jdbcUrl" value="${jdbc.url}"></property>
    	<property name="user" value="${jdbc.username}"></property>
    	<property name="password" value="${jdbc.password}"></property>
    	<property name="maxActive" value="10"></property>
    	<property name="maxIdle" value="5"></property>
    </bean>

    <!-- 2. SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 配置数据源 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- 加载mybatis的配置文件 -->
        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>
    </bean>
    
    <!-- 3. Mapper扫描器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.asd.mapper"/>
    </bean>   
</beans>

3.2 applicationContext-service.xml
(spring配置文件< beans>< /beans>)

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 商品管理的service -->
    <bean id="itemsService" class="com.asd.service.ItemsServiceImpl"></bean>
    
</beans>

3.3 applicationContext-transaction.xml
(spring配置文件< beans>< /beans>)

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 1.事务管理器 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    	<property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 2.事务通知 (即如何管理) -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
    	<tx:attributes>
    		<tx:method name="save" propagation="REQUIRED"/>
    		<tx:method name="insert" propagation="REQUIRED"/>
    		<tx:method name="delete" propagation="REQUIRED"/>
    		<tx:method name="update" propagation="REQUIRED"/>
    		<tx:method name="find" propagation="SUPPORTS" read-only="true"/>
    		<tx:method name="select" propagation="SUPPORTS" read-only="true"/>
    		<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
    	</tx:attributes>
    </tx:advice>
    <!-- 3.AOP,拦截什么方法(切入点表达式)+增强 -->
    <aop:config>
    	<aop:pointcut id="pt" expression="execution(* com.asd.ssm.service.*.*(..))"/>
    	<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
    </aop:config>
    
</beans>

4. SqlMapConfig.xml
(mybatis配置文件< configuration>< /configuration>)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
       
<configuration>
    <typeAliase> 
        <package name="com.iotek.po"/>
    </typeAliase>
</configuration>

猜你喜欢

转载自blog.csdn.net/qq_41029923/article/details/84645662
今日推荐