博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringMvc学习-增删改查
阅读量:4693 次
发布时间:2019-06-09

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

本节主要介绍SpringMVC简单的增删改查功能。

1.查询

dao中的代码

1     public List
getAllWeather(){ 2 3 String sql="select * from weathertest"; 4 List
pojos=new ArrayList
(); 5 pojos= jdbcTemplate.query(sql,new RowMapper() { 6 7 @Override 8 public Object mapRow(ResultSet rs, int arg1) throws SQLException { 9 // TODO Auto-generated method stub10 WeatherPojo weather=new WeatherPojo();11 weather.setName(rs.getString("name"));12 weather.setPassword(rs.getString("password"));13 weather.setId(rs.getInt("id"));14 return weather;15 }16 });17 return pojos;18 }
查询

同事,还可以写service和serviceimpl。需要对jdmctempl添加注解

@Autowired

private JdbcTemplate jdbcTemplate;

在impl中需要对dao添加注解

@Autowired

private WeatherDao weatherDao;

在controller中调用服务

1     @Autowired2     private WeatherServiceImpl weatherService;3     @RequestMapping(params="method=query")4     public ModelAndView getAllWeather(HttpServletRequest request,HttpServletResponse response){5         List
pojos=weatherService.getWeatherList();6 request.setAttribute("weathers", pojos);7 System.out.println(pojos.get(0).getName());8 return new ModelAndView("weatherlist");9 }

通过modelandview返回页面,页面代码如下:

1 <%@ page language="java" contentType="text/html; charset=utf-8" 2     pageEncoding="utf-8"%> 3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 4     <%String path = request.getContextPath(); 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 6 %> 7  8  9 10 
11 Insert title here12 13 14
15
16
26
17
18
19
20
21
22
23
24
25
27
28
29
30
31
32 33
34
姓名 说明 操作
${item.name } ${item.password } 编辑删除
35
36 37
list

2.增加

dao中代码

1     public void addWeather(WeatherPojo weather){2         String sql="insert into weathertest(id,name,password) values("+weather.getId()+",'"+weather.getName()+"','"+weather.getPassword()+"')";3         jdbcTemplate.execute(sql);4     }

controller代码,get方法是进入新增页面,页面传递空对象。post方法为添加新的记录

1     @RequestMapping(params="method=add",method=RequestMethod.GET) 2     public ModelAndView addWeather(HttpServletRequest request,HttpServletResponse reponse){ 3          4         request.setAttribute("weather", new WeatherPojo()); 5         return new ModelAndView("weatheradd"); 6     } 7     @RequestMapping(params="method=add",method=RequestMethod.POST) 8     public ModelAndView addWeather(WeatherPojo weather){ 9         10         weatherService.addWeather(weather);11         return new ModelAndView("redirect:/weather.do?method=query");12     }

jsp页面代码

1 <%@ page language="java" contentType="text/html; charset=utf-8" 2     pageEncoding="utf-8"%> 3         <%String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6  7  8  9 
10 Insert title here11 12 13
14
15
16
17
18
19
20
21
22 23

3.修改

dao中代码:

1 public void editWeather(WeatherPojo weather){2         String sql="update weathertest set name='"+weather.getName()+"',password='"+weather.getPassword()+"' where id="+weather.getId()+"";3         jdbcTemplate.execute(sql);4     }

controller代码

1 @RequestMapping(params="method=edit",method=RequestMethod.GET) 2     public ModelAndView editWeather(HttpServletRequest request,HttpServletResponse response){ 3          4         int id=Integer.valueOf(request.getParameter("id")); 5         WeatherPojo weather=new WeatherPojo(); 6         weather=weatherService.getWeatherById(id); 7         ModelAndView mav=new ModelAndView("editweather"); 8         request.setAttribute("weather", weather); 9         System.out.println("--------"+weather.getId());10         System.out.println("--------"+weather.getName());11         System.out.println("--------"+weather.getPassword());12         return mav;13     }14     @RequestMapping(params="method=edit",method=RequestMethod.POST)15     public ModelAndView editWeather(WeatherPojo weather){16         weatherService.editWeather(weather);17         return new ModelAndView("redirect:/weather.do?method=query");18     }
View Code

jsp页面:

1 <%@ page language="java" contentType="text/html; charset=utf-8" 2     pageEncoding="utf-8"%> 3             <%String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6  7  8  9 
10 Insert title here11 12 13
14
15
16
17
18
19
20
21
22 23
View Code

4.删除

dao中代码:

//delete    public void deleteWeather(int id){                String sql="delete from weathertest where id="+id;        jdbcTemplate.execute(sql);    }

controller代码:

@RequestMapping(params="method=delete",method=RequestMethod.GET)    public ModelAndView deleteWeather(HttpServletRequest request,HttpServletResponse response){                int id=Integer.valueOf(request.getParameter("id"));        weatherService.deleteWeather(id);        //页面重定向        return new ModelAndView("redirect:/weather.do?method=query");    }

jsp代码:

1 <%@ page language="java" contentType="text/html; charset=utf-8" 2     pageEncoding="utf-8"%> 3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 4     <%String path = request.getContextPath(); 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 6 %> 7  8  9 10 
11 Insert title here12 13 14
15
16
26
17
18
19
20
21
22
23
24
25
27
28
29
30
31
32 33
34
姓名 说明 操作
${item.name } ${item.password } 编辑删除
35
36 37

 

转载于:https://www.cnblogs.com/ggz19/p/5432346.html

你可能感兴趣的文章
页面重构总结
查看>>
IO 函数
查看>>
Unity V3 初步使用 —— 为我的.NET项目从简单三层架构转到IOC做准备
查看>>
JSP页面间传递参数
查看>>
VSNETcodePrint 2005 & SQL ServerPrint 2005
查看>>
java数组基本操作
查看>>
String的indexOf()用于获取字符串中某个子字符串的位置
查看>>
shell 脚本运算符
查看>>
又一道软通动力7K月薪面试题——银行业务调度系统
查看>>
Matlab画图-非常具体,非常全面
查看>>
ReactJS入门
查看>>
linux网站配置文件.htaccess伪静态转换到IIS web.config中
查看>>
CodeForces 1B
查看>>
win10应用UserControl
查看>>
BZOJ4516: [Sdoi2016]生成魔咒(后缀自动机)
查看>>
查看手机已经记住的WIFI密码
查看>>
最新版IntelliJ IDEA2019 破解教程(2019.08.07-情人节更新)
查看>>
我是怎么用缠论在商品里边抢钱之二 (2019-07-12 15:10:10)
查看>>
python入门之正则表达式
查看>>
SAS学习经验总结分享:篇五-过程步的应用
查看>>