SSM整合
查看不同MyBatis版本整合Spring时使用的适配包;
下载整合适配包
https://github.com/mybatis/spring/releases
官方整合示例,jpetstore
https://github.com/mybatis/jpetstore-6
1. 搭建环境
创建一个动态的Maven--WEB工程(New Maven Project--->packing war)
导入SSM需要使用的jar包依赖信息
2 Spring + Springmvc
在web.xml中配置: Springmvc的前端控制器 实例化Spring容器的监听器 字符编码过滤器 REST 过滤器
创建Spring的配置文件: applicationContext.xml:组件扫描、 连接池、 事务.....
创建Springmvc的配置文件: springmvc.xml : 组件扫描、 视图解析器 <mvc:...>
2.3 MyBatis
创建MyBatis的全局配置文件
编写实体类 Mapper接口 Mapper映射文件
2.4 Spring + MyBatis
MyBatis的 SqlSession的创建 .
MyBatis的 Mapper接口的代理实现类
整合的配置
① 导入jar包 MyBatis、MySql、log4j、spring-context、spring-webmvc、mybatis-spring(整合的jar包)、druid、jstl等
② 在web.xml中配置:
前端控制器、监听器、页面中文乱码的配置;
<!-- 配置前端控制器 -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.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>
<!-- 页面中文乱码的配置 -->
<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>
③ beans.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:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
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-4.0.xsd">
<!-- 设置自动扫描的包,不扫描handler -->
<context:component-scan base-package="com.atguigu.ssm">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 引入外部属性文件 -->
<context:property-placeholder location="classpath:druid.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="initialSize" value="${jdbc.initialSize}"></property>
<property name="minIdle" value="${jdbc.minIdle}"></property>
<property name="maxActive" value="${jdbc.maxActive}"></property>
<property name="maxWait" value="${jdbc.maxWait}"></property>
</bean>
<!-- 整合MyBatis -->
<!-- 1.配置SqlSessionFactoryBean -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置MyBatis全局配置文件路径 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 配置Mapper接口的映射文件路径 -->
<property name="mapperLocations" value="classpath:mybatis/*.xml"></property>
</bean>
<mybatis-spring:scan base-package="com.atguigu.ssm.mapper"/>
</beans>
④ springmvc.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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
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">
<!-- 设置自动扫描的包 -->
<context:component-scan base-package="com.atguigu.ssm" use-default-filters="false">
<!-- 设置只扫描包handler -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> //全类名,在handler包下面的.java文件中去取
</context:component-scan>
<!-- 配置试图解析器 -->
<bean id="" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/"></property> //pages目录下的,去拼;前缀
<property name="suffix" value=".jsp"></property> //后缀
</bean>
<!-- 配置处理静态资源, -->
<mvc:default-servlet-handler/>
<mvc:annotation-driven></mvc:annotation-driven>
</beans>