本节主要介绍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>

list

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>

SpringMvc学习-增删改查的更多相关文章

  1. springMVC实现增删改查

    首先需要准备好一张数据库表我这里用emp这张表:具体代码: /* SQLyog 企业版 - MySQL GUI v8.14 MySQL - 5.1.73-community ************* ...

  2. Oracle的登陆问题和初级学习增删改查(省略安装和卸载)

    1:学习Oracle首先需要安装Oracle,网上已经有很多很多教程了,这里不做叙述,自己百度即可,这里安装的标准版,个人根据需求安装学习或者企业开发即可.如果安装出错,自己百度Oracle的卸载即可 ...

  3. springMVC之增删改查

    一.核心原理 1. 用于发送请求给server: /home.htm 2. 请求被DispatchServlet拦截到 3. DispatchServlet通过HandleMapping检查url有没 ...

  4. 基于SpringMVC的增删改查

    废话不多说,直接开始步骤! 1.创建一个Dynamic Web Project 2.在WEB-INF包下的lib文件夹中引入相关jar commons-logging-.jar jstl.jar sp ...

  5. 【SpringBoot】11-1.Springboot整合Springmvc+Mybatis增删改查操作(下)

    整合过程:https://www.isdxh.com/68.html 一.增--增加用户 1.创建实体类 package com.dxh.pojo; public class Users { priv ...

  6. 总结day5 ---- ,字典的学习,增删改查,以及字典的嵌套, 赋值运算

    内容大纲: 一:字典的定义 二:字典的增加 >1:按照key增加,  无则增加,有则覆盖 >2:setdefault()  ,无则增加,有则不变 三:字典的删除 >1:pop()  ...

  7. SpringMVC之简单的增删改查示例(SSM整合)

    本篇文章主要介绍了SpringMVC之简单的增删改查示例(SSM整合),这个例子是基于SpringMVC+Spring+Mybatis实现的.有兴趣的可以了解一下. 虽然已经在做关于SpringMVC ...

  8. 基于springmvc、ajax,后台连接数据库的增删改查

    前言 前段时间在博客园上找了一个springmvc的例子,照着学了一下,算是对springmvc有了一个初步的了解,打一个基础,下面是链接.(我只看了博客,视频太耗时间了) 博客链接:http://w ...

  9. idea+spring4+springmvc+mybatis+maven实现简单增删改查CRUD

    在学习spring4+springmvc+mybatis的ssm框架,idea整合简单实现增删改查功能,在这里记录一下. 原文在这里:https://my.oschina.net/finchxu/bl ...

随机推荐

  1. OC基础 类的三大特性

    OC基础  类的三大特性 OC的类和JAVA一样,都有三大特性:继承,封装,多态,那么我们就来看一下OC中类的三大特性. 1.继承 继承的特点: (1)子类从父类继承了属性和方法. (2)子类独有的属 ...

  2. poj3090欧拉函数求和

    E - (例题)欧拉函数求和 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     ...

  3. linux 下配置mysql区分大小写(不区分可能出现找不到表的情况)怎么样使用yum来安装mysql

    Linux 默认情况下,数据库是区分大小写的:因此,要将mysql设置成不区分大小写 在my.cof 设置 lower_case_table_names=1(1忽略大小写,0区分大小写) 检查方式:在 ...

  4. php中的MVC模式运用

    [size=5][color=Red]php中的MVC模式运用[/color][/size] 首先我来举个例子: 一个简单的文章显示系统 简单期间,我们假定这个文章系统是只读的,也就是说这个例子将不涉 ...

  5. MySQL 设置远程访问

    MySQL远程访问,也就是通过ip访问MySQL服务,MySQL对于安全的要求是非常严格的,需要授权. 1.本地访问 GRANT ALL PRIVILEGES ON *.* TO admin@loca ...

  6. 基于cygwin构建u-boot(四)libgcc引用

    接上文,config.mk文件修改后,即使没有.depend也可以正常处理了: 六.错误:gcclib引用错误 完成之前几篇的工作后,程序就可以一直执行了,直到最后生成u-boot, 出现如下错误告警 ...

  7. Python操作 Memcache、Redis、RabbitMQ、SQLAlchemy

    Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...

  8. python之pandas模块

    一.pandas模块是基于Numpy模块的,pandas的主要数据结构是Series和DadaFrame,下面引入这样的约定: from pandas import Series,DataFrame ...

  9. /users/products.:format 这种写法的其对应解析字符写法

    “products.:format" 这种写法可以有对应的下面两种路由形式 /products.json /products.xml "products.:format?" ...

  10. Liunx+C编程一站式学习

    Liunx+C编程一站式学习这本书有什么特点?面向什么样的读者?这本书最初是为某培训班的嵌入式系统Linux工程师就业班课程量身定做的教材之一.该课程是为期四个月的全日制职业培训,要求学员毕业时具备非 ...