博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ssm框架整合
阅读量:6903 次
发布时间:2019-06-27

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

springmvc+mybaits的系统架构:

第一步:整合dao层

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

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

第二步:整合service层

         通过spring管理 service接口。

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

         实现事务控制。

第三步:整合springmvc

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

 

 整合dao

mybatis和spring进行整合。

sqlMapConfig.xml:

1 
2 5 6
7
8 9
10
11
12
13
14 15
19 20
23

applicationContext-dao.xml

配置:

数据源

SqlSessionFactory

mapper扫描器

1 
2
16 17
18
19
20 21
23
24
25
26
27
28
29
30 31
32
33
34
35
36
37
38 39
40
41
42
43
44

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

将生成的文件拷贝至工程中。

手动定义商品查询mapper

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

 ItemsMapperCustom.xml:

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

ItemsMapperCustom.java

1 public interface ItemsMapperCustom {2     3     //商品查询列表4     public List
findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;5 }

整合service

让spring管理service接口。

 定义service接口

public interface ItemsService {    //商品查询列表        public List
findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;}public class ItemsServiceImpl implements ItemsService { @Autowired private ItemsMapperCustom itemsMapperCustom; public List
findItemsList(ItemsQueryVo itemsQueryVo) throws Exception { return itemsMapperCustom.findItemsList(itemsQueryVo); }}

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

创建applicationContext-service.xml,文件中配置service。

1 
2
16 17
18

事务控制(applicationContext-transaction.xml)

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

1 
2
16 17
20
21
24
25
26 27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

整合springmvc

springmvc.xml

创建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
56 57

 配置前端控制器

 编写Controller(就是Handler)

1 @Controller 2 public class ItemsController { 3     @Autowired 4     private ItemsService itemsService; 5      6     //将方法和url进行映射,一个方法对应一个url 7         @RequestMapping("/queryItems") 8         public ModelAndView queryItems() throws Exception{ 9             //调用service查找 数据库,查询商品列表,这里使用静态数据模拟10             List
itemsList = itemsService.findItemsList(null);11 ModelAndView modelAndView = new ModelAndView();12 //相当 于request的setAttribut,在jsp页面中通过itemsList取数据13 modelAndView.addObject("itemsList", itemsList);14 //指定视图15 modelAndView.setViewName("items/itemsList");16 return modelAndView;17 }18 }

加载spring容器

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

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

1 
2
3
contextConfigLocation
4
/WEB-INF/classes/spring/applicationContext-*.xml
5
6
7
org.springframework.web.context.ContextLoaderListener
8
9 10 11
12
13
springmvc
14
org.springframework.web.servlet.DispatcherServlet
15
16
17
contextConfigLocation
18
classpath:spring/springmvc.xml
19
20
21 22
23
springmvc
24
26
*.action
27
28 29
30
31
CharacterEncodingFilter
32
org.springframework.web.filter.CharacterEncodingFilter
33
34
encoding
35
utf-8
36
37
38
39
CharacterEncodingFilter
40
/*
41

 编写jsp

1 
2 3 ${item.name } 4 ${item.price } 5
6 ${item.detail } 7 8
修改 9 10 11

 

转载于:https://www.cnblogs.com/cuibin/p/6850695.html

你可能感兴趣的文章
学习html我们从百度百科开始
查看>>
如何Spring Cloud Zuul作为网关的分布式系统中整合Swagger文档在同一个页面上
查看>>
实现一个炫酷的随机标签排列效果(颜色随机,大小随机,成菱形排列的列表)...
查看>>
flex 布局
查看>>
数字资产交易所开发:平台币快速吸金的背后
查看>>
小程序自定义音频组件,带滚动条,IOS循环失效问题
查看>>
Swift开发之粒子动画的实现
查看>>
我学Java我傲娇
查看>>
挖矿蠕虫肆虐,详解云防火墙如何轻松“制敌”
查看>>
Linux -- Samba之客户端访问(Linux和windows)
查看>>
八个Docker的真实应用场景
查看>>
vpc的使用方法
查看>>
GitExtensions GitCredentialWinStore syntax error near unexpected token `('
查看>>
Java获取EXE文件图标的方法
查看>>
“驱动程序在 \Device\Harddisk0\D 上检测到控制器错误”的根本解决办法!
查看>>
ubuntu 之修改权限的问题
查看>>
php 框架ci去index.php的方法
查看>>
Hyper-v学习(四),SMB虚拟机实时迁移
查看>>
基于spring3注解的google分页
查看>>
实用命令行工具详解—crontab
查看>>