Java日记之SSM整合一

讲讲SSM整合 ,即spring ,springMVC ,Mybatis三个框架做整合 .老规矩,第一篇先搭建框架

一.基本步骤

1.加载spring(web.xml中) :
(1)与以往加载spring的方式不同的是,我们现在通过监听器listener,加载spring容器.在tomcat服务器启动时就会执行.

(2)同时我们还需传递初始化参数<context-param></~>
需要用到的jar包中的类 : contextLocationListener

(3)代码

	<!-- 加载spring容器配置文件 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

2.加载springMVC(web.xml中) :
配置中央控制器,与之前的步骤一致,再来复习一遍
(1)<servlet></~>标签中,<servlet-name>,<servlet-class>,<init-param>标签

(2)<servlet-mappering></~>标签中,<servlet-name>,<url-pattern>标签

(3)代码:

<!-- 加载springmvc配置文件 -->
  	<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.xml</param-value>
  		</init-param>
  		<load-on-startup>1</load-on-startup>
  	</servlet>
  	<servlet-mapping>
  		<servlet-name>springmvc</servlet-name>
  		<url-pattern>*.action</url-pattern>
  	</servlet-mapping>

3.配置编码过滤器 :
使用过滤器标签进行设置编码方式
代码:

<!-- 加载编码过滤器 -->
  	<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>

4.配置springMVC(springmvc.xml中) :
同之前的配置方法一样,这里就再来复习一下
(1)controller层配置 : <mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="com.hzyc.lesson"></context:component-scan>

(2)视图解析(就是改后缀前缀) : InternalResourceViewResolver文件的路径需要拷贝到bean标签的class属性中,然后就是设置前缀后缀

	<bean class="org.springframework.web.servlet.view.
		InternalResourceViewResolver">
			<property name="prefix" value=""></property>
			<!-- 前缀  -->
			<property name="suffix" value=".jsp"></property>
	</bean>

(3)文件上传 : spring规定文件上传的bean的id必须命名为multipartResolver
同时我们也要设置最大文件上传

	<bean id="multipartResolver" class="org.springframework.web.
		multipart.commons.CommonsMultipartResolver">
		<property name="maxUploadSize" value="10485760"></property>
	</bean>

5.配置spring(applicationContext.xml配置文件中) : 首先需要说明的,我们原先在mybatis中的数据源,mapper扫描等配置,现在都需要移植到spring的配置中去

约束
<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:aop="http://www.springframework.org/schema/aop" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	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/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/mvc
         http://www.springframework.org/schema/mvc/spring-mvc.xsd
         http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
	 destroy-method="close">
		<property name="driverClassName" 
		value="com.mysql.jdbc.Driver"></property>
		<property name="url" 
		value="jdbc:mysql://localhost:3308/msms"></property>
		<property name="username" value="root"></property>
		<property name="password" value="mysql"></property>
		<!-- 最大连接数 -->
		<property name="maxActive" value="10"></property>
	</bean>
<!-- sqlSessionFactoryBean -->
	<bean id="" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- mybatis配置文件的位置 -->
		<property name="configLocation" value="classpath:mybatis.xml"></property>
		<!-- 引入数据源 -->
		<property name="dataSource" ref="dataSource"></property>
	</bean>
<!-- mapper扫描 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.hzyc.lesson.mapper"></property>
	</bean>
	<!-- 扫描service -->
	<context:component-scan base-package="com.hzyc.lesson.service"></context:component-scan>
	 <!-- spring 事务管理 -->
	 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	 	<property name="dataSource" ref="dataSource"></property>
	 </bean>
	 <!-- 开启注解的事务-->
	 <tx:annotation-driven transaction-manager="transactionManager" />

6.配置mybatis(在mybatis.xml配置文件中) : 只需要一个别名标签即可

	<typeAliases>
		<package name="com.hzyc.lesson.model" />
	</typeAliases>
约束
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">

7.配置事务管理(applicationContext.xml) : 拷贝带tx的约束,再通过<tx:annotation-driven transaction-manager="transactionManager" /> ,可以通过@Transational开启事务

二.代码

把上述的代码串联起来,贴一下整段的代码

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:aop="http://www.springframework.org/schema/aop" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	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/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/mvc
         http://www.springframework.org/schema/mvc/spring-mvc.xsd
         http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
	
	<!-- 数据源 -->
	<bean id="dateSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://localhost:3308/msms"></property>
		<property name="username" value="root"></property>
		<property name="password" value="mysql"></property>
		<!-- 最大连接数 -->
		<property name="maxActive" value="10"></property>
	</bean>
	
	<!-- sqlSessionFactoryBean -->
	<bean id="" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- mybatis配置文件的位置 -->
		<property name="configLocation" value="classpath:mybatis.xml"></property>
		<!-- 引入数据源 -->
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- mapper扫描 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.hzyc.lesson.mapper"></property>
	</bean>
	<!-- 扫描service -->
	<context:component-scan base-package="com.hzyc.lesson.service"></context:component-scan>
	
	
	 <!-- spring 事务管理 -->
	 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	 	<property name="dataSource" ref="dataSource"></property>
	 </bean>
	 <!-- 开启注解的事务-->
	 <tx:annotation-driven transaction-manager="transactionManager" />
</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>
	<typeAliases>
		<package name="com.hzyc.lesson.model" />
	</typeAliases>
</configuration>
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:aop="http://www.springframework.org/schema/aop" 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/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/mvc
         http://www.springframework.org/schema/mvc/spring-mvc.xsd">

	<!-- 包扫描 -->
	<context:component-scan base-package=""></context:component-scan>

	<!-- 基于注解的处理器适配器,映射器的自动加载 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<!-- 视图解析(前后缀) -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value=""></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<!-- 文件上传 -->
	<bean id="multipartResolver" 
	class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="maxUploadSize" value="10485760"></property>
	</bean>
	
</beans>

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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SSM</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 加载spring容器配置文件 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>  
	
  <!-- 加载springmvc配置文件 -->
  	<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.xml</param-value>
  		</init-param>
  		<load-on-startup>1</load-on-startup>
  	</servlet>
  	<servlet-mapping>
  		<servlet-name>springmvc</servlet-name>
  		<url-pattern>*.action</url-pattern>
  	</servlet-mapping>
  
  <!-- 加载编码过滤器 -->
  	<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>
  
</web-app>

三.需要的jar包

还差一个logger-property包在src下
在这里插入图片描述

四.思维导图

发扬一下"学不明白,思维导图"的习惯
等我明天起来画吧

五.相关文章

都是之前自己写的,拿来参考参考
SpringMVC传送门
Spring入门传送门
Mybatis传送门

最后讲讲关于一些莫名其妙的报错,在检查所有代码都没问题的前提下,可以尝试重建项目

猜你喜欢

转载自blog.csdn.net/qq_45596525/article/details/108009759