温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SSM框架整合(Spring+SpringMVC+MyBatis)

发布时间:2020-08-06 19:00:34 来源:ITPUB博客 阅读:124 作者:橙色的雨 栏目:编程语言

【SSM的系统架构】

SSM框架整合(Spring+SpringMVC+MyBatis)

【整合概述】

第一步:

  MyBatis和Spring整合,通过Spring管理mapper接口。

  使用mapper的扫描器自动扫描mapper接口在Spring中进行注册。

第二步:

  通过Spring管理Service接口。

  使用配置方式将Service接口配置在Spring配置文件中。

  实现事务控制。

第三步:

  由于SpringMVC是Spring的模块,无需整合这两个。

 

【工程截图】

SSM框架整合(Spring+SpringMVC+MyBatis)

【数据库的items】

[ 表结构 ]

SSM框架整合(Spring+SpringMVC+MyBatis)

 

[ 表内数据 ]

SSM框架整合(Spring+SpringMVC+MyBatis)

 

【1.整合dao】
将Mybatis和Spring进行整合

【1.1 sqlMapConfig.xml】MyBatis的配置文件

 

<?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> <!-- 全局setting配置 ,根据需要再添加--> <!-- 配置别名 --> <typeAliases> <!-- 批量扫描别名 --> <package name="cn.Higgin.ssm.po"/> </typeAliases> <!-- 配置mapper
     由于使用spring和mybatis的整合包进行整合,这了无需配置
     但必须遵循:mapper.xml和mapper.xml文件同名且在同一目录--> <!--<mappers></mappers>  --> </configuration>

 

【1.2 db.properties】数据库配置文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis jdbc.username=root
jdbc.password=

【1.3 applicationContext-dao.xml】

需要配置:数据源、SqlSessionFactory、mapper扫描器

 

<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 "> <!-- 加载db.properties文件中的内容,db.properties文件中key命名要有一定的特殊规则 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置数据源,dbcp --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="maxActive" value="30"/> <property name="maxIdle" value="5"/> </bean> <!-- sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 数据库连接池 --> <property name="dataSource" ref="dataSource" /> <!-- 加载mybatis的全局配置文件 --> <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" /> </bean> <!-- mapper扫描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 扫描包路径,如果需要扫描多个包,中间使用半角逗号隔开 --> <property name="basePackage" value="cn.Higgin.ssm.mapper"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> </beans>

 

【1.4 逆向工程生成PO类以及mapper】(对应表的增删改查)

SSM框架整合(Spring+SpringMVC+MyBatis)

【1.5 手动定义商品查询mapper】

针对综合查询mapper,一般情况下会有关联查询,建议 自定义 mapper。

【1.5.1 ItemsMapperCustom.java】

 

package cn.Higgin.ssm.po; /**
 *  商品信息的扩展
 */ public class ItemsCustom extends Items { //添加商品信息扩展的属性 }

 

【1.5.2 ItemsMapperCustom.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" > <mapper namespace="cn.Higgin.ssm.mapper.ItemsMapperCustom" > <!-- 定义商品查询的sql片段,就是商品查询条件 --> <sql id="query_items_where"> <!-- 使用动态sql,通过if判断,满足条件进行sql拼接 --> <!-- 商品查询条件通过ItemsQueryVo包装对象 中itemsCustom属性传递 --> <if test="itemsCustom!=null"> <if test="itemsCustom.name!=null and itemsCustom.name!=''"> items.name LIKE '%${itemsCustom.name}%' </if> </if> </sql> <!-- 商品列表查询 --> <!-- parameterType:传入包装对象(包装了查询条件)
          resultType:建议使用扩展对象
       --> <select id="findItemsList" parameterType="cn.Higgin.ssm.po.ItemsQueryVo" resultType="cn.Higgin.ssm.po.ItemsCustom"> SELECT items.* FROM items <where> <include refid="query_items_where"></include> </where> </select> </mapper>

 

 

【2.整合service】

让spring来管理service接口。

【2.1 ItemsService.java】定义service接口

 

package cn.Higgin.ssm.service; import java.util.List; import cn.Higgin.ssm.po.ItemsCustom; import cn.Higgin.ssm.po.ItemsQueryVo; public interface ItemsService { /**
     * 通过ItemsMapperCustomer查询数据库 
     */ public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)throws Exception;
}

 

【2.2 ItemsServiceImpl.java】service实现接口

 

