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

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

spring+springmvc+mybatis集成

一个核心:将对象交给spring管理。

1新建web项目

2添加项目jar包

 

spring包见上一篇博客

3建立项目的目录结构

 

4完成Mapper的集成

和mybatis进行集成,交给spring产生Mapper接口的代理对象。

4.1建立Mapper接口

1 package org.guangsoft.mapper; 2  3 import java.util.List; 4  5 import org.guangsoft.pojo.Privilege; 6  7 public interface PrivilegeMapper 8 { 9     public void addPrivilege(Privilege privilege);10     public void deletePrivilege(Privilege privilege);11     public void updatePrivilege(Privilege privilege);12     public Privilege selectPrivilegeById(Privilege privilege);13     public List
selectAllPrivileges();14 }

4.2建立Mapper.xml文件

1 
2 3
4
5 insert into privilege values(null,#{pname}) 6
7
8 delete from privilege 9
10
11 update privilege set pname=#{pname} where pid=#{pid}12
13
16
19
 

4.3建立application_mapper.xml

在config下建立:

配置数据库连接池。

管理SqlSessionFactory,注入DataSource

产生Mapper接口的代理对象。

1 
2
7 8
9
10
11
12
13
14
15
16
17
18
19
20
21
26
27
28
29
30
31
32
 

5完成service的功能

5.1建立Service接口

1 package org.guangsoft.service; 2  3 import java.util.List; 4  5 import org.guangsoft.pojo.Privilege; 6  7 public interface PrivilegeService 8 { 9     public void addPrivilege(Privilege privilege);10     public void deletePrivilege(Privilege privilege);11     public void updatePrivilege(Privilege privilege);12     public Privilege selectPrivilegeById(Privilege privilege);13     public List
selectAllPrivileges();14 } 

5.2建立业务接口实现类

将实现类的对象纳入spring容器。注入Mapper接口的代理对象

1 package org.guangsoft.service.impl; 2  3 import java.util.List; 4  5 import org.guangsoft.mapper.PrivilegeMapper; 6 import org.guangsoft.pojo.Privilege; 7 import org.guangsoft.service.PrivilegeService; 8 import org.springframework.beans.factory.annotation.Autowired; 9 import org.springframework.stereotype.Service;10 11 @Service12 public class PrivilegeServiceImpl implements PrivilegeService13 {14     /**15      * 注入Mapper接口的代理对象16      */17     @Autowired18     private PrivilegeMapper privilegeMapper;19     20     @Override21     public void addPrivilege(Privilege privilege)22     {23         privilegeMapper.addPrivilege(privilege);24     }25 26     @Override27     public void deletePrivilege(Privilege privilege)28     {29         privilegeMapper.deletePrivilege(privilege);30     }31 32     @Override33     public void updatePrivilege(Privilege privilege)34     {35         privilegeMapper.updatePrivilege(privilege);36     }37 38     @Override39     public Privilege selectPrivilegeById(Privilege privilege)40     {41         return privilegeMapper.selectPrivilegeById(privilege);42     }43 44     @Override45     public List
selectAllPrivileges()46 {47 return privilegeMapper.selectAllPrivileges();48 }49 50 } 

5.3建立application_service.xml

扫描service包

事务配置

1 
2
13 14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 

6完成Action的功能

6.1建立Handler处理器

1 package org.guangsoft.controller; 2  3 import org.guangsoft.pojo.Privilege; 4 import org.guangsoft.service.PrivilegeService; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.web.bind.annotation.RequestMapping; 8  9 @Controller10 public class PrivilegeController11 {12     @Autowired13     private PrivilegeService privilegeService;14     //定义菜单项求求的方法15     @RequestMapping("/addPrivilege")16     public String addPrivilege(Privilege privilege)17     {18         privilegeService.addPrivilege(privilege);19         return "index.jsp";20     }21 }

6.2建立springmvc的配置文件

1 
2
12 13
14
15
16
17
 

7配置web.xml

1 
2
3
4
contextConfigLocation
5
classpath:application_*.xml
6
7
8
org.springframework.web.context.ContextLoaderListener
9
10
11
springmvc
12
org.springframework.web.servlet.DispatcherServlet
13
14
contextConfigLocation
15
classpath:springmvc_servlet.xml
16
17
1
18
19
20
springmvc
21
*.action
22
23
 

8建立视图页面

1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6  7  8  9   10     11     My JSP 'index.jsp' starting page12     
13
14 15 16 17
18
19
pname:
20
21
22
23 24  

9发布测试

 

转载于:https://www.cnblogs.com/guanghe/p/6189074.html

你可能感兴趣的文章
Linux文件系统构成(第二版)
查看>>
杭电2099 整除的尾数
查看>>
Struts2--ActionContext及CleanUP Filter
查看>>
Spring MVC 学习笔记 对locale和theme的支持
查看>>
ntp时间同步服务
查看>>
Windows搭建wnmp
查看>>
请说明在.net中常用的几种页面间传递参数的方法,并说出他们的优缺点。
查看>>
12用户体验
查看>>
http://bbs.phome.net/showthread-13-45519-0.html
查看>>
POJ 1008 Maya Calendar / UVA 300【日期转换/常量数组】
查看>>
Java工具类-转换字符编码
查看>>
Pycharm中如何安装python库
查看>>
C++ transform for_each
查看>>
MySQL安装ODBC驱动出现126错误
查看>>
Redis持久化
查看>>
linux xampp eclipse xdebug 无法进入断点
查看>>
app启动时间命令
查看>>
Eclipse下修改工程名
查看>>
request.getSession()
查看>>
iphone 在设置了initial-scale=1 之后,在设置滚动条之后,没有滑动效果的解决办法...
查看>>