SSM 整合

Posted zhangzixian

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM 整合相关的知识,希望对你有一定的参考价值。

整合后文件

技术图片

 

 

 

 

配置文件:

1. spring与mybatis配置文件:applicatonContext.xml

  1. 导入数据库配置文件
  2. 配置数据源信息
  3. 配置事务管理器
  4. 开启事务注解
  5. 配置 mybatis 的 sqlSessionFactoryBean
  6. 配置 mapper 扫描器
  7. 扫描 service 文件

 

技术图片
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:aop="http://www.springframework.org/schema/aop"
 5        xmlns:context="http://www.springframework.org/schema/context"
 6        xmlns:tx="http://www.springframework.org/schema/tx"
 7        xsi:schemaLocation="http://www.springframework.org/schema/beans
 8         http://www.springframework.org/schema/beans/spring-beans.xsd
 9         http://www.springframework.org/schema/aop
10         http://www.springframework.org/schema/aop/spring-aop.xsd
11         http://www.springframework.org/schema/tx
12         http://www.springframework.org/schema/tx/spring-tx.xsd
13         http://www.springframework.org/schema/context
14         http://www.springframework.org/schema/context/spring-context.xsd">
15 <!--    导入数据库配置文件-->
16     <context:property-placeholder location="classpath:config/db.properties"/>
17 <!--    配置数据源-->
18     <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
19         <property name="driverClassName" value="${jdbc.driver}"/>
20         <property name="url" value="${jdbc.url}"/>
21         <property name="username" value="${jdbc.username}"/>
22         <property name="password" value="${jdbc.password}"/>
23         <property name="maxTotal" value="${jdbc.maxTotal}"/>
24         <property name="maxIdle" value="${jdbc.maxIdle}"/>
25         <property name="initialSize" value="${jdbc.initialSize}"/>
26     </bean>
27 <!--    事务管理器-->
28     <bean id="transactionManager" 
29           class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
30         <property name="dataSource" ref="dataSource"/>
31     </bean>
32 <!--    开启事务注解-->
33     <tx:annotation-driven/>
34 <!--    配置 Mybatis 的 sqlSessionFactoryBean-->
35     <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
36         <property name="dataSource" ref="dataSource"/>
37         <property name="configLocation" value="classpath:config/mybatis-config.xml"/>
38     </bean>
39 <!--    配置 mapper 扫描器-->
40     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
41         <property name="basePackage" value="com.zhh.dao"/>
42     </bean>
43 <!--    扫描 service-->
44     <context:component-scan base-package="com.zhh.service"/>
45 </beans>
applicationContext.xml

 

2. springMVC 配置文件:springMVC-config.xml

  1. 包扫描,扫描 controller 注解
  2. 加载注解qvdong
  3. 配置视图扫描器,配置前缀后缀

 

技术图片
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:tx="http://www.springframework.org/schema/tx"
 6        xmlns:mvc="http://www.springframework.org/schema/mvc"
 7        xsi:schemaLocation="http://www.springframework.org/schema/beans
 8         http://www.springframework.org/schema/beans/spring-beans.xsd
 9         http://www.springframework.org/schema/tx
10         http://www.springframework.org/schema/tx/spring-tx.xsd
11         http://www.springframework.org/schema/context
12         http://www.springframework.org/schema/context/spring-context.xsd
13         http://www.springframework.org/schema/mvc
14         http://www.springframework.org/schema/mvc/spring-mvc.xsd">
15 <!--    包扫描器,扫描 controller 注解-->
16     <context:component-scan base-package="com.zhh.controller"/>
17 <!--    加载注解驱动-->
18     <mvc:annotation-driven/>
19 <!--    配置视图解析器-->
20     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
21         <property name="prefix" value="/WEB-INF/jsp/"/>
22         <property name="suffix" value=".jsp"/>
23     </bean>
24 </beans>
springMVC-config.xml

 

3. web.xml

  1. 配置 spring 文件监听器 org.springframework.web.context.ContextLoaderListener
  2. 添加编码过滤器 org.springframework.web.filter.CharacterEncodingFilter
  3. 配置springMVC控制器 org.springframework.web.servlet.DispatcherServlet

 

技术图片
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
 5          version="4.0">
 6 <!--    配置 spring 文件监听器-->
 7     <context-param>
 8         <param-name>contextConfigLocation</param-name>
 9         <param-value>classpath:config/applicationContext.xml</param-value>
10     </context-param>
11     <listener>
12         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
13     </listener>
14 <!--    编码过滤器-->
15     <filter>
16         <filter-name>encoding</filter-name>
17         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
18         <init-param>
19             <param-name>encoding</param-name>
20             <param-value>UTF-8</param-value>
21         </init-param>
22     </filter>
23     <filter-mapping>
24         <filter-name>encoding</filter-name>
25         <url-pattern>*.action</url-pattern>
26     </filter-mapping>
27 <!--    配置 springMVC 前端核心配置器-->
28     <servlet>
29         <servlet-name>springMVC</servlet-name>
30         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
31         <init-param>
32             <param-name>contextConfigLocation</param-name>
33             <param-value>classpath:config/springMVC-config.xml</param-value>
34         </init-param>
35         <!--服务器启动后立刻加载springMVC-->
36         <load-on-startup>1</load-on-startup>
37     </servlet>
38     <servlet-mapping>
39         <servlet-name>springMVC</servlet-name>
40         <url-pattern>/</url-pattern>
41     </servlet-mapping>
42 </web-app>
web.xml

 

以上是关于SSM 整合的主要内容,如果未能解决你的问题,请参考以下文章

SSM框架整合

SSM 项目整合详细解读

SSM整合

520前,我放弃陪女朋友时间,被迫写代码:“SSM框架整合+excel文件上传到数据库+数据更新“

shiro整合SSM(spring_springmvc_mybatis)

ssm整合redis