SSM框架整合核心内容
所需要的jar包及其版本
Spring 版本:4.3.18 tx、aop、beans、core、web、web-mvc、context、expression、jdbc
MyBatis:3.4.6
mybatis-spring:1.3.2
mysql-connector 连接数据库:5.1.44
fastjson 生成json:1.2.13
druid 数据源:1.1.9
jsr 303 数据校验:hibernate-validate4.3.2、jboss-loggin3.1.0CR2、validation-api1.0GA
面向切面:aopaliance、aspectjweaver1.8.13
文件上传:commons-io、commons-fileupload、commons-loggin1.2
jsp、jstl:jstl.jar、standart.jar
log4j:1.2.17
准备配置文件:
database.properties 连接数据库
log4j.properties 输出日志
mybatis-config.xml mybatis配置文件
applicationContext-mybatis.xml spring配置数据操作的配置文件
springmvc-servlet.xml springmvc的配置文件
web.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> <settings> <!--日志实现--> <setting name="logImpl" value="log4j"/> <!--非懒加载--> <setting name="lazyLoadingEnabled" value="false"/> </settings> <!--实体类别名--> <typeAliases> <package name="cn.smbms.pojo"/> </typeAliases> </configuration>
applicationContext-mybatis.xml
<!--引入database.properties--> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:database.properties</value> </property> </bean> <!--数据源--> <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.user}"/> <property name="password" value="${jdbc.pwd}"/> </bean> <!--sqlSessionFactoryBean--> <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!--mybatis-config.xml文件--> <property name="configLocation" value="classpath:mybatisconfig.xml"/> <!--mapper文件路径--> <property name="mapperLocations"> <list> <!--模糊匹配--> <value>classpath:cn/smbms/dao/**/*.xml</value> </list> </property> </bean> <!--批量注入映射器实现 MapperScannerConfigurer--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.smbms.dao"/> </bean> <!--事务管理器--> <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="txManager"> <property name="dataSource" ref="dataSource"/> </bean> <!--启用声明式事务注解--> <tx:annotation-driven transaction-manager="txManager"/> <!--启用aop的增强注解--> <aop:aspectj-autoproxy/>
springmvc-servlet.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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--支持注解配置Bean--> <context:component-scan base-package="cn.smbms.controller"/> <!--视图对应--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <!--静态资源--> <mvc:resources mapping="/statics/**" location="/statics/"/> <!--spring mvc 注解支持 和消息转换器--> <mvc:annotation-driven> <mvc:message-converters> <!--spring 消息转换器 防止中文乱码--> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=utf-8</value> </list> </property> </bean> <!--fastJson 消息转换器 格式化日期--> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=utf-8</value> </list> </property> <property name="features"> <list> <value>WriteDateUseDateFormat</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!--文件上传--> <bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8"/> <property name="maxUploadSize" value="2000000"/> </bean> <!--拦截器--> <mvc:interceptors> <mvc:interceptor> <!--拦截的路径--> <mvc:mapping path="/sys/**"/> <bean class="cn.smbms.interceptor.SysInterceptor"/> </mvc:interceptor> </mvc:interceptors> </beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <!--springmvc过滤器--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--加载springmvc配置文件--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--springmvc 请求编码过滤器--> <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> <!--spring监听器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listenerclass> </listener> <!--上下文参数,指定spring配置文件所在目录--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-*.xml</param-value> </context-param> </web-app>