1、集成spring+springMVC
1.1、导包
1.2、Web.xml
<?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>ssm</display-name>
<!-- spring的配置 -->
<!-- spring核心配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- spring启动的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- springmvc -->
<!-- springmvc中央控制器 -->
<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-mvc.xml</param-value>
</init-param>
<!-- servlet启动的优先级,配置后Servlet会在web容器启动的时候创建实例并初始化 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<!-- 兼容RESTful风格的url /employee/delete/1 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- post中文乱码处理的过滤器 -->
<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>
</web-app>
1.3、applicationContext-mvc.xml 5个配置
<?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">
<!-- 扫描的包,这里只扫描controller -->
<context:component-scan base-package="cn.itsource.ssm.controller"/>
<!-- 开启mvc的全注解 -->
<mvc:annotation-driven/>
<!-- 静态资源放行 -->
<mvc:default-servlet-handler/>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置视图的前缀和后缀 -->
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 文件上传解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大尺寸为1MB -->
<property name="maxUploadSize">
<!-- spring el写法:5MB -->
<value>#{1024*1024*5}</value>
</property>
</bean>
</beans>
2、集成spring+mybatis
2.1、导包
- 核心包
- 依赖包(删除一个commons-logging-1.1.1.jar)
- 数据库连接包
2.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: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.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--
回忆:spring整JPA
引入属性文件==》数据源 ==> EntityManagerFactory ==> 事务
如何整Mybatis
引入属性文件==》数据源 ==> SqlSessionFactory ==> 事务
-->
<!-- 扫描包 service层 -->
<context:component-scan base-package="cn.itsource.ssm.service"></context:component-scan>
<!-- 用它来整合mybatis -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 将SqlSessionFactory交给Spring管理 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 配置别名 -->
<property name="typeAliasesPackage" value="cn.itsource.ssm.domain"></property>
<!-- 注册映射文件 classpath*不仅会在源码中找,也会在jar包中找-->
<property name="mapperLocations" value="classpath*:cn/itsource/ssm/mapper/*Mapper.xml"></property>
</bean>
<!-- 注入映射器 -->
<!-- <bean id="deptMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="cn.itsource.ssm.mapper.DeptMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean> -->
<!-- 一劳永逸的做法 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- mapper接口所在的包 -->
<property name="basePackage" value="cn.itsource.ssm.mapper" />
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
2.3、配置顺序
2.3.1、第一步:jdbc配置文件&数据源dataSource
<!-- 引入jdbc的配置 -->
<context:property-placeholder location="classpath:*properties"/>
<!-- 数据源dataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- 依赖注入连接池需要的属性 -->
<!-- property name="是BasicDataSource的set方法,本质属性" -->
<!-- property value="是jdbc.properties配置文件的key" -->
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!--maxActive: 最大连接数量 -->
<property name="maxActive" value="150" />
<!--minIdle: 最小空闲连接 -->
<property name="minIdle" value="5" />
<!--maxIdle: 最大空闲连接 -->
<property name="maxIdle" value="20" />
<!--initialSize: 初始化连接 -->
<property name="initialSize" value="30" />
<!-- 连接被泄露时是否打印 -->
<property name="logAbandoned" value="true" />
<!--removeAbandoned: 是否自动回收超时连接 -->
<property name="removeAbandoned" value="true" />
<!--removeAbandonedTimeout: 超时时间(以秒数为单位) -->
<property name="removeAbandonedTimeout" value="10" />
<!--maxWait: 超时等待时间以毫秒为单位 1000等于60秒 -->
<property name="maxWait" value="1000" />
<!-- 在空闲连接回收器线程运行期间休眠的时间值,以毫秒为单位. -->
<property name="timeBetweenEvictionRunsMillis" value="10000" />
<!-- 在每次空闲连接回收器线程(如果有)运行时检查的连接数量 -->
<property name="numTestsPerEvictionRun" value="10" />
<!-- 1000 * 60 * 30 连接在池中保持空闲而不被空闲连接回收器线程 -->
<property name="minEvictableIdleTimeMillis" value="10000" />
<property name="validationQuery" value="SELECT NOW() FROM DUAL" />
</bean>
2.3.2、第二步:SqlSessionFactoryBean的配置
<!-- 将SqlSessionFactory交给Spring管理 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 配置别名 -->
<property name="typeAliasesPackage" value="cn.itsource.ssm.domain"></property>
<!-- 注册映射文件 classpath*不仅会在源码中找,也会在jar包中找-->
<property name="mapperLocations" value="classpath*:cn/itsource/ssm/mapper/*Mapper.xml"></property>
</bean>
2.3.3、第三步:将mapper接口交给spring去注入
<!-- 注入映射器 -->
<!-- <bean id="deptMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="cn.itsource.ssm.mapper.DeptMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean> -->
<!-- 一劳永逸的做法 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- mapper接口所在的包 -->
<property name="basePackage" value="cn.itsource.ssm.mapper" />
</bean>
2.3.4、第四步:集成service
<!-- 扫描包 service层 -->
<context:component-scan base-package="cn.itsource.ssm.service"></context:component-scan>
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>