大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節(jié)點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 hot資訊 SpringMVC增刪改查功能的簡單介紹

SpringMVC增刪改查功能的簡單介紹

更新時間:2021-12-09 12:39:20 來源:動力節(jié)點 瀏覽1140次

本節(jié)主要介紹SpringMVC簡單的增刪改查功能。

1.查詢

dao中的代碼

public List<WeatherPojo> getAllWeather(){         
         String sql="select * from weathertest";
         List<WeatherPojo> pojos=new ArrayList<WeatherPojo>();
         pojos= jdbcTemplate.query(sql,new RowMapper() { 
             @Override
             public Object mapRow(ResultSet rs, int arg1) throws SQLException {
                 // TODO Auto-generated method stub
                 WeatherPojo weather=new WeatherPojo();
                 weather.setName(rs.getString("name"));
                 weather.setPassword(rs.getString("password"));
                 weather.setId(rs.getInt("id"));
                 return weather;
             }
         });
         return pojos;
     }

還可以寫service和serviceimpl。需要對jdmctempl添加注解

@Autowired

private JdbcTemplate jdbcTemplate;

在impl中需要對dao添加注解

@Autowired

private WeatherDao weatherDao;

在controller中調用服務

@Autowired
    private WeatherServiceImpl weatherService;
    @RequestMapping(params="method=query")
    public ModelAndView getAllWeather(HttpServletRequest request,HttpServletResponse response){
        List<WeatherPojo> pojos=weatherService.getWeatherList();
        request.setAttribute("weathers", pojos);
        System.out.println(pojos.get(0).getName());
        return new ModelAndView("weatherlist");
    }

通過modelandview返回頁面,頁面代碼如下:

<%@ page language="java" contentType="text/html; charset=utf-8"
     pageEncoding="utf-8"%>
 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
     <%String path = request.getContextPath();
 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 %>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 <title>Insert title here</title>
 </head>
 <body>
 <div><a href="<%=basePath %>weather.do?method=add">添加</a></div>
 <div>
 <table>
 <thead>
 <tr>
 <th>姓名</th>
 <th>說明</th>
 <th>操作</th>
 </tr>
 </thead>
 <tbody>
 <c:forEach var="item" items="${weathers}">
 <tr>
 <td>${item.name }</td>
 <td>${item.password }</td>
 <td></td>
 <td><a href="<%=basePath %>weather.do?method=edit&id=${item.id}">編輯</a><a href="<%=basePath %>weather.do?method=delete&id=${item.id}">刪除</a></td>
 </tr>
 </c:forEach>
 </tbody>
 </table>
 </div>
 </body>
 </html>

2.增加

dao中代碼

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

controller代碼,get方法是進入新增頁面,頁面?zhèn)鬟f空對象。post方法為添加新的記錄

@RequestMapping(params="method=add",method=RequestMethod.GET)
    public ModelAndView addWeather(HttpServletRequest request,HttpServletResponse reponse){
        request.setAttribute("weather", new WeatherPojo());
        return new ModelAndView("weatheradd");
    }
    @RequestMapping(params="method=add",method=RequestMethod.POST)
    public ModelAndView addWeather(WeatherPojo weather){
        weatherService.addWeather(weather);
        return new ModelAndView("redirect:/weather.do?method=query");
    }

jsp頁面代碼

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
        <%String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="<%=basePath%>weather.do?method=add" method="post">
<label for="id">id</label>
<input name="id"/><br>
<label for="name">name</label>
<input name="name"><br>
<label for="password">password</label>
<input name="password"><br>
<input type="submit" value="提交">
</form>
</body>
</html>

3.修改

dao中代碼:

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

controller代碼

@RequestMapping(params="method=edit",method=RequestMethod.GET)
    public ModelAndView editWeather(HttpServletRequest request,HttpServletResponse response){
        int id=Integer.valueOf(request.getParameter("id"));
        WeatherPojo weather=new WeatherPojo();
        weather=weatherService.getWeatherById(id);
        ModelAndView mav=new ModelAndView("editweather");
        request.setAttribute("weather", weather);
        System.out.println("--------"+weather.getId());
        System.out.println("--------"+weather.getName());
        System.out.println("--------"+weather.getPassword());
        return mav;
    }
    @RequestMapping(params="method=edit",method=RequestMethod.POST)
    public ModelAndView editWeather(WeatherPojo weather){
        weatherService.editWeather(weather);
        return new ModelAndView("redirect:/weather.do?method=query");
    }

jsp頁面:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
            <%String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="<%=basePath%>weather.do?method=edit" method="post">
<label for="id">id</label>
<input name="id" readonly="true" value="${weather.id }"/><br>
<label for="name">name</label>
<input name="name" value="${weather.name }"><br>
<label for="password" >password</label>
<input name="password" value="${weather.password }"><br>
<input type="submit" value="提交">
</form>
</body>
</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代碼:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<div><a href="<%=basePath %>weather.do?method=add">添加</a></div>
<div>
<table>
<thead>
<tr>
<th>姓名</th>
<th>說明</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="item" items="${weathers}">
<tr>
<td>${item.name }</td>
<td>${item.password }</td>
<td></td>
<td><a href="<%=basePath %>weather.do?method=edit&id=${item.id}">編輯</a><a href="<%=basePath %>weather.do?method=delete&id=${item.id}">刪除</a></td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</body>
</html>

 

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 国产婷婷丁香久久综合 | 日本不卡三区 | 免费一级a毛片免费观看欧美大片 | 精品国产亚洲一区二区三区 | 免费看一级a一片毛片 | 精品一区二区三区亚洲 | 国产妇乱子伦视频免费 | 亚洲综合久久久久久888 | 久久国产精品免费看 | 国产一级久久久久久毛片 | 毛片96视频免费观看 | 亚洲欧美日韩中文综合在线不卡 | 国产成人精品一区二区仙踪林 | 亚洲婷婷在线 | 免费黄色视屏网站 | 久9久9精品视频在线观看 | 四虎4hutv永久在线影院 | 99免费在线视频 | 亚洲精品国产综合99久久一区 | 色www国产阿娇 | 国产线路一 | 97影院理伦在线观看 | 国产激情一级毛片久久久 | 亚洲精品日本 | 国产亚洲综合精品一区二区三区 | 欧美精品一区二区三区观 | 国产高清区 | 在线中文字幕一区 | 四虎永久在线精品国产免费 | 国内精品免费一区二区三区 | 日本人一级毛片视频 | 国产欧美亚洲另类第一页 | 免费一级欧美片在线观看 | 91免费国产高清观看 | 精品精品国产高清a毛片 | 女人十八毛片免费特黄 | 免费一级黄色毛片 | 自拍偷自拍亚洲精品被多人伦好爽 | 欧美日韩一区二区高清免费视频 | 免费看黄色片视频 | 久草福利站 |