您的位置:首页 > 编程语言 > Java开发

SSM基础整合(spring-springMVC-mybatis)

2017-09-14 14:48 459 查看

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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://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.xsd http://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的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: