博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringMVC与MyBatis整合方法
阅读量:4544 次
发布时间:2019-06-08

本文共 9223 字,大约阅读时间需要 30 分钟。

一、springmvc+mybaits的系统架构:

第一步:整合dao层

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

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

第二步:整合service层

         通过spring管理 service接口。

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

         实现事务控制。

第三步:整合springmvc

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

 整合步骤:

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

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.Driverjdbc.url=jdbc:mysql://localhost:3306/mybatisjdbc.username=rootjdbc.password=mysql
View Code

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

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

ItemsMapperCustom.xml

1 
2 3
4 5
6
7
8
9
10
11 items.name LIKE '%${itemsCustom.name}%'12
13
14 15
16 17
18
21
28 29
View Code

 ItemsMapperCustom.java

1 package cn.itcast.ssm.mapper; 2  3 import cn.itcast.ssm.po.Items; 4 import cn.itcast.ssm.po.ItemsCustom; 5 import cn.itcast.ssm.po.ItemsExample; 6 import cn.itcast.ssm.po.ItemsQueryVo; 7  8 import java.util.List; 9 import org.apache.ibatis.annotations.Param;10 11 public interface ItemsMapperCustom {12     //商品查询列表13     public List
findItemsList(ItemsQueryVo itemsQueryVo)throws Exception;14 }
View Code

四、整合service,让spring管理service接口,进行事务管理配置。

首先,定义Service接口和Service的实现类

1 package cn.itcast.ssm.service; 2  3 import java.util.List; 4  5 import cn.itcast.ssm.po.ItemsCustom; 6 import cn.itcast.ssm.po.ItemsQueryVo; 7  8 /** 9  * 10  * 

Title: ItemsService

11 *

Description:商品管理service

12 *

Company: www.itcast.com

13 * @author 传智.燕青14 * @date 2015-4-13下午3:48:0915 * @version 1.016 */17 public interface ItemsService {18 19 //商品查询列表20 public List
findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;21 22 //根据id查询商品信息23 /**24 * 25 *

Title: findItemsById

26 *

Description:

27 * @param id 查询商品的id28 * @return29 * @throws Exception30 */31 public ItemsCustom findItemsById(Integer id) throws Exception;32 33 //修改商品信息34 /**35 * 36 *

Title: updateItems

37 *

Description:

38 * @param id 修改商品的id39 * @param itemsCustom 修改的商品信息40 * @throws Exception41 */42 public void updateItems(Integer id,ItemsCustom itemsCustom) throws Exception;43 44 45 }
View Code 
1 package cn.itcast.ssm.service.impl; 2  3 import java.util.List; 4  5 import org.springframework.beans.BeanUtils; 6 import org.springframework.beans.factory.annotation.Autowired; 7  8 import cn.itcast.ssm.mapper.ItemsMapper; 9 import cn.itcast.ssm.mapper.ItemsMapperCustom;10 import cn.itcast.ssm.po.Items;11 import cn.itcast.ssm.po.ItemsCustom;12 import cn.itcast.ssm.po.ItemsQueryVo;13 import cn.itcast.ssm.service.ItemsService;14 15 /**16  * 17  * 

Title: ItemsServiceImpl

18 *

Description: 商品管理

19 *

Company: www.itcast.com

20 * @author 传智.燕青21 * @date 2015-4-13下午3:49:5422 * @version 1.023 */24 public class ItemsServiceImpl implements ItemsService{25 26 @Autowired27 private ItemsMapperCustom itemsMapperCustom;28 29 @Autowired30 private ItemsMapper itemsMapper;31 32 @Override33 public List
findItemsList(ItemsQueryVo itemsQueryVo)34 throws Exception {35 //通过ItemsMapperCustom查询数据库36 return itemsMapperCustom.findItemsList(itemsQueryVo);37 }38 39 @Override40 public ItemsCustom findItemsById(Integer id) throws Exception {41 42 Items items = itemsMapper.selectByPrimaryKey(id);43 //中间对商品信息进行业务处理44 //....45 //返回ItemsCustom46 ItemsCustom itemsCustom = new ItemsCustom();47 //将items的属性值拷贝到itemsCustom48 BeanUtils.copyProperties(items, itemsCustom);49 50 return itemsCustom;51 52 }53 54 @Override55 public void updateItems(Integer id, ItemsCustom itemsCustom) throws Exception {56 //添加业务校验,通常在service接口对关键参数进行校验57 //校验 id是否为空,如果为空抛出异常58 59 //更新商品信息使用updateByPrimaryKeyWithBLOBs根据id更新items表中所有字段,包括 大文本类型字段60 //updateByPrimaryKeyWithBLOBs要求必须转入id61 itemsCustom.setId(id);62 itemsMapper.updateByPrimaryKeyWithBLOBs(itemsCustom);63 }64 65 }
View Code

然后,在spring容器配置service(applicationContext-service.xml)

1 
2
View Code

配置事务控制,applicationContext-transcation.xml

1 
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 // 返回ModelAndView18 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

转载于:https://www.cnblogs.com/ustc-anmin/p/10463025.html

你可能感兴趣的文章
windows电脑常用必备软件
查看>>
打包部署到tomcat
查看>>
第六课 链接学习
查看>>
Mysql索引PRIMARY、NORMAL、UNIQUE、FULLTEXT 区别和使用场合
查看>>
海量积分数据实时排名算法
查看>>
SQL server Error Number
查看>>
select2 插件
查看>>
MyEclipse 中文乱码
查看>>
SpringAOP03 项目脚手架、自定义注解、织入切面、引介增强
查看>>
核心动画-关键帧动画易混淆属性记录
查看>>
IOS蓝牙开发模块
查看>>
poj2728 最优比率生成树
查看>>
Android开发 打开已存在的项目(以虹软人脸识别sdk的demo为例)
查看>>
身份证号码最后一位校检码的计算公式
查看>>
使用 iTextSharp 生成 PDF 表格
查看>>
sql 查出一张表中重复的所有记录数据
查看>>
IIS 上传大文件 30MB 设置限制了上传大小
查看>>
vue2.0结合Element实现select动态控制input禁用
查看>>
【Codeforces Round #455 (Div. 2) A】Generate Login
查看>>
【Codeforces 476C】Dreamoon and Sums
查看>>