關於SSM的配置檔案和Web.xml的配置
阿新 • • 發佈:2019-01-10
首先宣告:此文章只代表我個人的立場 關於SSM的配置檔案好多種配置方法 我只舉其一.
博主用到的SSM版本如下:
spring 4.3.6
mybatis-3.2.3
資料來源用的第三方c3p0
配置檔案一般情況是上邊(不排除有些人把springsmv和spring分開些 都可以)
首先第一個 applicationcontext.xml
第一部分就是xsd約束檔案
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd ">
</beans>
第二部分就是註解掃描和mvc的掃描
<!-- 註解掃描 -->
<context:component-scan base-package="com.test"></context:component-scan>
<!-- mvc註解掃描-->
<mvc:annotation-driven />
第三部分就是來配置資料來源了
<!-- 配置資料來源 c3p0 -->
<!--這第一句話的意思是引用你的jdbc.properties檔案 如果你不想引用外邊的properties檔案 可以把下邊的資料寫死 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<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-framework-reference 19.3.1可以找到)
第四部分就是配置sqlsessionfactory
<!-- 配置sqlsessionfactory -->
<bean id="sqlsessionfactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 這句程式碼的意思找到你配置的mybatis-congif.xml 中配置的bean的別名-->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
(第五部分就是配置檢視解析器 也是可以從官方文件可以找到22.5)
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
(第六部分就是配置掃描Mapper以及子目錄的檔案)
<!-- 掃描mapper -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ssm.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlsessionfactory"></property>
</bean>
(第七部分就是配置事務了 當然還有事務驅動)
<!-- 配置事務 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 事務驅動 -->
<tx:annotation-drive
這就是applicationContext.xml的配置檔案
第二個配置檔案就是mybatis-config.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>
<typeAliases>
<package name="com.ssm.bean" />
</typeAliases>
</configuration>
最後的就是Web.xml啦 有好多同學在專案啟動的時候都報錯
很多原因都是Web.xml沒有配置好
下面我們一起來看web.xml的配置
(一共分四部分 前三部分可以在spring官方文件22.2找到,而post亂碼解決可以在springsmc的百度百科中找到)
<!-- 載入spring配置檔案 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- servlet核心控制器 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- servlet監聽器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置解決亂碼問題 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
以上就是SSM全部的配置檔案.