SpringMvc学习-增删改查

时间:2022-04-29
本文章向大家介绍SpringMvc学习-增删改查,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

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

1.查询

dao中的代码

 1     public List<WeatherPojo> getAllWeather(){
 2         
 3         String sql="select * from weathertest";
 4         List<WeatherPojo> pojos=new ArrayList<WeatherPojo>();
 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 stub
10                 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     @Autowired
2     private WeatherServiceImpl weatherService;
3     @RequestMapping(params="method=query")
4     public ModelAndView getAllWeather(HttpServletRequest request,HttpServletResponse response){
5         List<WeatherPojo> 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 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 8 <html>
 9 <head>
10 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
11 <title>Insert title here</title>
12 </head>
13 <body>
14 <div><a href="<%=basePath %>weather.do?method=add">添加</a></div>
15 <div>
16 <table>
17 <thead>
18 <tr>
19 <th>姓名</th>
20 <th>说明</th>
21 <th>操作</th>
22 </tr>
23 </thead>
24 <tbody>
25 <c:forEach var="item" items="${weathers}">
26 <tr>
27 <td>${item.name }</td>
28 <td>${item.password }</td>
29 <td></td>
30 <td><a href="<%=basePath %>weather.do?method=edit&id=${item.id}">编辑</a><a href="<%=basePath %>weather.do?method=delete&id=${item.id}">删除</a></td>
31 </tr>
32 </c:forEach>
33 </tbody>
34 </table>
35 </div>
36 </body>
37 </html>

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 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 7 <html>
 8 <head>
 9 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
10 <title>Insert title here</title>
11 </head>
12 <body>
13 <form action="<%=basePath%>weather.do?method=add" method="post">
14 <label for="id">id</label>
15 <input name="id"/><br>
16 <label for="name">name</label>
17 <input name="name"><br>
18 <label for="password">password</label>
19 <input name="password"><br>
20 <input type="submit" value="提交">
21 </form>
22 </body>
23 </html>

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 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 7 <html>
 8 <head>
 9 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
10 <title>Insert title here</title>
11 </head>
12 <body>
13 <form action="<%=basePath%>weather.do?method=edit" method="post">
14 <label for="id">id</label>
15 <input name="id" readonly="true" value="${weather.id }"/><br>
16 <label for="name">name</label>
17 <input name="name" value="${weather.name }"><br>
18 <label for="password" >password</label>
19 <input name="password" value="${weather.password }"><br>
20 <input type="submit" value="提交">
21 </form>
22 </body>
23 </html>

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 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 8 <html>
 9 <head>
10 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
11 <title>Insert title here</title>
12 </head>
13 <body>
14 <div><a href="<%=basePath %>weather.do?method=add">添加</a></div>
15 <div>
16 <table>
17 <thead>
18 <tr>
19 <th>姓名</th>
20 <th>说明</th>
21 <th>操作</th>
22 </tr>
23 </thead>
24 <tbody>
25 <c:forEach var="item" items="${weathers}">
26 <tr>
27 <td>${item.name }</td>
28 <td>${item.password }</td>
29 <td></td>
30 <td><a href="<%=basePath %>weather.do?method=edit&id=${item.id}">编辑</a><a href="<%=basePath %>weather.do?method=delete&id=${item.id}">删除</a></td>
31 </tr>
32 </c:forEach>
33 </tbody>
34 </table>
35 </div>
36 </body>
37 </html>