ssm整合配置文件

ssm整合配置文件

整合思路:
Dao層:
1、SqlMapConfig.xml,空文件即可。需要文件頭。
2、applicationContext-dao.xml。
a)數據庫連接池
b)SqlSessionFactory對象,需要spring和mybatis整合包下的。
c)配置mapper文件掃描器。

Service層:
1、applicationContext-service.xml包掃描器,掃描@service註解的類。
2、applicationContext-trans.xml配置事務。

Controller層:
Springmvc.xml
1、包掃描器,掃描@Controller註解的類。
2、配置註解驅動。
3、視圖解析器

Web.xml
1.配置spring容量監聽器
2.配置前端控制器

SqlMapConfig.xml

<?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>
</configuration>

applicationContext-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
	
	
	<!--加載配置文件  -->
	<context:property-placeholder location="classpath:jdbc.properties"/>
	
	<!--數據庫連接池  -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}"/>
		<property name="url" value="${jdbc.url}"/>
		<property name="username" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"/>
		
		<!--連接池的最大數據庫連接數  -->
		<property name="maxActive" value="10"/>
		<!--最大空閒數  -->
		<property name="maxIdle" value="5"/>
	</bean>
	
	<!--sqlsessionFactory配置  -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
			<property name="dataSource" ref="dataSource"/>
			
			<!--加載mybatis核心配置文件  -->
			<property name="configLocation" value="classpath:SqlMapConfig.xml"/>
			<!-- 別名包掃描 -->
			<property name="typeAliasesPackage" value="springmvc.ssm.po"/>
	</bean>
	
	<!--動態代理,第二種方式:包掃描(推薦)  -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
			<!-- basePackage多個包用“,”分隔 -->
			<property name="basePackage" value="springmvc.ssm.mapper"/>
	</bean>
</beans>

applicationContext-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util ht
	tp://www.springframework.org/schema/util/spring-util-4.0.xsd">
	
	<!--@Service掃描包  -->
	<context:component-scan base-package="springmvc.service"/>
</beans>

applicationContext-trans.xml配置事務

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
	
	<!--事務管理器  -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!--數據源  -->
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
	<!--通知  -->
<!-- 	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<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="get*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="query*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>
	</tx:advice> -->
	
	<!--切面  -->
	<!-- <aop:config>
		<aop:advisor advice-ref="txAdvice"
		pointcut="execution(* springmvc.service.*.*(..))" />
	</aop:config>-->
</beans> 

Springmvc.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-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
     
     <!-- 配置@controller掃描包 -->
     <context:component-scan base-package="springmvc.controller"/>
     
     <!--配置註解驅動,相當於同時使用最新處理器映射器跟處理器適配器。對json數據響應提供支持  -->
     <mvc:annotation-driven/>
     
     <!-- 配置視圖解析器 -->
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     		<property name="prefix" value="/WEB-INF/jsp/"/>
     		<property name="suffix" value=".jsp"/>
     </bean>
</beans>

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>springmvc-mybatis3</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <!--配置spring  -->
  <context-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:spring/applicationContext*.xml</param-value>
  </context-param>
  
  <!--使用監聽器加載Spring配置文件  -->
   <listener>
  		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener> 
  
  
  <!-- 解決post亂碼問題 -->
	<filter>
		<filter-name>encoding</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<!-- 設置編碼參是UTF8 -->
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
  
  <!--前端控制器  -->
  <servlet>
  		<servlet-name>springmvc-web</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>
  </servlet>
  
  <servlet-mapping>
  		<servlet-name>springmvc-web</servlet-name>
  		<url-pattern>*.action</url-pattern>
  </servlet-mapping>
</web-app>

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springmvc?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456

日誌文件log4j.properties

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章