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需要的包 ...
随机推荐
- Paper: A Novel Time Series Forecasting Method Based on Fuzzy Visibility Graph
Problem define a fuzzy visibility graph (undirected weighted graph), then give a new similarity meas ...
- ps怎么把一种颜色变成另一种颜色
使用软件:PS CC版 使用Photoshop 将图片中的一种颜色变成另一种颜色的两种方法: 1.打开图片,Ctrl+J复制一份: 2.执行菜单-选择-色彩范围,使用吸管和吸管+选取图片颜色部分,点确 ...
- 【模板】裸SPFA
SPFA可以处理带负边权的图,可以判负环,然而SPFA容易被卡,即使加了各种优化. 队列优化的贝尔福德曼:裸SPFA //SPFA #include<bits/stdc++.h> usin ...
- unity命令行参数
Typically, Unity will be launched by double-clicking its icon from the desktop but it is also possib ...
- HTML连载61-焦点图、固定定位
一.焦点图 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- Redis Distributed lock
using StackExchange.Redis; using System; using System.Collections.Generic; using System.Linq; using ...
- AcWing 874. 筛法求欧拉函数
#include<bits/stdc++.h> using namespace std; typedef long long ll; ; int primes[N],cnt; int ph ...
- [git] git合并冲突 本地已有项目上传 各种问题
git... 讲道理 我现在能隐约感觉到他的强大控制能力了 但是依旧是不习惯.... 无论是什么操作 在我这里都会出问题,,,, 上传本地已有的项目到码云 首先需要现在码云创建一个仓库 然后用git工 ...
- golang中的net/rpc包
本文先介绍RPC,然后go原生对RPC的使用,之后是介绍go语言中有哪些RPC框架以及一些其他常见的框架,最后是探究go语言中rpc的源码. (1)首先介绍下什么RPC? (2)RPC可以做什么? ( ...
- 512,a标签的target属性
<a> 标签的 target 属性规定在何处打开链接文档. 1.“_blank”的意思: 浏览器总在一个新打开.未命名的窗口中载入目标文档. 2.“_parent”的意思: 这个目标,使得 ...