1.SSM框架简介
SSM框架指的是SpringMVC+Spring+MyBatis(连接mysql数据库)
2.配置文件
(1)applicationContext-mvc.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: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.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.xsd">
<!--1. 配置扫描包路径子容器只扫描controller包(cn.itsource.controller这个路径自己修改-->
<context:component-scan base-package="cn.itsource.controller" />
<!-- 2. 配置静态资源放行 -->
<mvc:default-servlet-handler />
<!-- 3. 配置开启Spring对mvc的支持,支持@RequestMapping注解 -->
<mvc:annotation-driven>
<!-- 避免在IE浏览器中返回JSON时出现下载文件的情况 -->
<mvc:message-converters>
<bean id="mappingJackson2HttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 配置上传解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<!-- 允许上传附件最大的MB -->
<property name="maxUploadSize" value="10485760"/>
</bean>
<!-- 4. 配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<!--配置拦截器-->
<mvc:interceptors>
<!-- 配置单个拦截器 -->
<mvc:interceptor>
<!--
拦截
/* 表示单层路径结构
/** 表示可以嵌套多层路径结构
-->
<mvc:mapping path="/article/**"/>
<!--放行-->
<mvc:exclude-mapping path="/login/login"/>
<!-- 自己写的拦截器类-->
<bean class="cn.itsource.interceptor.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>
(2)applicationContext.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: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.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.xsd">
<!-- 1. 配置扫描包路径子容器只扫描service包(cn.itsource.service路径自己修改)-->
<context:component-scan base-package="cn.itsource.service" />
<!-- 2. 管理属性文件,查找当前src下查找jdbc.properties文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 3. 管理连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<!-- 三斜杠代表localhost:3306 -->
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 4.配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 配置mybatis (mapper)映射器路径 -->
<property name="mapperLocations" value="classpath:cn/itsource/mapper/*Mapper.xml" />
<!-- 配置mybatis 类型别名,多个包的配置(cn.itsource.domain路径自改) -->
<property name="typeAliasesPackage">
<value>cn.itsource.domain</value>
</property>
</bean>
<!-- 扫描Mapper接口包,自动生成Mapper接口的代理类,建议使用,会自动注入sqlSessionFactory,一劳永逸 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.itsource.mapper"></property>
</bean>
</beans>
(3)jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///ssm?useUnicode=true&characterEncoding=UTF-8
jdbc.username=用户名
jdbc.password=密码
(4)log4j.properties
log4j.rootLogger=ERROR, stdout
#log4j.rootLogger=NONE
log4j.logger.cn.itsource=TRACE
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
(5)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_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>day0301_ssm</display-name>
<!-- 1:独立容器方案,Spring和SpringMVC都有自己配置文件,实现责任分离 -->
<!-- 1.1 Spring容器初始化 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 1.2 配置SpringMVC前端控制器 -->
<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:applicationContext-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- /*会拦截所有包括jsp -->
<!-- /不会拦截jsp,但是会拦截静态资源html、css、js、图片等 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 1.3 解决post请求参数中文乱码问题 -->
<!-- post请求乱码解决 get请求实在tomcat的server.xml中配置URIEncoding="UTF-8" -->
<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>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>/home/index.html</welcome-file>
</welcome-file-list>
</web-app>
3.温馨提示
这些配置文件的文件夹为资源文件,且需要导包!!!