SSM框架整合

思路

软件三层架构
在这里插入图片描述
SSM即SpringMVC+Spring+MyBatis,简称SSM
SpringMVC管理控制层,Spring管理Service,MyBatis管理Dao层
SpringMVC的配置文件:springmvc.xml
spring的配置文件:applicationContext.xml
MyBatis的配置文件:mybatis.xml
项目工程文件:web.xml
(一般的web工程中都会用到web.xml,web.xml主要用来配置,可以方便的开发web工程。web.xml主要用来配置Filter、Listener、Servlet等。但是要说明的是web.xml并不是必须的,一个web工程可以没有web.xml文件)
在web.xml中整合进spring、springmvc
在spring配置文件中整合进mybatis

操作步骤

在这里插入图片描述
第一步:
  导入jar包:springmvc+spring+mybatis+mybatis-spring.jar+mysql/c3p0+jstl
第二步:
  在web.xml文件中配置spring的监听器【ContextLoaderListener】、springmvc的前
端控制器、两个Filter
第三步:
  创建springmvc.xml并配置
  扫描包【@Controller、@ControllerAdvice】
  配置内部资源视图解析器
  Mvc:default-servlet-handler标签
  Mvc:anntotation-driven
第四步:
  创建applicationContext.xml并配置
  扫描包【除了加@Controller、@ControllerAdvice】
  加载properties配置文件信息
  配置数据源
  配置数据源事务管理器
  开启基于注解的事务支持
  配置SqlSessionFactoryBean对象
  DataSource:配置数据源
  configLocation:加载MyBatis全局配置文件
  配置MapperScannerConfigurer对象
  BasePackage:指定Mapper接口所在的包
第五步:写测试代码,测试

代码部分

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>test3</display-name>
  <!-- 指定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的前端控制器 -->
	<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:springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	<!--配置Filter,处理POST请求乱码  -->
	<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>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!--将put请求转换为put或delete请求的filter  -->
	<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>

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-4.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

	<!-- 配置扫描包 -->
	<context:component-scan base-package="com.weilinyang">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
		<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvices"/>
	</context:component-scan>
	<!--加载properties配置文件-->
	<context:property-placeholder location="classpath:db.properties"/>
	<!--配置数据源信息-->
	<bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
		<property name="user" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="driverClass" value="${jdbc.className}"/>
	</bean>
	
	<!-- 配置数据源事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <property name="dataSource" ref="comboPooledDataSource"/>
    </bean>
    
    <!-- 开启基于注解的事务支持 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
    
    <!--配置SqlSessionFactoryBean对象-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
    	<property name="dataSource" ref="comboPooledDataSource"/>
    	<property name="configLocation" value="classpath:mybatis.xml"></property>
    </bean>
    <!--配置扫描Mapper接口的类-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    	<property name="basePackage" value="com.weilinyang.dao"/>
    </bean>
</beans>

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: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.0.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.0.xsd">

	<!-- 配置扫描包 -->
	<context:component-scan base-package="com.weilinyang" use-default-filters="false">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
		<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvices"/>
	</context:component-scan>
	<!--配置内部资源视图解析器-->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	<!--配置处理静态资源文件的标签-->
	<mvc:default-servlet-handler/>
	
	
	<mvc:annotation-driven/>
</beans>

mybatis.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>
  
  <settings>
      <!-- 开启懒加载 -->
     <setting name="lazyLoadingEnabled" value="true"/>
     <setting name="aggressiveLazyLoading" value="false"/>
  </settings>
	
</configuration>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值