1、导包
1)、Spring
【AOP核心】
aopalliance.jar
aspectjweaver.jar
cglib-2.2.2.jar
spring-aspects-4.1.6.RELEASE.jar
【IOC核心】
commons-logging-1.1.1.jar
spring-aop-4.1.6.RELEASE.jar
spring-beans-4.1.6.RELEASE.jar
spring-context-4.1.6.RELEASE.jar
spring-core-4.1.6.RELEASE.jar
spring-expression-4.1.6.RELEASE.jar
【JDBC核心】
spring-jdbc-4.1.6.RELEASE.jar
spring-orm-4.1.6.RELEASE.jar
spring-tx-4.1.6.RELEASE.jar
【测试核心】
spring-test-4.1.6.RELEASE.jar
2)、SpringMVC
【SpringMVC核心】
spring-web-4.1.6.RELEASE.jar
spring-webmvc-4.1.6.RELEASE.jar
【文件上传下载】
commons-fileupload-1.4.jar
commons-io-2.6.jar
【jstl-jsp标准标签库】
jstl.jar
taglibs-standard-impl-1.2.5.jar
taglibs-standard-spec-1.2.5.jar
【数据校验】
classmate-1.0.0.jar
jboss-logging-3.1.3.GA.jar
validation-api-1.1.0.Final.jar
hibernate-validator-5.1.0.Final.jar
hibernate-validator-annotation-processor-5.1.0.Final.jar
【AJAX】
jackson-core-2.7.5.jar
jackson-databind-2.7.5.jar
jackson-annotations-2.7.5.jar
3)、Mybatis
【Mybatis核心】
mybatis-3.4.1.jar
mybatis-spring-1.2.3.jar
【ehcache整合】
log4j-1.2.17.jar
ehcache-core-2.6.11.jar
mybatis-ehcache-1.1.0.jar
slf4j-api-1.7.25.jar
slf4j-log4j12-1.7.25.jar
4)、其他包
【c3p0数据源】
c3p0-0.9.5.2.jar
mchange-commons-java-0.2.12.jar
【mysql数据库驱动】
mysql-connector-java-5.1.1.jar
【验证码】
kaptcha-2.3.2.jar
2、写配置
1、web配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>1.SSM_merge</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置Spring容器启动 -->
<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 指定Spring配置文件位置 -->
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置SpringMVC的前端控制器 -->
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 两个标准配置 -->
<!-- 字符编码 -->
<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>
<!-- 支持Rest风格的过滤器 -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
2、Spring配置
<?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: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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<!-- 引入外部配置文件 -->
<context:property-placeholder location="classpath:dbconfig.properties"/>
<!-- 除了控制器不扫描其余都扫描 -->
<context:component-scan base-package="com.wx.ssm">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<!-- 最大连接数 -->
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>
<!-- 配置Mybatis操作数据库-->
<!-- 可以根据配置文件得到SqlSessionFactory -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定配置文件位置 -->
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
<property name="dataSource" ref="dataSource"></property>
<!-- 指定xml映射文件的位置 -->
<property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"></property>
</bean>
<!-- 要把每一个Dao接口实现加入到IOC容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定dao接口所在的包 -->
<property name="basePackage" value="com.wx.ssm.dao"></property>
</bean>
<!-- 配置事务控制;配置事务管理器,让它控制住数据源里面的链接、关闭、提交 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 基于xml配置,配置事务,哪些方法切入事务还要写切入点表达式 -->
<aop:config>
<aop:pointcut expression="execution( * com.wx.ssm.service.*.*(..))" id="txPoint"/>
<aop:advisor advice-ref="myTx" pointcut-ref="txPoint"/>
</aop:config>
<!-- 配置事务增强,transaction-manager:指定要配置的事务管理器的id-->
<tx:advice id="myTx" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" rollback-for="java.lang.Exception"/>
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 开启基于注解的事务控制模式;依赖tx名称空间 -->
<!-- <tx:annotation-driven transaction-manager="transactionManager"/> -->
<!-- 给事务方法加入注解 -->
</beans>
3、SpringMVC配置
<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
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-4.1.xsd">
<!-- SpringMVC只扫描控制器
expression:写的全类名
禁用默认扫描规则
-->
<context:component-scan base-package="com.wx.ssm" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置国际化 id必须是messageSource -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="guojihua/i18n"></property>
</bean>
<!-- 文件上传解析器 id必须是multipartResolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默认上传最大30MB -->
<property name="maxUploadSize" value="#{1024*1024*30}"></property>
<property name="defaultEncoding" value="UTF-8"></property>
</bean>
<!-- 动态扫描 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 静态扫描 -->
<mvc:default-servlet-handler/>
</beans>
4、Mybatis配置
<?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="mapUnderscoreToCamelCase" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
<!-- 开启二级缓存 -->
<setting name="cacheEnabled" value="true"/>
</settings>
<typeAliases>
<typeAlias type="com.wx.ssm.bean.Employee" alias="emp"/>
<typeAlias type="com.wx.ssm.bean.Department" alias="dept"/>
</typeAliases>
</configuration>
5、整合步骤
1)、导入整合包;(能将dao的实现加入到容器中)
mybatis-spring-1.2.3.jar
<!-- 配置Mybatis操作数据库-->
<!-- 可以根据配置文件得到SqlSessionFactory -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定配置文件位置 -->
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
<property name="dataSource" ref="dataSource"></property>
<!-- 指定xml映射文件的位置 -->
<property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"></property>
</bean>
<!-- 要把每一个Dao接口实现加入到IOC容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定dao接口所在的包 -->
<property name="basePackage" value="com.wx.ssm.dao"></property>
</bean>
6、其他配置