package cn.Higgin.ssm.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import cn.Higgin.ssm.mapper.ItemsMapperCustom; import cn.Higgin.ssm.po.ItemsCustom; import cn.Higgin.ssm.po.ItemsQueryVo; import cn.Higgin.ssm.service.ItemsService; public class ItemsServiceImpl implements ItemsService{  @Autowired private ItemsMapperCustom itemsMapperCustom; //通过ItemsMapperCustom查询数据库 public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception { return itemsMapperCustom.findItemsList(itemsQueryVo);
    }
}

 

【2.3 applicationContext-service.xml】

在spring容器中配置service

 

<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 -->  <bean id="itemService" class="cn.Higgin.ssm.service.impl.ItemsServiceImpl" /> </beans>

 

【2.4 applicationContext-transaction.xml】事务控制

在applicationContext-transaction.xml中使用spring声明 事务控制方法。

 

<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 "> <!-- 事务管理器 对mybatis操作数据库进行事务控制,spring使用jdbc的事务控制类 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 数据源 dataSource在applicationContext-dao.xml中已经配置
         --> <property name="dataSource" ref="dataSource" /> </bean> <!-- 通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 传播行为 --> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="find*" propagation="SUPPORTS" read-only="true"/> <tx:method name="get*" propagation="SUPPORTS" read-only="true"/> <tx:method name="select*" propagation="SUPPORTS" read-only="true"/> </tx:attributes> </tx:advice> <!-- aop --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.Higgin.ssm.service.impl.*.*(..))"/> </aop:config> </beans>

 

 

【3.整合springmvc】

【3.1 springmvc.xml】

在springmvc中配置 处理映射器、适配器、视图解析器

 

<?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: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 "> <!-- 可以扫描controller、service、...
    这里让扫描controller,指定controller的包
     --> <context:component-scan base-package="cn.Higgin.ssm.controller"></context:component-scan> <!-- 使用 mvc:annotation-driven代替注解映射器和注解适配器配置 mvc:annotation-driven默认加载很多的参数绑定方法,
    比如json转换解析器就默认加载了,如果使用mvc:annotation-driven不用配置上边的RequestMappingHandlerMapping和RequestMappingHandlerAdapter
    实际开发时使用mvc:annotation-driven
     --> <mvc:annotation-driven ></mvc:annotation-driven> <!-- 视图解析器 解析jsp解析,默认使用jstl标签,classpath下的得有jstl的包
     --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 配置jsp路径的前缀 --> <property name="prefix" value="/WEB-INF/jsp/"/> <!-- 配置jsp路径的后缀 --> <property name="suffix" value=".jsp"/> </bean> </beans>

 

【3.2 在web.xml中】 在web.xml中配置前端控制器的代码

 

<!-- springMVC前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <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>*.action</url-pattern> </servlet-mapping>

 

【3.3 ItemsController.java】

编写Controller(即Handler)

 

package cn.Higgin.ssm.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import cn.Higgin.ssm.po.ItemsCustom; import cn.Higgin.ssm.service.ItemsService; @Controller public class ItemsController { @Autowired private ItemsService itemsService; //商品查询 @RequestMapping("/queryItems") public ModelAndView queryItems() throws Exception{ //调用Service查找数据库,查询商品列表,这里使用静态数据模拟 List<ItemsCustom> itemsList=itemsService.findItemsList(null); //返回ModelAndView ModelAndView modelAndView=new ModelAndView(); //相当于request的setAttribute,在jsp页面中通过itemList来获取 modelAndView.addObject("itemsList",itemsList); //指定视图 modelAndView.setViewName("items/itemsList");
       System.out.println("注解方式:ItemsComtroller......"); return modelAndView;
   }
}

 

【4.前端jsp页面 itemsList.jsp】

SSM框架整合(Spring+SpringMVC+MyBatis)

 

位置:WEB-INF/jsp/items/itemsList.jsp

 

<%@ 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 }/item/editItem.action?id=${item.id}">修改</a></td> </tr> </c:forEach> </table> </form> </body> </html>

 

 

【5. 加载spring容器】

将mapper、service、controller都加载到spring容器中。

SSM框架整合(Spring+SpringMVC+MyBatis)

建议使用通配符加载上边的配置文件。

在web.xml中,添加Spring容器监听器,加载spring容器

【web.xml完整】

 

<?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="WebApp_ID" version="2.5"> <display-name>Spring_SpringMVC_MyBatis</display-name> <!-- 加载spring容器 -->  <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>  <!-- springMVC前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <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>*.action</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>

 

源码来源:http://minglisoft.cn/technology

朋友需要请加QQ:2042849237

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI