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

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中代码:

  1. //delete
  2. public void deleteWeather(int id){
  3.  
  4. String sql="delete from weathertest where id="+id;
  5. jdbcTemplate.execute(sql);
  6. }

controller代码:

  1. @RequestMapping(params="method=delete",method=RequestMethod.GET)
  2. public ModelAndView deleteWeather(HttpServletRequest request,HttpServletResponse response){
  3. int id=Integer.valueOf(request.getParameter("id"));
  4. weatherService.deleteWeather(id);
  5. //页面重定向
  6. return new ModelAndView("redirect:/weather.do?method=query");
  7. }

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>

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. linux 虚拟机centos64位_6.5+VM10 主机是固定IP局域网设置代理上网,虚机设置固定ip 图文详细步骤

    一种: 虚机是Desktop 安装 1.虚拟机—设置—网络适配器子选项—选择“桥接模式” 2.在虚拟机中选择系统(System)—首选项(Preferences)—网络连接(Network Conne ...

  2. javaWeb RSA加密使用

      加密算法在各个网站运用很平常,今天整理代码的时候看到了我们项目中运用了RSA加密,就了解了一下. 先简单说一下RSA加密算法原理,RSA算法基于一个十分简单的数论事实:将两个大质数相乘十分容易,但 ...

  3. 1、java编程的建议,面试相关

    http://www.cnblogs.com/selene/p/5829605.html 面试相关:http://www.cnblogs.com/anrainie/p/5640208.html lin ...

  4. I Love You Too HDU 2816

    Description This is a true story. A man showed his love to a girl,but the girl didn't replied clearl ...

  5. [每日一题] OCP1z0-047 :2013-07-30 表连接――内联视图当作表使用

    用sys用户登录,给oe用户授权dba,以便可以用oe用户查看执行计划: oe@OCM> conn / as sysdba Connected. sys@OCM> grant dba to ...

  6. JavaScript中的一些细节

    1.设置id / class等属性 用 setAttribute 设置一些常规属性如 id ,className 的时候经常不起作用,只能用 object.id = value 这样来设置 news_ ...

  7. linux 文件查找和压缩工具

    文件查找 1,which命行查找可执行文件,which 只会搜索系统$PATH目录 2,whereis,查找可执行文件,并显示出此文件的man page文件,并且可以查找到系统的库目录 3,locat ...

  8. python 画正弦曲线

    要画正弦曲线先设定一下x的取值范围,从0到2π.要用到numpy模块. numpy.pi 表示π numpy.arange( 0 , 2π ,0.01)  从0到2π,以0.01步进. 令 x=num ...

  9. JavaWeb学习笔记--2.3内置对象

    参考资料:http://www.cnblogs.com/qqnnhhbb/archive/2007/10/16/926234.html 目录 1. JSP内置对象分类2. 属性保存范围 2.1 pag ...

  10. EF 一对一,一对多,多对多 Flunt API 配置

       一对一 就拿后台用户权限相关的实体来说明吧,用户表,用户详细表,是一对一的关系: /// <summary> /// 用户信息类 /// </summary> publi ...