SSM(Spring+SpringMVC+Mybatis)整合详细步骤

SSM整合原理(思路):
Spring(把管理权给spring) -springmvc -mybatis
1.spring - mybatis : 需要整合,将mybatis的SqlSessionFactory交给spring

2.spring-springmvc: 就是将spring-springmvc各自的配置配置一遍

SM整合详细步骤:
1.jar包
(整合Mybatis 所需的jar包:Mybatis-spring.jar spring-tx.jar spring-jdbc.jar spring-exception.jar spring-context-support.jar spring-core.jar spring-context.jar spring-beans.jar spring-aop.jar spring-web.jar commons-logging.jar commons-dbcp.jar mysql-connector-java mybatis.jar log4j.jar commons-pool.jar )
(配置springmvc所需的jar包:spring-webmvc.jar

2.类-表
3.—(mybatis与spring整合时,conf.xml可省)----Mybatis配置文件mybatis-config.xml(数据源、mapper.xml)--------可省,将该文件中的配置全部交给spring管理

4.通过mapper.xml将类、表建立映射关系(可以通过mybatis-generator来自动生成:详细配置见:mybatis-generator代码生成配置文件)

5.之前使用MyBatis:conf.xml ->SqlSessionFactory
现在整合的时候,需要通过Spring 管理SqlSessionFactory,因此产生SqlSessionFactory所需要的数据库信息

配置Spring 配置文件(applicationContext.xml —>得在web.xml引入这个配置的文件):
在web.xml引入applicationContext.xml

 <!-- 引入spring配置文件 -->
  <!-- needed for ContextLoaderListener -->
	<context-param> 
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath: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>

在applicationContext.xml中的具体配置:

<!--①:首先在里面配置数据源(整合mybatis的准备工作);-->
<!-- 数据源、mapper.xml ,以前是在mybatis里的conf.xml里配置的,现在在sqlSessionFactory里配置-->



<!-- 数据源 -->   
注意:由于在数据源里,里面的属性值用到属性文件jdbc.propertier,所以得加下面这个设置:
<!-- 加载数据库jdbc.properties设置 -->
    <bean id="config" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    	<property name="location" value="classpath:jdbc.properties"></property>
    </bean>


<!-- 配置数据库信息(代替mybatis的配置文件mybatis-config.xml -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <!-- 基本属性 url、user、password -->
        <property name="driverClassName" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
        </bean>


<!--②:创建mybatis的核心类sqlsessionfactory-->
<!-- 在pringIOC容器中创建mybatis的核心类sqlsessionfactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 在这里引用数据源 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描mapping.xml文件 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!--③将Mybatis的SqlSessionFaction 交给Spring(将mybatis的控制权交给spring)-->
 	<!-- Spring整合Mybatis:将Mybatis的SqlSessionFaction 交给Spring -->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
   		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<!-- 指定批量产生哪个包中的mapper对象 ,对象名就是首字母小写的接口名-->
        <property name="basePackage" value="mapper" />    
    </bean>
    <!-- 上面的basePaqckage所在的property的作用:
    		将mapper包中的,所有接口产生与之对应的动态代理对象(对象名是首字符小写的接口名)
     -->
    <!--  <bean id = "studentService" class="service.Impl.StudentServiceImpl">
     	<property name="studentMapper" ref="studentMapper"></property>
     </bean> -->

jdbc.properties配置文件:

driver =com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/wechatstudio?useUnicode=true&serverTimezone=GMT&characterEncoding=utf-8
username=root
password=123456

6.继续整合SpringMVC(也就是配置SpringMVC):
a.加入SpringMVC所需要的jar包:spring-webmvc.jar
b.加入SpringMVC项目支持:springmvc配置文件:pringmvc.xml(写其他名字也可以,比如:applicationContext-Controller.xml)
在web.xml里alt+/,选择dispatcherServlet,加入这个配置文件配置,详情如下:

<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:applicationContext-Controller.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>

c.编写springmvc配置文件
springmvc.xml:视图解析器+两个基础配置
视图解析器:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<property name="prefix" value="/WEB-INF/views/"></property>
	<property name="suffix" value=".jsp"></property>
</bean>

两个基础配置:

<mvc:annotation-driven></mvc:annotation-driven>
<mvc:default-servlet-handler></mvc:default-servlet-handler>

最重要的一点需要加上两个(不然就无法扫描识别@controller和@service,出现404):

<context:component-scan base-package="controller"></context:component-scan>
<context:component-scan base-package="service"></context:component-scan>

综上整合在一起的springmvc.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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.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.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

		<context:component-scan base-package="controller"></context:component-scan>
		<context:component-scan base-package="service"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
	<property name="prefix" value="/WEB-INF/views/"></property>
	<property name="suffix" value=".jsp"></property>
</bean>

<mvc:annotation-driven></mvc:annotation-driven>
<mvc:default-servlet-handler></mvc:default-servlet-handler>

</beans>

Ssm:
controller–>service–>dao

由于普通浏览器不支持delete和put, 所以需要在web.xml配置一个过滤器:

<!-- 增加HiddenHttpMethodFilter过滤器,目的是为了给普通浏览器怎么加put/delete请求方式 -->
	<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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值