SSM整合步骤
发布时间
阅读量:
阅读量
Spring + SpringMVC + MyBatis 整合
首先Spring mvc是Spring框架的子模块,是基于Spring功能之上的Web框架,所以整合只是Spring以及SpringMVC与Mybatis的整合。(两类整合所需的jar包)
1.创建一个web项目 (Dynamic Web Project)
会产生web.xml配置文件(WEB-INF目录下创建web.xml文件
在web.xml其中配置:
web.xml
实例化ApplicationContext容器(Spring配置文件)并启动Spring容器
配置DispatcherServlet(SpringMVC配置文件)
配置字符编码过滤器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!-- 实例化ApplicationContext容器 -->
<context-param>
<!-- 加载src目录下的applicationContext.xml文件 -->
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<!-- 指定以ContextLoaderListener方式启动Spring容器 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!--配置DispatcherServlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<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>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
2.Spring mvc配置文件:配置控制器扫描包和视图解析器
在
WEB-INF目录下创建:springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 使用扫描机制,扫描控制器类 -->
<context:component-scan base-package="controller"/>
<context:component-scan base-package="service"/>
<mvc:annotation-driven />
<!-- annotation-driven用于简化开发的配置,
注解DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->
<!-- 使用resources过滤掉不需要dispatcher servlet的资源。
使用resources时,必须使用annotation-driven,不然resources元素会阻止任意控制器被调用。
如果不使用resources,则annotation-driven可以没有。 -->
<!-- 允许css目录下所有文件可见 -->
<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
<!-- 允许html目录下所有文件可见 -->
<mvc:resources location="/html/" mapping="/html/**"></mvc:resources>
<!--允许images目录下所有文件可见 -->
<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
3.配置Spring配置文件applicationContext.xml
在src目录下创建applicationContext.xml:配置数据源、添加事务支持、开启事务注解、配置Mybatis工厂、Mapper代理开发以及扫描包。
applicationContext.xml
- 配置数据源 :BasicDataSource(driverClassName,url,username,password,maxTotal,maxIdle,initialSize)
- 事务支持:DataSourceTransactionManager(dataSource),tx:annotation-driven
- 配置MyBatis工厂:SqlSessionFactoryBean(dataSource,configLocation)
- Mapper代理开发: MapperScannerConfigurer (basePackage,sqlSessionFactoryBeanName)
- 扫描包:context:component-scan
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/springtest?characterEncoding=utf8" />
<property name="username" value="root" />
<property name="password" value="root" />
<!-- 最大连接数 -->
<property name="maxTotal" value="30"/>
<!-- 最大空闲连接数 -->
<property name="maxIdle" value="10"/>
<!-- 初始化连接数 -->
<property name="initialSize" value="5"/>
</bean>
<!-- 添加事务支持 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 开启事务注解-->
<tx:annotation-driven transaction-manager="txManager" />
<!-- 配置MyBatis工厂,同时指定数据源,并与MyBatis完美整合 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- configLocation的属性值为MyBatis的核心配置文件 -->
<property name="configLocation" value="classpath:com/mybatis/mybatis-config.xml"/>
</bean>
<!--Mapper代理开发,使用Spring自动扫描MyBatis的接口并装配
(Spring将指定包中所有被@Mapper注解标注的接口自动装配为MyBatis的映射接口) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- mybatis-spring组件的扫描器 -->
<property name="basePackage" value="com.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!-- 指定需要扫描的包(包括子包),使注解生效。dao包在mybatis-spring组件中已经扫描,这里不再需要扫描-->
<context:component-scan base-package="com.service"/>
</beans>
4.配置Mybatis配置文件mybatis-config.xml
在src目录下创建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>
<!-- 将mybatis的执行过程显示在控制台 -->
<setting name="logImpl" value="STDOUT_LOGGING" />
<!--true: 把所有级联查询都执行 -->
<setting name="aggressiveLazyLoading" value="false" />
<!-- 在用到级联属性的时候才会查询 -->
<setting name="lazyLoadingEnabled" value="true" />
<!-- 将下划线格式转换为驼峰格式: table:user_name java: userName -->
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
<typeAliases>
<package name="cn.qst.model" />
</typeAliases>
<mappers><!-- 映射器,告诉 MyBatis到哪里去找映射文件-->
<mapper resource="com/mybatis/UserMapper.xml"/>
</mappers>
</configuration>
ssm请求驱动模型

dao层就是mapper接口,记得写其dao对应的Mapper.xml映射文件并在配置文件中配置。
全部评论 (0)
还没有任何评论哟~
