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>
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 <!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     }
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 <!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>
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 <!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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM