SSM整合

一、整合SSM的大致步骤如下

  • 导入对应的jar包
    spring:
    springMVC:
    mybatis:
    第三方支持:log4j,pageHelper,AspectJ,jackson,jstl
  • 搭建SpringMVC,配置web.xml以及SpringMVC.xml
  1. web.xml:
    DispatcherServlet
    HiddenHttpMethodFilter
    CharacterEncodingFilter
  2. springMVC.xml:
    扫描控制层组件
    视图解析器
    Default Servlet
    MVC驱动
    可选:MultipartResolver,拦截器
  • 整合spring和SpringMVC,配置web.xml以及spring
  1. web.xml:
    ContextLoaderListener
    context-param
  2. spring.xml:
    扫描组件(排除控制层)
  • 搭建mybatis:配置核心文件、mapper接口以及mapper映射文件
  • spring整合mybatis,在spring.xml中添加如下配置:
    properties文件的引入
    DataSource数据源的配置
    事务管理器
    开启事务驱动
    SqlSessionFactoryBean:管理SqlSession
    MapperScannerConfigurer

二、具体代码实现如下:
1) 对应的Jar包如图所示:
在这里插入图片描述
2)搭建SpringMVC
①先配置web.xml文件,如上所示我们要配置:编码过滤器CharacterEncodingFilter、以及前端核心控制器DispatcherServlet.
注意:如果要使用REST风格,我们还要加入HiddenHttpMethodFilter过滤器

<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>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
	<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>
  <servlet-mapping>
    <servlet-name>springDispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

注意:这里的classpath:springMVC.xml是为了获取springMVC.xml文件,同时springMVC.xml文件可以放在类路径下,具体可以看我的关于springMVC的文章。

<!-- REST请求方式处理 -->
    <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>

注意:如果你不用REST风格,可以不用配置此标签。
②配置springMVC.xml文件,文件放在如下位置:
在这里插入图片描述

在这里我们只需要配置:扫描控制层组件、 视图解析器、Default Servlet、 MVC驱动

<?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.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.controller"></context:component-scan>
	
	<!--配置视图解析器  -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/view/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<!--默认的servlet -->
	<mvc:default-servlet-handler/>
	
	<!--  设置MVC驱动 -->
	<mvc:annotation-driven />
</beans>

3) 整合spring和springMVC
① 在web.xml文件中添加如下代码:

<!-- 自定义spring配置文件的位置和名称 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

注意:ContextLoaderListener监听器的作用就是启动Web容器时,自动装配spring.xml的配置信息.
Spring提供ServletContentListener的一个实现类ContextLoaderListener监听器,该类可以作为Listener使用,在启动Tomcat容器的时候,该类的作用就是自动装载ApplicationContext的配置信息,如果没有设置contextConfigLocation的初始参数,则会使用默认参数WEB-INF路径下的application.xml文件。如果需要自定义读取多个配置文件或者修改默认路径,则可以在web.xml,使用context-param自定义路径。
ContextLoaderListener会读取这些XML文件并产生 WebApplicationContext对象,然后将这个对象放置在ServletContext的属性里,这样我们只要可以得到Servlet就可以得到WebApplicationContext对象,并利用这个对象访问spring容器管理的bean。

② 在conf目录下创建并且配spring.xml文件

<!-- 扫描除了controller注解以外的组件 -->
 <context:component-scan base-package="com.atguigu.ssm">
 	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
 </context:component-scan>

4)搭建mybatis
①:创建核心配置文件步骤

  • 创建 jdbc.properties的外部资源文件
    在这里插入图片描述
  • 在这里插入图片描述
    最基本核心的配置代码如下:就是数据库环境的配置以及mapper映射器
    注意:transactionManager、和dataSource都必须有
<?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>
<properties resource="jdbc.properties"></properties>

<environments default="development">
	<environment id="development">
		<transactionManager type="JDBC"></transactionManager>
		<dataSource type="POOLED">
		<property name="driver" value="${jdbc.driver}"/>
		<property name="url" value="{jdbc.url}"/>
		<property name="username" value="{jdbc.username}"/>
		<property name="password" value="{jdbc.password}"/>
		</dataSource>
	</environment>
</environments>

<mappers>
<package name="com.atguigu.ssm.mapper"/>
</mappers>
</configuration>
</beans>

一些其他的setting配置如下:放在configuration标签中

<settings>
		<!-- 将下划线映射成驼峰,user_name映射为userName -->
		<setting name="mapUnderscoreToCamelCase" value="true"/>
		<!-- 开启延迟加载 -->
		<setting name="lazyLoadingEnabled" value="true"/>
		<!-- 是否查询所有数据 -->
		<setting name="aggressiveLazyLoading" value="false"/>
		<!-- 是否开启二级缓存 -->
		<setting name="cacheEnabled" value="true"/>
	</settings>
	<plugins>
		<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
	</plugins>

mapper映射文件格式如下:

<?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">

<mapper namespace="com.atguigu.ssm.mapper.EmployeeMapper">
	<!-- public List<Employee> getAllEmps(); -->
	<select id="getAllEmps" resultMap="myEmpsAndDept" >
		select e.id eid, e.last_name,e.email,e.gender, d.id did, d.dept_name
		from tbl_employee e ,tbl_dept d 
		where e.d_id = d.id 
	</select>
	<resultMap type="com.atguigu.ssm.beans.Employee" id="myEmpsAndDept">
		<id column="eid" property="id"/>
		<result column="last_name" property="lastName"/>
		<result column="email" property="email"/>
		<result column="gender" property="gender"/>
		
		<association property="dept" javaType="com.atguigu.ssm.beans.Department">
			<id column="did" property="id"/>
			<result column="dept_name" property="departmentName"/>
		</association>
	</resultMap>
</mapper>

5)spring整合mybatis:实质上就是将mybatis-config文件的一些配置,放在spring容器中去管理

  • 在spring.xml文件中添加如下代码:
<!-- spring整合Mybatis -->
 <!--  数据源 -->
 <context:property-placeholder location="classpath:jdbc.properties"/>
 
 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
 	<property name="driverClassName" value="${jdbc.driver}"></property>
 	<property name="url" value="${jdbc.url}"></property>
 	<property name="username" value="${jdbc.username}"></property>
 	<property name="password" value="${jdbc.password}"></property>
 </bean>
 
 <!-- 声明事物管理器  ref代表引用上面创建的dataSource,依赖于数据源-->
 <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource"></property> 
</bean>	

<!-- 开启事物注解驱动,transaction-manage的值要与事物管理器的id一致-->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>

<!-- 管理mybatis操作数据库的会话对象SqlSession -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
	<!-- 设置mybatis核心配置文件的路径 -->
	<property name="configLocation" value="classpath:mybatis-config.xml"></property>
	<!-- 设置数据源 -->
	<property name="dataSource" ref="dataSource"></property>
	<!-- 设置类型别名 -->
	<property name="typeAliasesPackage" value="com.atguigu.ssm.bean"></property>
	<!-- 设置映射文件的路径 -->
	<property name="mapperLocations" value="classpath:com/atguigu/ssm/mapper/*.xml"></property>
</bean>

<!-- Mapper接口
		MapperScannerConfigurer 在所设置的包下,将所有的接口生成动态代理实现类,并由spring容器管理,从而可以实现自动装配
	 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.atguigu.ssm.mapper"></property>
	</bean>

而mybatis-config中剩下的就是setting标签中的代码。至此SSM已经整合完毕。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值