SpringMVC增删改查功能的简单介绍 - 极悦
专注Java教育14年 全国咨询/投诉热线:444-1124-454
极悦LOGO图
始于2009,口口相传的Java黄埔军校
首页 hot资讯 SpringMVC增删改查功能的简单介绍

SpringMVC增删改查功能的简单介绍

更新时间:2021-12-09 12:39:20 来源:极悦 浏览915次

本节主要介绍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方法是进入新增页面,页面传递空对象。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>

 

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>