springmvc(二) ssm框架整合的各种配置

时间:2023-03-08 21:11:18
springmvc(二) ssm框架整合的各种配置

      ssm:springmvc、spring、mybatis这三个框架的整合,有耐心一步步走。

                        --WZY

一、SSM框架整合

      1.1、整合思路

        从底层整合起,也就是先整合mybatis与spring,然后在编写springmvc。

      1.2、开发需求

        查询商品列表(从数据库中查询)

      1.3、创建web工程

          springmvc(二) ssm框架整合的各种配置

        现在ssm的工程创建就有区别于原先的dao、service、web这样的三层目录了,现在是mapper、service、controller这样的目录,mapper就相当于以前的dao、controller相当于以前的web,改变了名称而已。不要因此看不懂了。

      1.4、添加jar包

        这种jar包,上网直接百度ssm整合的jar包即可

        数据库驱动、Mybatis的核心、依赖包、Mybatis与spring的整合包、Dbcp连接池包、Spring的包(包括springmvc的包)、Aop的依赖包、Jstl包、Common-logging包   

          springmvc(二) ssm框架整合的各种配置

      1.5、开始整合mapper(mybatis与spring的整合)

        详细的整合思路讲解:mybatis与spring的整合 这里我直接上代码。步骤

        1.5.1、SqlMapConfig.xml

          springmvc(二) ssm框架整合的各种配置

<?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> <!-- 取别名,或者别的其他全局配置信息,就在这里编写 -->
<typeAliases>
<!-- 别名为类名首字母大小写都可以 -->
<package name="com.wuhao.ms.domain"/>
</typeAliases>
<!-- 原先这里还有连接数据库的一些配置,与spring整合后,都交由spring来管理 --> <!-- 加载mapper映射文件,使用通用的配置 -->
<mappers>
<package name="com.wuhao.ssm.mapper"/>
</mappers>
</configuration>

SqlMapConfig.xml

        1.5.2、applicationContext-dao.xml的配置

            springmvc(二) ssm框架整合的各种配置   

          这里需要注意一点,在指定mybatis的全局配置文件的路径的时候,也就是在value="classpath:SqlMapConfig.xml"时,如果在创建的config的配置文件目录下还有层级目录,则这里需要加上,比如,config下面分为了mybatis和spring,那么这里就需要写value="classpath:mybatis/SqlMapConfig.xml",看根据你自己的需求来编写

            springmvc(二) ssm框架整合的各种配置

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 引用java配置文件 -->
<context:property-placeholder location="classpath:db.properties" /> <!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${db.driver}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean> <!-- 配置SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定mybatis的全局配置文件的路径 -->
<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 批量配置,这里是批量配置mapper代理,那么下面就不用配置id了。
我们想要获取哪个mapper代理用这个格式:类名首字母小写
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wuhao.ssm.mapper"></property>
<!-- 默认不需要配置,但是如果有多个数据源的配置,那么就需要配置此项 -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean> </beans>

applicationContext-dao.xml

               

        1.5.3、db.properties配置

            springmvc(二) ssm框架整合的各种配置

db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
db.username=root
db.password=root

db.properties

        1.5.4、开发mapper,将逆向工程生成的添加进来

            springmvc(二) ssm框架整合的各种配置

          注意:Mapper开发时,先要根据需求进行分析,是否匹配逆向工程生成的代码,如果匹配成功,则不需要再开发mapper;如果不匹配,再去扩展一个新的mapper接口和mapper映射文件来处理该需求,通俗点讲,就是逆向工程生成的mapper接口中的定义的功能是否满足我们开发的需求,因为逆向工程生成的都是对于单表进行操作的,而我们有时候需要的是更复杂的查询,所以如果有需要我们在自己创建mapper接口和mapper映射文件,其实就是扩展功能。

      1.6、整合service

        添加applicationContext-service.xml配置文件,用来处理事务,

        applicationContext-service.xml:如果不懂其中的代码的意思,就查看之前讲解spring管理事务的文章。这里直接复制粘帖即可,修改一些包名称等

          springmvc(二) ssm框架整合的各种配置

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 組件掃描service -->
<context:component-scan base-package="com.wuhao.ssm.service"></context:component-scan>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 配置通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 传播特性 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="find*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="select*" read-only="true" />
<tx:method name="get*" read-only="true" />
</tx:attributes>
</tx:advice> <!-- aop -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.wuhao.ssm.service.*.*(..))"/>
</aop:config>
</beans>

