SSM框架配置用法

//SSM框架的配置
<!-- web.xml中的配置 -->
//Spring和MyBatis的配置文件
<context-param>
<param-name>contextConfigLocation</param-name>
//以下为Spring和MyBatis配置文件的路径及名称
<param-value>classpath:org/great/config/spring-mybatis.xml</param-value>
</context-param>

//Spring监听器
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

//防止Spring内存溢出监听器
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>

//中文乱码过滤器
<filter>
<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

//访问后跳转到SpringMVC框架
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:org/great/config/springmvc-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

==================================================================================================================================================

<!--springmvc-servlet.xml中的配置,在src根目录下 -->
<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<!-- 配置拦截器,多个拦截器,顺序执行 -->
<mvc:interceptors>
<mvc:interceptor>
<!-- 匹配的是url路径, 如果不配置或/**,将拦截所有的Controller -->
<!--
/unicom 只拦截该路径
/unicom/ 拦截路径为/unicom/最后还有斜杠
/* 拦截第一级路径
/** 拦截 多级路径
-->
<mvc:mapping path="/unicom/**"/>
<bean class="org.great.interceptor.MyInterceptor"></bean>
</mvc:interceptor>
<!-- 当设置多个拦截器时,先按顺序调用preHandle方法,然后逆序调用每个拦截器的postHandle和afterCompletion方法 -->
</mvc:interceptors>

<!-- 数据库连接资源 -->
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name="username" value="test" />
<property name="password" value="123456" />
</bean>
<!-- sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定连接资源 -->
<property name="dataSource" ref="myDataSource" />
<!-- 指定映射文件 -->
<property name="mapperLocations" value="classpath:org/great/config/mybatis/*.xml" />
</bean>
<!-- 加载mapper文件 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
<property name="basePackage" value="org.great.mapper" />
</bean>

<!-- 配置注解的支持 -->
<mvc:annotation-driven/>

<!-- 静态资源处理 -->
<mvc:default-servlet-handler/>

<!-- 启用自动扫描 -->
<context:component-scan base-package="org.great"/>

<!-- 配置视图解析器,即在模型视图名称添加前后缀 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/web/"></property>
<!-- 配置视图类型为jsp类型 -->
<property name="suffix" value=".jsp"></property>
</bean>

<!-- 上传文件 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

</beans>

==================================================================================================================================================

<!-- spring-mybatis.xml中的配置,在src根目录下 -->
<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<!-- 启用自动扫描 -->
<context:component-scan base-package="org.great"/>

<!-- 数据库连接资源 -->
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name="username" value="test" />
<property name="password" value="123456" />
</bean>
<!-- sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定连接资源 -->
<property name="dataSource" ref="myDataSource" />
<!-- 指定映射文件 -->
<property name="mapperLocations" value="classpath:org/great/config/mybatis/*.xml" />
</bean>
<!-- 加载mapper文件 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
<property name="basePackage" value="org.great.mapper" />
</bean>

</beans>

==================================================================================================================================================

posted on 2017-07-09 09:50  猴子敲代码  阅读(197)  评论(0编辑  收藏  举报