SSM整合 spring + springmvc + mybatis 巨详细步骤,从0开始

整个项目的压缩包:
链接:https://pan.baidu.com/s/1v-nXX8XRvTNOzDXYqxQp2A
提取码:1234

spring + springmvc + mybatis

1、导包

所有包的整理下载

2、写配置
web.xml配置
spring的配置,springmvc的配置,mybatis的配置
其他小家伙配置
在这里插入图片描述
先写spring配置:
在这里插入图片描述

配置web.xml------->先配置spring容器的启动

在这里插入图片描述
在这里插入图片描述
在web.xml文件中写上类路径
在这里插入图片描述

配置web.xml------->再配置springmvc的前端控制器

在这里插入图片描述
在这里插入图片描述
配置字符编码
在这里插入图片描述

	<!-- 字符编码 -->
	<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>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>	
	</filter-mapping>

配置支持rest风格的过滤器
在这里插入图片描述

	<!-- 支持Rest风格过滤器 -->
	<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.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" id="WebApp_ID" version="3.0">
  <display-name>7.SSM</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>   
  </welcome-file-list>
  
  <!-- 配置spring容器启动 -->
<!-- needed for ContextLoaderListener -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<!-- 指定配置文件位置 -->
		<param-value>classpath:spring/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>
	
	<!-- 配置springmvc的前端控制器 -->
	<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
	<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:spring/applicationContext-mvc.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>
	
	<!-- 两个标准配置: -->
	<!-- 字符编码 -->
	<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>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>	
	</filter-mapping>
	
	<!-- 支持Rest风格过滤器 -->
	<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>

配置spring-mvc文件

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

有了上面的mvc标签之后:在这里插入图片描述

在这里插入图片描述
创建pages文件夹,下面新建一个success.jsp
在这里插入图片描述

class输入InternalResourceView就可以提示出来了在这里插入图片描述

配置文件上传解析器 在这里插入图片描述

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		
	<property name="defaultEncoding" value="utf-8"></property>
	<property name="maxUploadSize" value="#{1024*1024*20}"></property>
	</bean>

springmvc配置的所有信息

在这里插入图片描述

<?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.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">
	
	<!-- 让springmvc只扫描控制器,禁用默认的规则 -->
	<context:component-scan base-package="com.atshiyou" use-default-filters="false">		
	<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/pages/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	<!-- 配置文件上传解析器 -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		
	<property name="defaultEncoding" value="utf-8"></property>
	<property name="maxUploadSize" value="#{1024*1024*20}"></property>
	</bean>
	<!-- 扫静态资源  -->
	<mvc:default-servlet-handler/>
	<!-- 扫动态资源  -->
	<mvc:annotation-driven></mvc:annotation-driven>
</beans>

配置spring文件

在这里插入图片描述
和上面springmvc配置相似的是:
在这里插入图片描述

<!-- spring 除过控制器不要,剩下的业务逻辑组件都要,包括dao,包括service -->
	<context:component-scan base-package="com.atshiyou">
		<!-- 扫描排除不写 use -->
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>	
	</context:component-scan>

配置数据源
数据库信息单独放在一个配置文件里db.properties
在这里插入图片描述
在这里插入图片描述
配置事务控制:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

<!-- 配置事务控制;配置事务管理器,让他控制住数据源里面的连接的关闭和提交 -->
	<bean id = "tm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="ds"></property>
	</bean>

基于xml配置,配置事务;哪些方法切入事务,还要写切入点表达式
创建一个service
在这里插入图片描述

在这里插入图片描述
创建bean 类和dao接口
在这里插入图片描述
编写类和接口,为teacher增加属性,为接口增加一个方法名
在这里插入图片描述
编写TeacherDao.xml文件
在这里插入图片描述
重新编写mybatis-config.xml
在这里插入图片描述

现在mybatis需要和spring整合到一起,整合只弄最核心的部分,mybatis最核心的部分就是SQLsessionFactory,通过这个工厂获得dao映射

整合步骤

1)导入整合包(能将dao的实现加入到容器中)
mybatis-spring-1.3.0.jar

spring还有mybatis没有配置,现在配置:
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
spring配置的现在的配置为:

