关于SSM的配置文件和Web.xml的配置

来源:互联网 发布:万达网络科技集团 编辑:程序博客网 时间:2024/05/22 13:46

首先声明:此文章只代表我个人的立场 关于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 configurationPUBLIC "-//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全部的配置文件.

原创粉丝点击