ssm框架整合,配置文件中的配置内容
转自:https://www.cnblogs.com/dong-dong-1/p/8724127.html
使用idea工具开发,用maven进行管理。
最近在写毕业设计,因为对ssm框架一直半解,常常导致出来问题不知道去哪里找,或者读懂了bug却解决不了问题的尴尬局面。今天就重新去理解一下ssm的配置文件,以及之间的相互作用。
先说一下有几个配置文件。
resources--applicationContext.xml : spring容器的配置文件,实现框架整合
--db.properties : 数据库常量配置(无难度)
--log4j.properties :日志信息的存放(开发时为debug,上线改为info)
--springmvc.xml :springMVC的配置文件。
-- sqlMapConfig.xml :mybats的配置文件,整合后 几乎只剩下 《别名》
WEBINF -- web.xml :让配置文件随着项目的启动而加载
1. applicationContext.xml 配置文件说明
①添加约束条件
②开启注解扫描(除了控制器,其他都扫描)
扫描方式有两种:
<context:annotation-config>:注解扫描是针对已经在Spring容器里注册过的Bean
<context:component-scan>:不仅具备<context:annotation-config>的所有功能,还可以在指定的package下面扫描对应的bean
用法:
<!--自动扫描包-->
<context:component-scan base-package="cn.hfbin.crud">
<!--除了描控制器不扫描外 其它全部扫描-->
<context:exclude-filter type="annotation"expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
③加载jdbc.properties文件,并且配置数据源
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
④配置sqlSessionFactory (Spring和Mybatis整合)
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 指定mybatis配置文件的位置 -->
<property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
</bean>
⑤事务控制的配置
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--控制住数据源 -->
<property name="dataSource" ref="DataSource"></property>
</bean><!--开启基于注解的事务,使用xml配置形式的事务(必要主要的都是使用配置式) -->
<aop:config>
<!-- 切入点表达式 -->
<aop:pointcut expression="execution(* cn.hfbin.crud.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> 2. springmvc.xml 配置文件说明
① 扫描controller
<!-- SpringMVC 的配置文件 ,包含网站跳转的逻辑控制器 -->
<context:component-scan base-package="cn.hfbin" use-default-filters="false">
<!-- 只扫描控制器 -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
② 注解驱动
<!-- 能支持springmvc更高级的一些功能:JSR303校验 ,快捷的ajax ,映射动态请求-->
<mvc:annotation-driven/> ③视图解析器<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
3. sqlMapConfig.xml 配置
① 别名设置(2种方法) (扫描的是pojo的类) 单个配置:<typeAliases> <typeAlias type="cn.edu.hpu.ssm.po.User" alias="user"/> </typeAliases>
批量设置(推荐)
<typeAliases> <package name="cn.edu.hpu.ssm.po"/> </typeAliases> ②扫描mapper
<mappers>
<mapper resource="sqlmap/User.xml"/>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
这里要注意的是:mapper也可以在applicationContext中扫描。
在applicationContext配置如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<!-- 配置SqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 加载mybatis的配置文件--> <property name="configLocation" value="classpath:config/mybatis/SqlMapConfig.xml"/> <!-- 数据源--> <property name="dataSource" ref="dataSource"/> <!-- mapper.xml扫描--> <property name="mapperLocations" value="classpath:config/mapper/*.xml"/> </bean> <!-- mapper扫描器--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <property name="basePackage" value="com.youye.mapper"/> </bean> |
在配置sqlSessionFactory时,配置mapperLocations属性,用来加载mapper.xml文件,mapper扫描器配置则如上所示。
mapper扫描器中的两个property的顺序不能交换,即SQLSessionFactoryBeanName必须在前。
4. web.xml 配置
这个比较简单,基本上大家的推荐都是一样的。