<?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"
	xmlns:aop="http://www.springframework.org/schema/aop"
	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.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
	
	
	<!-- spring 除过控制器不要,剩下的业务逻辑组件都要,包括dao,包括service -->
	<context:component-scan base-package="com.atshiyou">
		<!-- 扫描排除不写 use -->
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>	
	</context:component-scan>
	
	<!-- 导入外部配置文件 -->
	<context:property-placeholder location="classpath:db.properties"/>
	<!-- 配数据源 -->
	<bean id = "ds" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="driverClass" value="${jdbc.driverClass}"></property>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
		
		<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
		<property name="minPoolSize" value="${jdbc.minPoolSize}"></property>
	</bean>
	
	<!-- 配置mybatis操作数据库 -->
	<!-- 可以根据配置文件得到SQLsessionFactory -->
	<bean class="org.mybatis.spring.SqlSessionFactoryBean">
		<!--可以指定配置文件位置  -->
		<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
		<!-- 指定数据源  -->
		<property name="dataSource" ref="ds"></property>
		<!--指定xml映射文件的位置  -->
		<property name="mapperLocations" value="claspath:mybatis/mapper/*.xml"></property>
	
	</bean>
	
	
	<!-- 配置事务控制;配置事务管理器,让他控制住数据源里面的连接的关闭和提交 -->
	<bean id = "tm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="ds"></property>
	</bean>
	
	<!--基于xml配置,配置事务;哪些方法切入事务,还要写切入点表达式  -->
	<aop:config>
		<!-- 配置切入点表达式 
		* com.atshiyou.servcie.*.*(..)
		任意返回值,所有'业务'逻辑包下的都切入 ,这个包下的任意类的任意方法,任意参数
		-->
		<aop:pointcut expression="execution(* com.atshiyou.servcie.*.*(..))" id="txPoint"/>
		<aop:advisor advice-ref="myTx" pointcut-ref="txPoint"/>
	</aop:config>
	<!-- 配置事务增强、事务属性、事务建议  
	transaction-manager="tm":指定要配置的事务管理器id
	-->
	<tx:advice id="myTx" transaction-manager="tm">
	
	<!-- 配置事务属性 -->
	<tx:attributes>
		<tx:method name="*" rollback-for="java.lang.Exception"/>
		<tx:method name="get*" read-only="true" />
	</tx:attributes>
		
	
	</tx:advice>
	
	
</beans>

关键点来了,兄弟们:
先看一段mybatis的代码:
在这里插入图片描述
然后我们在spring配置中把每一个dao接口的实现加入到IOC容器中:
在这里插入图片描述

mybatis整合的关键配置:
在这里插入图片描述

spring的最终配置为:

<?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"
	xmlns:aop="http://www.springframework.org/schema/aop"
	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.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
	
	
	<!-- spring 除过控制器不要,剩下的业务逻辑组件都要,包括dao,包括service -->
	<context:component-scan base-package="com.atshiyou">
		<!-- 扫描排除不写 use -->
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>	
	</context:component-scan>
	
	<!-- 导入外部配置文件 -->
	<context:property-placeholder location="classpath:db.properties"/>
	<!-- 配数据源 -->
	<bean id = "ds" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="driverClass" value="${jdbc.driverClass}"></property>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
		
		<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
		<property name="minPoolSize" value="${jdbc.minPoolSize}"></property>
	</bean>
	
	<!-- 配置mybatis操作数据库 -->
	<!-- 可以根据配置文件得到SQLsessionFactory -->
	<bean id = "sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!--可以指定配置文件位置  -->
		<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
		<!-- 指定数据源  -->
		<property name="dataSource" ref="ds"></property>
		<!--指定xml映射文件的位置  -->
		<property name="mapperLocations" value="claspath:mybatis/mapper/*.xml"></property>	
	</bean>
	
	<!-- 我们要把每一个dao接口的实现加入到IOC容器中  -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 指定到接口所在的包 -->
		<property name="basePackage" value="com.atshiyou.dao"></property>
	
	</bean>
	
	
	
	
	
	<!-- 配置事务控制;配置事务管理器,让他控制住数据源里面的连接的关闭和提交 -->
	<bean id = "tm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="ds"></property>
	</bean>
	
	<!--基于xml配置,配置事务;哪些方法切入事务,还要写切入点表达式  -->
	<aop:config>
		<!-- 配置切入点表达式 
		* com.atshiyou.servcie.*.*(..)
		任意返回值,所有'业务'逻辑包下的都切入 ,这个包下的任意类的任意方法,任意参数
		-->
		<aop:pointcut expression="execution(* com.atshiyou.servcie.*.*(..))" id="txPoint"/>
		<aop:advisor advice-ref="myTx" pointcut-ref="txPoint"/>
	</aop:config>
	<!-- 配置事务增强、事务属性、事务建议  
	transaction-manager="tm":指定要配置的事务管理器id
	-->
	<tx:advice id="myTx" transaction-manager="tm">
	
	<!-- 配置事务属性 -->
	<tx:attributes>
		<tx:method name="*" rollback-for="java.lang.Exception"/>
		<tx:method name="get*" read-only="true" />
	</tx:attributes>
		
	
	</tx:advice>
	
	
</beans>

然后我们创建一个controller进行测试

我们在index页面写一个请求:
在这里插入图片描述
在这里插入图片描述
测试:
在这里插入图片描述
在这里插入图片描述

编写框架过程遇到的错误

在这里插入图片描述错误:
严重: 在路径为/7.SSM的上下文中,Servlet[springDispatcherServlet]的Servlet.service()引发了具有根本原因的异常Handler dispatch failed; nested exception is java.lang.AbstractMethodError: Method com/mchange/v2/c3p0/impl/NewProxyResultSet.isClosed()Z is abstract

需要升级c3p0这个包
在这里插入图片描述

在这里插入图片描述
还要安装这个包:
在这里插入图片描述
我的jdk版本是 12 的,会发生包冲突
我会把我用的包整理发出另外一篇博客中

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你在狗叫什么、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值