资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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

【SSM的系统架构】

目前创新互联建站已为1000+的企业提供了网站建设、域名、虚拟空间、网站托管、服务器托管、企业网站设计、南康网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

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的配置文件

 

           

 

【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扫描器

 

                      "cn.Higgin.ssm.mapper">   

 

【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】

 

         items.name LIKE '%${itemsCustom.name}%'      

 

 

【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 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{  @Autowiredprivate ItemsMapperCustom itemsMapperCustom; //通过ItemsMapperCustom查询数据库public List findItemsList(ItemsQueryVo itemsQueryVo) throws Exception { return itemsMapperCustom.findItemsList(itemsQueryVo);
    }
}

 

【2.3 applicationContext-service.xml】

在spring容器中配置service

 

   

 

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

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

 

                       

 

 

【3.整合springmvc】

【3.1 springmvc.xml】

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

 

             

 

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

 

  springmvc org.springframework.web.servlet.DispatcherServlet  contextConfigLocation classpath:spring/springmvc.xml    springmvc *.action 

 

【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; @Controllerpublic class ItemsController{ @Autowiredprivate ItemsService itemsService; //商品查询 @RequestMapping("/queryItems")public ModelAndView queryItems() throws Exception{ //调用Service查找数据库,查询商品列表,这里使用静态数据模拟 List 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"%>     查询商品列表   
查询条件:
商品列表:
商品名称 商品价格 生产日期 商品描述 操作
${item.name } ${item.price } ${item.detail } 修改

 

 

【5. 加载spring容器】

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

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

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

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

【web.xml完整】

 

  Spring_SpringMVC_MyBatis   contextConfigLocation /WEB-INF/classes/spring/applicationContext-*.xml   org.springframework.web.context.ContextLoaderListener   springmvc org.springframework.web.servlet.DispatcherServlet  contextConfigLocation classpath:spring/springmvc.xml    springmvc *.action   index.jsp  

 

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

朋友需要请加QQ:2042849237


分享名称:SSM框架整合(Spring+SpringMVC+MyBatis)
转载来源:http://cdkjz.cn/article/gipsso.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220