本节主要介绍SpringMVC简单的增删改查功能。
1.查询
dao中的代码
1 public ListgetAllWeather(){ 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 Listpojos=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 11Insert title here 12 13 14 15 36 37
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 1022 23Insert title here 11 12 13
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 }
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 1022 23Insert title here 11 12 13
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 11Insert title here 12 13 14 15 36 37