当前位置:网站首页>SSM整合所需配置文件及常见配置错误引起的报错
SSM整合所需配置文件及常见配置错误引起的报错
2022-06-22 05:39:00 【小薛的BUG日志】
文章目录
SSM整合的实质,仅仅就是将Mybatis整合入Spring。因为SpringMVC原本就是Spring的一部分,不用专门整合
SSM整合配置文件
1、创建Web工程或Maven-webapp模板
maven使用模板时添加archetypeCatalog=local,因为网络问题可能会失败
2、添加jar包或导入依赖
<dependencies>
<!-- spring相关 根据依赖传递特性,只需导入webmvc,所依赖的spring其他jar包会被自动导入:aop/beans/context/core/expression/web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.16</version>
</dependency>
<!--spring-jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.16</version>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--jsp-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<!--jstl-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- ServletAPI -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.8</version>
</dependency>
<!--数据源-->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<!--mybatis-spring整合-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!--文件上传和下载-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
3、配置web.xml文件
<!--上下文参数 (记录spring文件位置的)-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring_mybatis.xml</param-value>
</context-param>
<!-- 监听器(spring文件相关的监听器) -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--编码过滤器-->
<filter>
<filter-name>CharsetEncodingFilter</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>
<!--强制响应编码,默认false-->
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharsetEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--核心控制器servlet DispatcherServlet-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置SpringMVC配置文件的位置和名称-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--将前端控制器DispatcherServlet的初始化时间提前到服务器启动时-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--拦截所有会导致拦截静态资源,需要在mvc文件中设置放行-->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
4、配置spring_mybatis.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" 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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--1.关联数据库配置文件-->
<context:property-placeholder location="classpath:db.properties"/>
<!--2.配置数据源datasource-->
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--3.SqlSessionFactoryBean 会话工厂-->
<bean id="sqlsessionfactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
<!--mybatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--maven项目需要将映射文件写在resources中-->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!--4.扫描映射器对象 批量实例化Mapper接口-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zb.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlsessionfactory"/>
</bean>
</beans>
5、db.properties数据库连接信息
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
jdbc.username=root
jdbc.password=root
6、配置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/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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--扫描组件-->
<context:component-scan base-package="com.zb"/>
<!--mvc注解驱动-->
<mvc:annotation-driven/>
<!--静态资源配置-->
<mvc:resources mapping="/statics/**" location="/statics/"/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--异常处理-->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="RuntimeException">error</prop>
</props>
</property>
<property name="exceptionAttribute" value="ex"/>
</bean>
<!--文件上传-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"/>
<property name="maxUploadSize" value="-1"/>
</bean>
</beans>
7、配置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>
<settings>
<!--将下划线自动映射为驼峰,emp_name:empName-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<plugins>
<!--分页插件-->
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
<!--typeAliases:设置类型别名-->
<typeAliases>
<package name="com.zb.pojo"/>
</typeAliases>
</configuration>
附:配置错误导致的问题
1、web.xml中没配置监听器

2、web.xml中没配置上下文

3、springmvc.xml没有开启一键式配置

4、未引入spring-jdbc依赖
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘org.mybatis.spring.mapper.MapperScannerConfigurer#0’ defined in class path resource [spring_mybatis.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/dao/support/DaoSupport
Caused by: java.lang.NoClassDefFoundError: org/springframework/dao/support/DaoSupport
org.apache.catalina.core.StandardContext.startInternal One or more listeners failed to start. Full details will be found in the appropriate container log file
org.apache.catalina.core.StandardContext.startInternal Context [] startup failed due to previous errors
边栏推荐
- vscode极简安装教程
- AUTOSAR from getting started to mastering 100 lectures (150) -soa architecture and Application
- The first week of wechat applet development: page setup, page Jump and data binding
- 线性回归:最小二乘、泰尔森估计、RANSAC
- Ethernet UDP frame contract design
- MinGW download and installation
- Development forecast and investment risk outlook report of China's GaAs industry during the 14th Five Year Plan period 2022-2027
- [soft test] senior system architecture designer learning experience sharing
- Analysis of annual average temperature based on TMP data in cru
- tmux -- ssh terminal can be closed without impact the server process
猜你喜欢

402 string (Title: Sword finger offer58 ii. left rotation string, 28. implementation of strstr(), 459 Repeated substrings)

MFC TabCtrl 控件修改标签尺寸

单精度,双精度和精度(转载)

基于CRU中的tmp数据进行年平均气温分析

vscode极简安装教程

以太网UDP帧发包设计

机器学习笔记 八:Octave实现神经网络的手写数字识别

MATLAB系统辨识

402-字符串(题目:剑指Offer58-II.左旋转字符串、 28. 实现 strStr()、459.重复的子字符串)

虚职、架空、拖后腿,大厂开源办公室到底什么样?
随机推荐
open source hypervisor
Linear regression: least squares, Tellson estimation, RANSAC
idea插件Easy Code的简单使用
记录在处理SIF数据中,遇到的一些问题及解决过程
OPTEE notes
402 string (Title: Sword finger offer58 ii. left rotation string, 28. implementation of strstr(), 459 Repeated substrings)
Go language uses zap log Library
QEMU ARM interrupt system architecture 2
PID笔记
Gerrit Code Review Setup
Le contrôle MFC tabctrl modifie la taille de l'étiquette
朗国科技助力OpenHarmony生态繁荣
D3D学习笔记(1)—— SO 阶段介绍 AutoDraw使用条件
Unity 加密ASE 游戏数据
W800芯片平台进入OpenHarmony主干
以太网UDP帧发包设计
C language pointer (Advanced)
401-字符串(344. 反转字符串、541. 反转字符串II、题目:剑指Offer 05.替换空格、151. 颠倒字符串中的单词)
Understanding of C pointer
从转载阿里开源项目 Egg.js 技术文档引发的“版权纠纷”,看宽松的 MIT 许可该如何用?