SSM整合时配置文件的编写
编写web.xml
1.配置项目初始化时启动Spring容器
传递参数,spring配置文件的位置
<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为spring配置文件,位置在src/main/resource目录下。
2.编写SpringMVC前段控制器,拦截所有请求
方式一:
在param中通过location指定springMVC配置文件的位置
<servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>location</param-value></init-param><load-on-startup>1</load-on-startup></servlet><!-- Map all requests to the DispatcherServlet for handling --><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
方式二:
去掉init-param,名字统一改为dispatcherServlet,然后在与web.xml同级目录下新建dispatcherServlet-servlet.xml
其中dispatcherServlet要与上面的servlet-name一致。
<servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- <init-param><param-name>contextConfigLocation</param-name><param-value>location</param-value></init-param> --><load-on-startup>1</load-on-startup></servlet><!-- Map all requests to the DispatcherServlet for handling --><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
3.编写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>forceRequestEncoding</param-name><param-value>true</param-value></init-param><init-param><param-name>forceResponseEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>
4.配置Rest风格的URL的过滤器
<!-- 使用Rest风格的URI,将页面普通的post请求转为指定的delete或者put请求 --><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><filter><filter-name>HttpPutFormContentFilter</filter-name><filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class></filter><filter-mapping><filter-name>HttpPutFormContentFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>
5.完整的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"><!--1、启动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><!--2、springmvc的前端控制器,拦截所有请求 --><!-- The front controller of this Spring Web application, responsible for handling all application requests --><servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- <init-param><param-name>contextConfigLocation</param-name><param-value>location</param-value></init-param> --><load-on-startup>1</load-on-startup></servlet><!-- Map all requests to the DispatcherServlet for handling --><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>/</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><init-param><param-name>forceRequestEncoding</param-name><param-value>true</param-value></init-param><init-param><param-name>forceResponseEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- 4、使用Rest风格的URI,将页面普通的post请求转为指定的delete或者put请求 --><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><filter><filter-name>HttpPutFormContentFilter</filter-name><filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class></filter><filter-mapping><filter-name>HttpPutFormContentFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>
编写springMVC的配置文件
在上面方式二新建的dispatcherServlet-servlet.xml中进行配置
主要配置网站的跳转逻辑的控制、配置视图解析器,方便页面返回等。
所以我们先在项目下新建相应的mvc结构的包。
1.配置扫描组件
<!--SpringMVC的配置文件,包含网站跳转逻辑的控制,配置 --><context:component-scan base-package="com.badao" use-default-filters="false"><!--只扫描控制器。 --><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan>
SpringMVC默认是扫描所有的,所以为了让它只扫描控制器,要加上use-default-filters="false"
2.配置视图解析器
<!--配置视图解析器,方便页面返回 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/views/"></property><property name="suffix" value=".jsp"></property></bean>
在WEB-INF目录下新建views目录,用来存放jsp页面,配置好前缀与后缀,就能解析视图,返回相应页面。
3.SpringMVC的两个标准配置
<!--两个标准配置 --><!-- 将springmvc不能处理的请求交给tomcat,这样动态与静态资源就都能访问了 --><mvc:default-servlet-handler/><!-- 能支持springmvc更高级的一些功能,JSR303校验,快捷的ajax...映射动态请求 --><mvc:annotation-driven/>
4.完整配置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.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"><!--SpringMVC的配置文件,包含网站跳转逻辑的控制,配置 --><context:component-scan base-package="com.badao" 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/views/"></property><property name="suffix" value=".jsp"></property></bean><!--两个标准配置 --><!-- 将springmvc不能处理的请求交给tomcat,这样动态与静态资源就都能访问了 --><mvc:default-servlet-handler/><!-- 能支持springmvc更高级的一些功能,JSR303校验,快捷的ajax...映射动态请求 --><mvc:annotation-driven/></beans>
编写spring的配置文件
在src/main/resource目录下新建applicationContext.xml
这里主要配置和业务逻辑有关的,比如数据源、事务控制等。
1.配置数据源
<context:property-placeholder location="classpath:dbconfig.properties" /><bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property><property name="driverClass" value="${jdbc.driverClass}"></property><property name="user" value="${jdbc.user}"></property><property name="password" value="${jdbc.password}"></property></bean>
其中location="classpath:dbconfig.properties" 就是属性文件存放的位置。
通过<context:property-placeholder location="classpath:dbconfig.properties" />就可以将属性文件引入。
2.新建属性文件dbconfig.properties
在resource下新建dbconfig.properties文件,用来存取数据源的相关属性,在applicationContext.xml中就可以通过
${}来获取属性。
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm_crud
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=123456
这里ssm_crud是建立的mysql的数据库的名字
root与123456是对应自己的连接数据库的用户名和密码
3.配置扫描业务逻辑组件
<context:component-scan base-package="com.badao"><context:exclude-filter type="annotation"expression="org.springframework.stereotype.Controller" /></context:component-scan>
4.配置和Mybatis整合
<!--================== 配置和MyBatis的整合=============== --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 指定mybatis全局配置文件的位置 --><property name="configLocation" value="classpath:mybatis-config.xml"></property><property name="dataSource" ref="pooledDataSource"></property><!-- 指定mybatis,mapper文件的位置 --><property name="mapperLocations" value="classpath:mapper/*.xml"></property></bean>
所以在resource下新建mapper目录用来存放mapper文件,在上面已经声明位置。
5.配置扫描器
将mybatis接口的实现加入到ioc容器中
<!-- 配置扫描器,将mybatis接口的实现加入到ioc容器中 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!--扫描所有dao接口的实现,加入到ioc容器中 --><property name="basePackage" value="com.badao.ssm.dao"></property></bean>
6.配置事务控制
<!-- ===============事务控制的配置 ================--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!--控制数据源 --><property name="dataSource" ref="pooledDataSource"></property></bean><!--开启基于注解的事务,使用xml配置形式的事务(必要主要的都是使用配置式) --><aop:config><!-- 切入点表达式 此包下的所有类的所有方法都切入事务--><aop:pointcut expression="execution(* com.badao.ssm.service..*(..))" id="txPoint"/><!-- 配置事务增强 --><aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/></aop:config><!--配置事务增强,事务如何切入 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><!-- 所有方法都是事务方法 --><tx:method name="*"/><!--以get开始的所有方法 --><tx:method name="get*" read-only="true"/></tx:attributes></tx:advice>
7.完整的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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"><context:component-scan base-package="com.badao"><context:exclude-filter type="annotation"expression="org.springframework.stereotype.Controller" /></context:component-scan><!-- Spring的配置文件,这里主要配置和业务逻辑有关的 --><!--=================== 数据源,事务控制,xxx ================--><context:property-placeholder location="classpath:dbconfig.properties" /><bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property><property name="driverClass" value="${jdbc.driverClass}"></property><property name="user" value="${jdbc.user}"></property><property name="password" value="${jdbc.password}"></property></bean><!--================== 配置和MyBatis的整合=============== --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 指定mybatis全局配置文件的位置 --><property name="configLocation" value="classpath:mybatis-config.xml"></property><property name="dataSource" ref="pooledDataSource"></property><!-- 指定mybatis,mapper文件的位置 --><property name="mapperLocations" value="classpath:mapper/*.xml"></property></bean><!-- 配置扫描器,将mybatis接口的实现加入到ioc容器中 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!--扫描所有dao接口的实现,加入到ioc容器中 --><property name="basePackage" value="com.badao.ssm.dao"></property></bean><!-- 配置一个可以执行批量的sqlSession --><bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"><constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg><constructor-arg name="executorType" value="BATCH"></constructor-arg></bean><!--============================================= --><!-- ===============事务控制的配置 ================--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!--控制数据源 --><property name="dataSource" ref="pooledDataSource"></property></bean><!--开启基于注解的事务,使用xml配置形式的事务(必要主要的都是使用配置式) --><aop:config><!-- 切入点表达式 此包下的所有类的所有方法都切入事务--><aop:pointcut expression="execution(* com.badao.ssm.service..*(..))" id="txPoint"/><!-- 配置事务增强 --><aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/></aop:config><!--配置事务增强,事务如何切入 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><!-- 所有方法都是事务方法 --><tx:method name="*"/><!--以get开始的所有方法 --><tx:method name="get*" read-only="true"/></tx:attributes></tx:advice></beans>
编写MyBatis的配置文件
在resource下新建mybatis-config.xml,作为全局配置文件
1.开启驼峰命名规则
<settings><setting name="mapUnderscoreToCamelCase" value="true"/></settings>
2.开启别名
<typeAliases><package name="com.badao.ssm.bean"/></typeAliases>
3.完整mybatis-config.xml文件代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><settings><setting name="mapUnderscoreToCamelCase" value="true"/></settings><typeAliases><package name="com.badao.ssm.bean"/></typeAliases></configuration>
代码下载
本阶段示例代码下载
https://download.csdn.net/download/badao_liumang_qizhi/10857091
SSM整合时配置文件的编写相关推荐
- SSM整合时Maven项目的pom.xml版本兼容的代码备份
场景 jdk:1.8 Tomcat:7.0 本地mysql:8.0 Eclipse:Eclipse Jee Photon Spring:4.3.8 AOP:1.8.0 mybatis:3.3.0 My ...
- SSM整合(配置文件)
Web.XML <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="htt ...
- SSM整合时出错:Error creating bean with name ‘studentService‘ defined in file [/Users...
出现这个错误的原因是spring在整合junit单元测试时,导入坐标错误导致的. 错误信息: /Library/Java/JavaVirtualMachines/jdk1.8.0_331.jdk/Co ...
- SSM整合时IDE: File is included in 4 contexts
问题描述: 问题所在: 在spring-dao.xml,spring-service.xml,spring-web.xml没有放在同一个上下文中时 spring-service.xml中dataSou ...
- ssm整合开发配置文件
配置的具体解释可以参考米米商城项目实战 ssm框架部分 pom.xml <?xml version="1.0" encoding="UTF-8"?> ...
- 记第一次ssm整合的配置文件
applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xm ...
- springMVC02-SSM整合(Result统一响应数据格式、异常页面修改、SSM整合vue-elementUI小案例、SpringMVC的拦截器Interceptor)
文章目录 今日内容 一.SSM整合[重点] 1 SSM整合配置 问题导入 1.1 SSM整合流程 1.2 SSM整合配置 1.2.1 创建工程,添加依赖和插件 1.2.2 Spring整合Mybati ...
- SSM框架学习文档以及SSM整合(附Github地址=含SSM学习时的实例代码)
SSM框架学习 软件架构: 基于流行SSM框架:Spring+SpringMVC+Mybatis 项目配置: 使用Maven进行项目jar导入 使用Git进行版本控制,并将每次编写的代码上传到Gi ...
- SpringMVC以及SSM整合
本人才疏学浅,如有错误欢迎批评!转载请注明出处:https://www.cnblogs.com/lee-yangyaozi/p/11226145.html SpringMVC概述 Spring Web ...
最新文章
- Leangoo敏捷工具如何升级至企业版
- WebApi跨域的解决方法
- ROS学习(八):ROS URDF-transmission
- 【自动驾驶】7. MDC常用术语、DDS、SOME/IP
- [科技]Loj#6564-最长公共子序列【bitset】
- css写七步诗,兄弟情谊的优美句子
- C语言随笔小算法:创建双向链表
- 关于“未能打开文稿 文本编码Unicode(UTF-8)不适用”的解决办法
- 全面超越Swin Transformer | Facebook用ResNet思想升级MViT
- 禁用app里面的java_java – 我们可以禁用AOP调用吗?
- mysql explain G_MySQL 性能优化神器 Explain 使用分析
- iptv组播和单播的区别
- 7-46 新浪微博热门话题 (30 分)
- 总结常用损失函数的基本形式、原理及特点
- 2021年低压电工免费试题及低压电工考试技巧
- 视频类网站的简单研究
- 348、弱电工程FTTH光纤入户施工全过程讲解,看完这一篇就够了
- 基于RNN的短期股票预测
- pytorch dali 加速 dali支持的数据处理列表,mxnet tensorflow caff读取数据转换 pytorch训练
- python 批量编译pyc文件
热门文章
- SpringBoot的全局异常处理的优雅吃法!要进来学习下吗
- 带你读懂Spring Bean 的生命周期,嘿,就是玩儿~
- JVM中垃圾回收相关算法 - 值得了解一下的,因为早晚得了解
- groovy定义变量获取当前时间_Groovy - 比较日期和时间
- java calendar字符串显示_Java获取当前时间年月日、时间格式化打印、字符串转日期...
- v-viewer图片打不开一直在刷新_WordPress 上传图片时 async-upload.php出现520 Bug的原因及解决方案...
- python3爬取网易云歌单数据清洗_网页抓取网易云音乐及评论数据分析
- 树梅派kali界面_使用树莓派和kali Linux打造便携式渗透套件
- python格式化字符_Python格式化字符 %s %d %f
- 注意力机制中的Q、K和V的意义