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需要的包 ...
随机推荐
- CenterOS下 Mysql数据库中数据字符乱码
1.修改数据库字符编码 mysql> alter database mydb character set utf8 ; 2.创建数据库时,指定数据库的字符编码 mysql> create ...
- Mesh R-CNN 论文翻译(实验部分)
本文为 Mesh R-CNN 论文翻译(原理部分)的后续.Mesh R-CNN 原论文. 4 实验 我们在ShapeNet上对网格预测分支进行基准测试,并与最先进的方法相比较.然后,我们在野生的有 ...
- 数据从mmsql导入mysql
SQL SERVER数据导入MYSQL 工具: navicat for mysql 百度搜一个安装就可以,前提是你已经安装了mysql 1,创建目标数据库 点击创建好的目标数据库website点的表一 ...
- AcWing 1020. 潜水员 二维费用背包
//体积最多是j 全部为0,v>=0 //体积恰好为j f[0][0]=0,f[i]=无穷,v>=0 //体积至少是j f[0][0]=0,f[i]=无穷,体积为负数时于0取大 #incl ...
- Java中new一个子类对象的同时并不会自动创建一个父类对象
首先重申一个概念:子类会继承父类所有非私有成员变量和方法,包括父类的构造方法 当创建一个子类对象时,首先开辟内存,然后调用类的构造函数,这里的构造函数由两部分组成,一部分是从父类继承而来的父类的构造方 ...
- Codeforces Round #570 (Div. 3) B. Equalize Prices
原文链接https://codeforces.com/contest/1183/problem/B 题意:进行Q组测试,在每组中有长度为n的数组a[i],然后现在给你一个K,问你找到一个bi使得|ai ...
- 1-Django2.2安装指南
django快速安装指南 作为一个Python Web框架,Django需要Python环境.下面是Django需要对应的python版本. Django版本 python版本 1.11 2.7, 3 ...
- 记manjaro图形驱动删除后的一次补救
#一.前言 众所周知,NVIDIA的闭源驱动在Linux上的兼容性不是很好,再加上我不玩游戏,于是我就想卸载独显只留核显.我以为我装了独显和核显两种驱动,原本想直接删除独显驱动,没想到删除的是bumb ...
- appium---webview(H5)元素定位
我们在做UI自动化的时候,肯定需要元素定位,那么webview(H5)的元素怎么定位呢? webview定位方法 方法一: 1.打开chrome浏览器,输入chrome://inspect 2.连接手 ...
- array.find()方法
//array.find(function(currentValue, index, arr),thisValue) 方法说明 let tempArray = [ {"key":1 ...