SSM框架整合环境的搭建

由于Spring MVC是Spring框架中的一个模块,所以Spring MVC与Spring之间不存在整合的问题,只要引入相应JAR包就可以直接使用。因此SSM框架的整合就只涉及到了Spring与MyBatis的整合,以及Spring MVC与MyBatis的整合。
要实现SSM框架的整合,首先要准备这三个框架的JAR包,以及其他整合所需的JAR包。在第10章讲解Spirng与MyBatis框架整合时,已经介绍了Spring与MyBatis整合所需要的JAR包,这里只需要再加入Spring MVC的相关JAR包即可,具体如下:
spring-web-4.3.6.RELEASE.jar
spring-webmvc-4.3.6.RELEASE.jar
根据上述整合需求,SSM框架整合所需要的基本JAR包如下图所示:
在这里插入图片描述
一、 在Eclipse中,创建一个名为chapter17的Web项目,将整合所需的JAR包添加到项目的lib目录中,并发布到类路径下。
二、 在chapter17项目下,创建一个名为config的源文件夹(Source Folder),在该文件夹中分别创建数据库常量配置文件db.properties、Spring配置文件applicationContext.xml,以及MyBatis的配置文件mybatis-config.xml。
db.properties配置文件:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=root
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5

applicationContext.xml配置文件:

 <context:property-placeholder location="classpath:db.properties"/>
 <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
                <property name="driverClassName" value="${jdbc.driver}" />
                 ...
 </bean>
 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
                <property name="dataSource" ref="dataSource" /></bean>	
  <tx:annotation-driven transaction-manager="transactionManager"/>
  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
               <property name="dataSource" ref="dataSource" />
               <property name="configLocation" value="classpath:mybatis-config.xml" /></bean>
  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
               <property name="basePackage" value="com.itheima.dao"/></bean>
  <context:component-scan base-package="com.itheima.service" />

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>
	<typeAliases>
		<package name="com.itheima.po" />
	</typeAliases>
</configuration>

三、在config文件夹中,创建Spring MVC的配置文件springmvc-config.xml:

<context:component-scan base-package="com.itheima.controller" />
      <mvc:annotation-driven />
      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
      </bean>
四、在web.xml中,配置Spring的文件监听器、编码过滤器以及Spring MVC的前端控制器等信息
<context-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
         <filter-name>encoding</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>encoding</filter-name>
          <url-pattern>*.action</url-pattern>
</filter-mapping>
<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:springmvc-config.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>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值