SpringMVC与MyBatis整合方法

您所在的位置:网站首页 mybatis与spring整合过程 SpringMVC与MyBatis整合方法

SpringMVC与MyBatis整合方法

2023-04-25 04:23| 来源: 网络整理| 查看: 265

一、springmvc+mybaits的系统架构:

第一步:整合dao层

         mybatis和spring整合,通过spring管理mapper接口。

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

第二步:整合service层

         通过spring管理 service接口。

         使用配置方式将service接口配置在spring配置文件中。

         实现事务控制。

第三步:整合springmvc

         由于springmvc是spring的模块,不需要整合。

 整合步骤:

一、首先创建Mybatis自己的配置文件(例如:sqlMapConfig.xml)

DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> View Code

 tips:使用自动扫描器时,mapper.xml文件如果和mapper.java接口在一个目录则此处不用定义mappers.

二、创建applicationContext-dao.xml文件,用以配置数据源、事务管理,配置SqlSessionFactory、mapper扫描器。

1 15 16 17 18 19 20 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 View Code

 db.properties:

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

 tips:使用逆向工程生成po类及mapper(单表增删改查)

 三、手动定义商品查询mapper及商品查询dao接口

ItemsMapperCustom.xml

1 2 DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 3 4 5 6 7 8 9 10 11 items.name LIKE '%${itemsCustom.name}%' 12 13 14 15 16 17 18 21 23 SELECT items.* FROM items 24 25 26 27 28 29 15 16 19 20 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 View Code 

五、配置springmvc.xml文件

1 15 16 19 20 21 22 23 24 25 26 27 32 33 34 35 38 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 View Code

六、编写controller

1 @RequestMapping("/items") 2 public class ItemsController { 3 4 @Autowired 5 private ItemsService itemsService; 6 7 // 商品查询 8 @RequestMapping("/queryItems") 9 public ModelAndView queryItems(HttpServletRequest request) throws Exception { 10 //测试forward后request是否可以共享 11 12 System.out.println(request.getParameter("id")); 13 14 // 调用service查找 数据库,查询商品列表 15 List itemsList = itemsService.findItemsList(null); 16 17 // 返回ModelAndView 18 ModelAndView modelAndView = new ModelAndView(); 19 // 相当 于request的setAttribut,在jsp页面中通过itemsList取数据 20 modelAndView.addObject("itemsList", itemsList); 21 22 // 指定视图 23 // 下边的路径,如果在视图解析器中配置jsp路径的前缀和jsp路径的后缀,修改为 24 // modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp"); 25 // 上边的路径配置可以不在程序中指定jsp路径的前缀和jsp路径的后缀 26 modelAndView.setViewName("items/itemsList"); 27 28 return modelAndView; 29 30 } 31 } View Code

七、编写JSP页面(省略...)

八、加载spring容器

在web.xml文件中进行如下配置

1 2 6 springmvc_mybatis1208 7 8 9 10 contextConfigLocation 11 /WEB-INF/classes/spring/applicationContext-*.xml 12 13 14 org.springframework.web.context.ContextLoaderListener 15 16 17 18 19 20 springmvc 21 org.springframework.web.servlet.DispatcherServlet 22 23 24 contextConfigLocation 25 classpath:spring/springmvc.xml 26 27 28 29 30 springmvc 31 33 *.action 34 35 36 37 38 CharacterEncodingFilter 39 org.springframework.web.filter.CharacterEncodingFilter 40 41 encoding 42 utf-8 43 44 45 46 CharacterEncodingFilter 47 /* 48 49 50 51 index.html 52 index.htm 53 index.jsp 54 default.html 55 default.htm 56 default.jsp 57 58 View Code


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3