SSM基础整合(spring-springMVC-mybatis)
来源:互联网 发布:淘宝手办散货 编辑:程序博客网 时间:2024/06/05 22:52
ssm整合配置流程
1.配置web.xml
- 配置spring核心配置文件
- spring监听器的加载
- 配置编码过滤器,解决POST乱码问题
- 配置SpringMVC核心配置文件的路径
- 坐标
<?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="MyWebApp" version="2.5"><display-name>ssm-usermanage</display-name><!-- 初始化参数,配置Spring核心配置文件位置 初始化所有以applicationContext开头的xml --><context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext*.xml</param-value></context-param><!-- ContextLoaderListener监听器的作用就是启动Web容器时,自动装配ApplicationContext的配置信息 --><listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 编码过滤器,以UTF8编码,解决POST乱码问题 --><filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF8</param-value> </init-param></filter><filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern></filter-mapping><!-- 配置SpringMVC --><servlet> <servlet-name>usermanage</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 初始化参数,指定SpringMVC的配置文件路径,不再找WEB-INF下的默认配置 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/ssm-usermanage-servlet.xml</param-value> </init-param> <!-- 0:第一次被调用时创建; 1:Tomcat启动时就创建 --> <load-on-startup>1</load-on-startup></servlet><servlet-mapping> <servlet-name>usermanage</servlet-name> <!-- 拦截除了JSP以外的所有路径,不用再写.do了 --> <url-pattern>/</url-pattern></servlet-mapping><welcome-file-list> <welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>
2.配置applicationContext.xml spring的核心
- 配置Spring的注解扫描@Service
- 加载jdbc的资源文件
- 配置c3p0连接池
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><!-- 配置注解扫描器 @Service --><context:component-scan base-package="cn.demo.usermanage.service"/><!-- 加载资源文件 --><bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <!-- 配置资源文件 --> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property></bean><!-- 配置连接池,数据源 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${mysql.driver}"/> <property name="jdbcUrl" value="${mysql.url}"/> <property name="user" value="${mysql.user}"/> <property name="password" value="${mysql.password}"/></bean></beans>
3.配置applicationContext-tx.xml 事务管理
- 定义事务管理器
- 配置事务管理策略
aop切面配置
4.applicationContext-mybatis.xml 用来加载mybatis配置的
- SqlSession工厂mapper的接口配置
- 加载mybatis全局配置文件
- 配置mapper的扫描,扫描所有mapper.xml映射文件
- 配置类型别名
配置扫描所有的Dao接口所在路径
#
5.ssm-usermanage-servlet.xml springMVC的核心
- 配置springMVC的注解驱动
- 开启springMVC的注解扫描
配置试图解析器
#
6.配置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> <!-- 开启驼峰匹配 --> <setting name="mapUnderscoreToCamelCase" value="true" /></settings><!-- 分页助手 --><plugins> <!-- com.github.pagehelper为PageHelper类所在包名 --> <plugin interceptor="com.github.pagehelper.PageHelper"> <!-- 数据库方言 --> <property name="dialect" value="mysql" /> <!-- 设置为true时,使用RowBounds分页会进行count查询 会去查询出总数 --> <property name="rowBoundsWithCount" value="true" /> </plugin> <!-- 通用Mapper的插件 --> <plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor"> <!--主键自增回写方法,默认值MYSQL --> <property name="IDENTITY" value="MYSQL" /> <!--通用Mapper默认接口,我们定义的Mapper需要实现该接口 --> <property name="mappers" value="com.github.abel533.mapper.Mapper" /> </plugin></plugins></configuration>
7.mapper.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!-- namespace 理论上随意命名 因为mybatis动态代理的使用 书写时,要讲namespace指定为接口的全路径 --><mapper namespace="abc"></mapper>
8.所有配置的加载顺序及联系
1.web.xml配置会去加载所有的applicationContext开头的xml文件;
2.首先加载Spring的核心配置applicationContext.xml,jdbc数据源被初始化,存放于Spring容器中;
3.加载applicationContext-tx.xml,在定义事务中注入了spring核心初始化的数据源,设置事务,事务初始化完毕;
4.加载mybatis.xml.初始化SqlSessionFactory,再次注入Spring核心初始化的数据源.在工厂配置中,去加载mybatis核心和mapper.xml,定义了类型的别名.mybatis初始化完毕;
5.最后加载ssm-usermanage-servlet.xml.springMVC扫描注解驱动,和注解扫描
9.为什么不在同一个配置中开启spring注解扫描?
springMVC是spring的子容器,不处于同一个容器.如果在applicationContext.xml(父容器中)开启,那么注解就只会扫描父容器的,如果在ssm-manager-config.xml中开启,那么注解就只会扫描子容器的;子容器可以访问父容器,父容器是没有权限访问子容器的,就是Controller可以注入servlet层的bean,但是在service中是不允许注入Controllerbean的
阅读全文
0 0
- SSM基础整合(spring-springMVC-mybatis)
- SSM(SpringMVC+Spring+Mybatis)整合
- springmvc+mybatis+spring 整合SSM
- 【SSM】Spring+SpringMVC+Mybatis整合
- SSM整合(Spring+SpringMVC+Mybatis)
- SSM框架整合( Spring 、 SpringMVC 和 Mybatis )
- SSM(Spring+SpringMVC+MyBatis)整合
- SSM整合配置文件(Springmvc+Spring+Mybatis)
- SSM(springmvc+spring+mybatis)整合过程
- spring、springmvc、mybatis整合(SSM)
- SSM(Spring,SpringMVC,MyBatis)整合开发流程
- SSM(spring+mybatis+springmvc)整合 配置文件
- SSM整合配置文件(Springmvc+Spring+Mybatis)
- SSM整合配置文件(Springmvc+Spring+Mybatis)
- SSM整合(SpringMVC+Spring+Mybatis)
- Spring+SpringMVC+Mybatis+Maven+Eclipse整合--SSM
- SSM框架整合( Spring 、 SpringMVC 和 Mybatis )
- Spring+SpringMVC+MyBatis(SSM)框架整合
- ViewPager中复用View导致在某些自定义动画下会有问题的bug。
- 由《网易面试OC多线程题目》引发后续的思考
- JS 一定要放在 Body 的最底部么?聊聊浏览器的渲染机制
- Android 设计素材积累(九层之台起于垒土)
- linux ca-certificates维护openssl证书
- SSM基础整合(spring-springMVC-mybatis)
- 文章标题
- MAVEN新建项目中index.jsp报错
- kotlin学习笔记
- POJ2763-Housewife Wind
- django 与 百度 ueditor 富文本编辑器集成之一:图片上传,文件上传
- 图论-强连通分量 poj-2186
- 第3周实践项目4 -顺序表的应用 删除顺序表中元素为x的值
- Scoket编程