<!-- 启动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> <!-- 启动springmvc的前端控制器 ,拦截所有请求 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet> <!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springmvc</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>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> <!-- 使用Rest风格的URL -->
<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>
注意(这里加入了支持Rest风格的URL 的配置):
<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>
ssm框架整合,配置文件中的配置内容的更多相关文章
- ssm框架整合配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- ssm 框架整合 代码初步 maven配置
pom.xml 配置<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <de ...
- springmvc(二) ssm框架整合的各种配置
ssm:springmvc.spring.mybatis这三个框架的整合,有耐心一步步走. --WH 一.SSM框架整合 1.1.整合思路 从底层整合起,也就是先整合mybatis与spring,然后 ...
- JAVAEE——宜立方商城01:电商行业的背景、商城系统架构、后台工程搭建、SSM框架整合
1. 学习计划 第一天: 1.电商行业的背景. 2.宜立方商城的系统架构 a) 功能介绍 b) 架构讲解 3.工程搭建-后台工程 a) 使用maven搭建工程 b) 使用maven的tomcat插件启 ...
- 转 SSM框架整合to萌新
作用: SSM框架是spring MVC ,spring和mybatis框架的整合,是标准的MVC模式,将整个系统划分为表现层,controller层,service层,DAO层四层 使用spring ...
- SpringMVC札集(10)——SSM框架整合
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
- SSM框架——整合搭建流程
1.首先创建maven工程,使用哪种方式进行创建都可以,可以参考博主之前的文章: <两种方式创建Maven项目[方式二]><两种方式创建Maven项目[方式一]> 2.先看看搭 ...
- (转)淘淘商城系列——SSM框架整合之Dao层整合
http://blog.csdn.net/yerenyuan_pku/article/details/72721093 一个项目中往往有三层即Dao层.Service层和Web层,看标题就知道了,本文 ...
- SSM框架整合模板
SSM框架整合--MAVEN依赖 spring方面(包含了springmvc): spring-webmvc:spring与mvc的整合依赖,主要包括spring的核心包和springmvc需要的包 ...
随机推荐
- 【做题笔记】洛谷P1506 拯救oibh总部
跑一遍染色法,最后判断哪些位置没被染色即可 一些技巧: 为了判断方便,把字符转换成 int 型的数字. 注意边界问题 详细解释见代码 #include <iostream> #includ ...
- Docker学习笔记三
4.Docker数据管理 4.1.数据卷 数据卷是一个可供容器使用的特殊目录,它将主机操作系统目录直接映射进容器,类似与Linux的mount行为. 特性: 数据卷可以在容器之间共事和重用,容器间传递 ...
- leetcode腾讯精选练习之相交链表(六)
相交链表 题目: 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4,5 ...
- 支持CSS3的高级CSS
一.匹配部分字符串 在CSS2中,可以使用[ href='info.htm' ] 这样的属性选择器来匹配href属性中的字符,但是无法只匹配部分字符串.而CSS3给出了三个新的运算符:^ , $ 和 ...
- 后端——框架——缓存框架——memcached——《Memcached教程》阅读笔记
Memcached的知识点大致可以分为三个部分. 服务器部分:环境搭建. 概念:存储的数据类型,指令,内存的替换策略. 集成:与Java语言的集成. 1.搭建环境 1.1 Linux环境 在Linux ...
- 2019牛客多校第七场E Find the median 离散化+线段树维护区间段
Find the median 题意 刚开始集合为空,有n次操作,每次操作往集合里面插入[L[i],R[i]]的值,问每次操作后中位数是多少 分析 由于n比较大,并且数可以达到1e9,我们无法通过权值 ...
- .htaccess详解
http://www.cnblogs.com/adforce/archive/2012/11/23/2784664.html .htaccess是什么 .htaccess文件(或者"分布式配 ...
- 记录 shell学习过程(9)正则表达式 转自树明聊运维
正则表达式 正则表达式介绍 特殊字符 POSIX特殊字符 一.正则表达式介绍 正则表达式是一种文本模式匹配,包括普通字符(例如,a 到 z 之间的字母)和特殊字符(称为"元字符") ...
- 点击事件后根据url保持相应导航高亮
直接上代码吧!!! HTML页面: <div class="nav"> <ul class="sub-tit"> <li>& ...
- CentOS7下使用C/C++连接MariaDB/MySQL
前言 连接数据库通常在Java中使用比较多,但是C/C++在Linux下操作数据库也是比较重要的,很多时候都能用得到,在网上查了很多教程,大多写的有些问题,通过自己摸索,终于成功的连接了MariaDB ...