java mvc增删改查_SpringMvc学习-增删改查
本节主要介绍SpringMVC简单的增删改查功能。
1.查询
dao中的代码


1 public ListgetAllWeather(){2
3 String sql="select * from weathertest";4 List pojos=new ArrayList();5 pojos= jdbcTemplate.query(sql,newRowMapper() {6
7 @Override8 public Object mapRow(ResultSet rs, int arg1) throwsSQLException {9 //TODO Auto-generated method stub
10 WeatherPojo weather=newWeatherPojo();11 weather.setName(rs.getString("name"));12 weather.setPassword(rs.getString("password"));13 weather.setId(rs.getInt("id"));14 returnweather;15 }16 });17 returnpojos;18 }
查询
同事,还可以写service和serviceimpl。需要对jdmctempl添加注解
@Autowired
private JdbcTemplate jdbcTemplate;
在impl中需要对dao添加注解
@Autowired
private WeatherDao weatherDao;
在controller中调用服务
1 @Autowired2 privateWeatherServiceImpl weatherService;3 @RequestMapping(params="method=query")4 publicModelAndView 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
2 pageEncoding="utf-8"%>
3
4
7
8
9
10
11
Insert title here12
13
14
15
16
17
18
19
姓名20
说明21
操作22
23
24
25
26
27
${item.name }28
${item.password }29
30
weather.do?method=edit&id=${item.id}">编辑weather.do?method=delete&id=${item.id}">删除31
32
33
34
35
36
37
list
2.增加
dao中代码
1 public voidaddWeather(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 publicModelAndView addWeather(HttpServletRequest request,HttpServletResponse reponse){3
4 request.setAttribute("weather", newWeatherPojo());5 return new ModelAndView("weatheradd");6 }7 @RequestMapping(params="method=add",method=RequestMethod.POST)8 publicModelAndView addWeather(WeatherPojo weather){9
10 weatherService.addWeather(weather);11 return new ModelAndView("redirect:/weather.do?method=query");12 }
jsp页面代码
1
2 pageEncoding="utf-8"%>
3
6
7
8
9
10
Insert title here11
12
13 weather.do?method=add"method="post">
14 id
15
16 name
17
18 password
19
20
21
22
23
3.修改
dao中代码:
1 public voideditWeather(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 publicModelAndView editWeather(HttpServletRequest request,HttpServletResponse response){3
4 int id=Integer.valueOf(request.getParameter("id"));5 WeatherPojo weather=newWeatherPojo();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 returnmav;13 }14 @RequestMapping(params="method=edit",method=RequestMethod.POST)15 publicModelAndView editWeather(WeatherPojo weather){16 weatherService.editWeather(weather);17 return new ModelAndView("redirect:/weather.do?method=query");18 }
View Code
jsp页面:


1
2 pageEncoding="utf-8"%>
3
6
7
8
9
10
Insert title here11
12
13 weather.do?method=edit"method="post">
14 id
15
16 name
17
18 password
19
20
21
22
23
View Code
4.删除
dao中代码:
//delete
public void deleteWeather(intid){
String sql="delete from weathertest where id="+id;
jdbcTemplate.execute(sql);
}
controller代码:
@RequestMapping(params="method=delete",method=RequestMethod.GET)publicModelAndView 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
2 pageEncoding="utf-8"%>
3
4
7
8
9
10
11
Insert title here12
13
14
15
16
17
18
19
姓名20
说明21
操作22
23
24
25
26
27
${item.name }28
${item.password }29
30
weather.do?method=edit&id=${item.id}">编辑weather.do?method=delete&id=${item.id}">删除31
32
33
34
35
36
37
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)