applicationContext-service.xml

      1.7、整合controller

        也就是使用springmvc了。非常简单。

        1.7.1、在web.xml中配置前端控制器DispatcherServlet

          springmvc(二) ssm框架整合的各种配置

 <!-- springmvc 的前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定springmvc的配置文件的地址 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 这里有三种配置url-pattern方案
1、*.do:后缀为.do的请求才能够访问到该servlet[用这个]
2、/ :所有请求都能够访问到该servlet(除jsp),包括静态请求(处理会有问题,不用)
3、/* :有问题,因为访问jsp也会到该servlet,而访问jsp时,我们不需要这样,也不用
-->
<url-pattern>*.do</url-pattern>
</servlet-mapping>

web.xml中前端控制器DispatcherServlet的配置

        1.7.2、配置springmvc.xml

          springmvc(二) ssm框架整合的各种配置

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 扫描 -->
<context:component-scan base-package="com.wuhao.ssm.controller"></context:component-scan>
<!-- 配置处理器映射器和处理器适配器 -->
<mvc:annotation-driven /> <!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean> </beans>

springmvc.xml

      1.8、整合spring配置文件      

        就是将所有的spring的配置文件都进行加载启动。也就是在web.xml中配置spring的监听器

          springmvc(二) ssm框架整合的各种配置    

 <!-- 加载spring容器 -->
<!-- 配置监听器,用于加载spring 配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value> </context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

web.xml中配置加载spring容器和监听器

      1.9、总结所有的配置如下图

          springmvc(二) ssm框架整合的各种配置    

      1.10、部署测试

        1.10.1、查询商品列表(从数据库中查询)

          1、编写service层

            ItemsService 接口

              springmvc(二) ssm框架整合的各种配置

            ItemsServiceImpl 实现类 不使用注解开发

              springmvc(二) ssm框架整合的各种配置

            applicationContext-service.xml中配置该service的bean

              springmvc(二) ssm框架整合的各种配置  

            ItemsServiceImpl 实现类 使用注解的话,就不需要在applicationContext-service.xml中配置该service的bean了

              springmvc(二) ssm框架整合的各种配置

          2、编写controller层

            该层的编写有很多中方式,我记得前一节讲解过,比如实现Controller接口,使用注解等,一般直接使用注解。

            ItemsController

              springmvc(二) ssm框架整合的各种配置    

          3、添加jsp页面

              springmvc(二) ssm框架整合的各种配置

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查询商品列表</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/item/queryItem.action" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<td><input type="submit" value="查询"/></td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr>
<td>商品名称</td>
<td>商品价格</td>
<td>生产日期</td>
<td>商品描述</td>
<td>操作</td>
</tr>
<c:forEach items="${itemsList }" var="item">
<tr>
<td>${item.name }</td>
<td>${item.price }</td>
<td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
<td>${item.detail }</td> <td><a href="${pageContext.request.contextPath }/editItems.do?id=${item.id}">修改</a></td> </tr>
</c:forEach> </table>
</form>
</body> </html>

itemsList.jsp

          4、测试

              http://localhost:8080/ssm_test01/queryItems.do 如下图,即成功

              springmvc(二) ssm框架整合的各种配置

二、总结

      这样,ssm的框架整合就结束了,非常简单,按步骤,先整合mybatis与spring,然后在整合springmvc。自己练习几遍就会了。接下来的文章就会以此为基础,讲解springmvc的各种小功能,比如,springmvc的参数绑定、springmvc的校验器,图片的上传等。