crud springmvc
实体类:Student.java
- package demo.entity;
- public class Student
- {
- private int id;
- private String name;
- private String sex;
- private String address;
- public int getId()
- {
- return id;
- }
- public void setId(int id)
- {
- this.id = id;
- }
- public String getName()
- {
- return name;
- }
- public void setName(String name)
- {
- this.name = name;
- }
- public String getSex()
- {
- return sex;
- }
- public void setSex(String sex)
- {
- this.sex = sex;
- }
- public String getAddress()
- {
- return address;
- }
- public void setAddress(String address)
- {
- this.address = address;
- }
- }
IStudentDao.java
- package demo.dao;
- import java.util.List;
- import demo.entity.Student;
- public interface IStudentDao
- {
- public void insert(Student stu);
- public void delete(int id);
- public void update(Student stu);
- public List<Student> findAll();
- public Student findById(int id);
- }
StudentDaoImpl.java
- package demo.dao.impl;
- import java.util.List;
- import java.util.Map;
- import javax.annotation.Resource;
- import org.springframework.jdbc.core.BeanPropertyRowMapper;
- import demo.dao.IStudentDao;
- import demo.entity.Student;
- public class StudentDaoImpl implements IStudentDao
- {
- public static final String INSERT = "insert into student(name,sex,address) values(?,?,?)";
- public static final String DELETE = "delete from student where id=?";
- public static final String UPDATE = "update student set name=?,sex=?,address=? where id=?";
- public static final String FINDALL = "select * from student";
- public static final String FINDBYID = "select * from student where id=?";
- private org.springframework.jdbc.core.simple.SimpleJdbcTemplate simpleJdbcTemplate;
- @Resource
- public void setSimpleJdbcTemplate(
- org.springframework.jdbc.core.simple.SimpleJdbcTemplate simpleJdbcTemplate)
- {
- this.simpleJdbcTemplate = simpleJdbcTemplate;
- }
- public void delete(int id)
- {
- // TODO Auto-generated method stub
- this.simpleJdbcTemplate.update(DELETE, id);
- }
- @SuppressWarnings("deprecation")
- public List<Student> findAll()
- {
- // TODO Auto-generated method stub
- List<Student> stuList = this.simpleJdbcTemplate.query(FINDALL,new BeanPropertyRowMapper<Student>(Student.class));
- return stuList;
- }
- public Student findById(int id)
- {
- // TODO Auto-generated method stub
- Map<String, Object> stuMap = this.simpleJdbcTemplate.queryForMap(FINDBYID, id);
- Student stu = new Student();
- stu.setId(id);
- stu.setName(stuMap.get("name").toString());
- stu.setSex(stuMap.get("sex").toString());
- stu.setAddress(stuMap.get("address").toString());
- return stu;
- }
- public void insert(Student stu)
- {
- // TODO Auto-generated method stub
- this.simpleJdbcTemplate.update(INSERT, stu.getName(),stu.getSex(),stu.getAddress());
- }
- public void update(Student stu)
- {
- // TODO Auto-generated method stub
- this.simpleJdbcTemplate.update(UPDATE, stu.getName(),stu.getSex(),stu.getAddress(),stu.getId());
- }
- }
IStudentService.java
- package demo.service;
- import java.util.List;
- import demo.entity.Student;
- public interface IStudentService
- {
- public void insert(Student stu);
- public void delete(int id);
- public void update(Student stu);
- public List<Student> findAll();
- public Student findById(int id);
- }
StudentServiceImpl.java
- package demo.service.impl;
- import java.util.List;
- import javax.annotation.Resource;
- import demo.dao.IStudentDao;
- import demo.entity.Student;
- import demo.service.IStudentService;
- public class StudentServiceImpl implements IStudentService
- {
- private IStudentDao stuDao;
- @Resource
- public void setStuDao(IStudentDao stuDao)
- {
- this.stuDao = stuDao;
- }
- public void delete(int id)
- {
- // TODO Auto-generated method stub
- stuDao.delete(id);
- }
- public List<Student> findAll()
- {
- // TODO Auto-generated method stub
- return stuDao.findAll();
- }
- public Student findById(int id)
- {
- // TODO Auto-generated method stub
- return stuDao.findById(id);
- }
- public void insert(Student stu)
- {
- // TODO Auto-generated method stub
- stuDao.insert(stu);
- }
- public void update(Student stu)
- {
- // TODO Auto-generated method stub
- stuDao.update(stu);
- }
- }
StudentController.java
- package demo.controller;
- import java.io.UnsupportedEncodingException;
- import java.util.List;
- import javax.annotation.Resource;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.ModelMap;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.servlet.ModelAndView;
- import demo.entity.Student;
- import demo.service.IStudentService;
- @Controller
- @RequestMapping("/student")
- public class StudentController
- {
- private IStudentService stuService;
- @Resource
- public void setStuService(IStudentService stuService)
- {
- this.stuService = stuService;
- }
- /**
- * 全部
- */
- @RequestMapping(value = "/all", method = RequestMethod.GET)
- public ModelAndView findAll(HttpServletRequest request,
- HttpServletResponse response, ModelMap modelMap)
- {
- List<Student> stuList = stuService.findAll();
- modelMap.put("stuList", stuList);
- return new ModelAndView("allStudent", modelMap);
- }
- /**
- * 删除
- */
- @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
- public String delete(HttpServletRequest request,
- HttpServletResponse response, @PathVariable("id")
- int id, ModelMap modelMap)
- {
- stuService.delete(id);
- return "redirect:/student/all.do";
- }
- /**
- * 添加
- */
- @RequestMapping(value = "/add", method = RequestMethod.POST)
- public String addStudent(HttpServletRequest request,
- HttpServletResponse response, Student stu)
- throws UnsupportedEncodingException
- {
- stuService.insert(stu);
- return "redirect:/student/all.do";
- }
- /**
- * pre4Update
- */
- @RequestMapping("/pre4Update/{id}")
- public ModelAndView pre4Update(HttpServletRequest request,
- HttpServletResponse response, @PathVariable("id")
- int id, ModelMap modelMap)
- {
- Student stu = stuService.findById(id);
- modelMap.put("stu", stu);
- return new ModelAndView("updateStudent", modelMap);
- }
- /**
- * updateStudent
- */
- @RequestMapping("/updateStudent")
- public String updateStudent(HttpServletRequest request,
- HttpServletResponse response, Student stu)
- {
- stuService.update(stu);
- return "redirect:/student/all.do";
- }
- /**
- * 跳转:用于跳转的过度
- */
- @RequestMapping(value = "/redir/{url}", method = RequestMethod.GET)
- public String sendRedirect(HttpServletRequest request,
- HttpServletResponse response, @PathVariable("url")
- String url)
- {
- return url;
- }
- }
================================================
配置文件:
jdbc.properties
- jdbc.driverClassName=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql://localhost:3306/demo?characterEncoding=utf-8
- jdbc.username=root
- jdbc.password=root
servlet.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
- <context:component-scan base-package="demo.controller"></context:component-scan>
- <!-- 内部资源视图解析器 (用于支持Servlet、JSP视图解析) -->
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/jsp/"/>
- <property name="suffix" value=".jsp"/>
- </bean>
- <!-- 数据库连接配置文件路径及读取方式 -->
- <bean id="propertyConfigurer"
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>classpath:jdbc.properties</value>
- </list>
- </property>
- </bean>
- <!-- 数据库连接池 -->
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName" value="${jdbc.driverClassName}" />
- <property name="url" value="${jdbc.url}" />
- <property name="username" value="${jdbc.username}" />
- <property name="password" value="${jdbc.password}" />
- </bean>
- <bean id="simpleJdbcTemplate"
- class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
- <constructor-arg><ref bean="dataSource"/></constructor-arg>
- </bean>
- <!-- spring JDBC -->
- <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
- <property name="dataSource" ref="dataSource" />
- </bean>
- </beans>
applicationContext-dao.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" " http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <bean id="stuDao" class="demo.dao.impl.StudentDaoImpl">
- <property name="simpleJdbcTemplate"
- ref="simpleJdbcTemplate" />
- </bean>
- </beans>
applicationContext-service.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" " http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <bean id="stuService" class="demo.service.impl.StudentServiceImpl">
- <property name="stuDao" ref="stuDao"/>
- </bean>
- </beans>
web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <servlet>
- <servlet-name>demo2</servlet-name>
- <servlet-class>
- org.springframework.web.servlet.DispatcherServlet
- </servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/classes/spring/*.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>demo2</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
- <!-- 处理乱码 -->
- <filter>
- <filter-name>encodingFilter</filter-name>
- <filter-class>
- org.springframework.web.filter.CharacterEncodingFilter
- </filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>utf-8</param-value>
- </init-param>
- <init-param>
- <param-name>forceEncoding</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>encodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- </web-app>
==================
allStudent.jsp
- <%@ page language="java" pageEncoding="utf-8"%>
- <%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="e"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>Student列表</title>
- </head>
- <table width="50%" border="1">
- <tr>
- <td>id</td>
- <td>姓名</td>
- <td>性别</td>
- <td>地址</td>
- <td>操作</td>
- </tr>
- <e:forEach items="${requestScope.stuList}" var="stu">
- <tr>
- <td>${stu.id}</td>
- <td>${stu.name}</td>
- <td>${stu.sex}</td>
- <td>${stu.address}</td>
- <td><a href="pre4Update/${stu.id}.do">修改</a>||<a href="delete/${stu.id}/${stu.name}.do">删除</a></td>
- </tr>
- </e:forEach>
- </table>
- <a href="redir/addStudent.do">添加</a>
- <body>
- </body>
- </html>
crud springmvc的更多相关文章
- 学习SpringMVC——你们要的REST风格的CRUD来了
来来来,让一下,客官,您要的REST清蒸CRUD来了,火候刚刚好,不油不腻,请慢用~~~ 如果说前面是准备调料,洗菜,切菜,摆盘,那么今天就来完整的上道菜,主要说的是基于REST风格实现数据的增删改查 ...
- 【SpringMVC】SpringMVC系列11之Restful的CRUD
11.Restful的CRUD 11.1.需求 11.2.POST转化为PUT.DELETE的fileter 11.3.查询所有 11.4.添加 11.5.删除 优雅的 REST 风格的资 ...
- 构建web应用之——SpringMVC实现CRUD
配置好SpringMVC最基本的配置后,开始实现处理数据的CRUD(CREATE, READ, UPDATE, DELETE) 为实现模块上的松耦合,我们将与数据库的交互任务交给DAO(Data Ac ...
- SpringMvc+Mybatis+Maven+Mysql做一个CRUD的简单例子
本文档结合 SpringMVC. Mybatis. MySQL,说明如何实现一个简单的数据库单表 CRUD操作.开发工具使用集成了spring mvc的eclipse(Spring Tool Suit ...
- SpringMVC 使用 RESTful 架构实现 CRUD 操作
软件152 余建强 源码下载:http://download.csdn.net/detail/qq_35318576/9826210 1 使用框架 SpringMVC.Maven.Ajax.JSTL. ...
- 21-spring学习-springMVC实现CRUD
结合业务层实现一共完成CRUD操作 1,定义一共IMessageServese接口 package com.SpringMVC.Service; import java.util.Map; impor ...
- 使用springmvc时报错org.springframework.beans.NullValueInNestedPathException: Invalid property 'department' of bean class [com.atguigu.springmvc.crud.entities.Employee]:
使用springmvc时报错 org.springframework.beans.NullValueInNestedPathException: Invalid property 'departmen ...
- SpringMVC 学习笔记(五) 基于RESTful的CRUD
1.1. 概述 当提交的表单带有_method字段时,通过HiddenHttpMethodFilter 将 POST 请求转换成 DELETE.PUT请求,加上@PathVariable注解从而实现 ...
- SpringMVC案例1——对User表进行CRUD操作
------------------------------------------------------------------web.xml--------------------------- ...
随机推荐
- Mysql 格式化日期格式
DATE_FORMAT(date, format) 根据格式串format 格式化日期或日期和时间值date,返回结果串. 可用DATE_FORMAT( ) 来格式化DATE 或DATETIME 值, ...
- server2008R2服务器开启远程
通常我们开启远程桌面只需要在我的电脑那边把远程的打开就可以了,然后允许访问 上面说的这些其实晚上很多都有的,自己找一下,这边是自己遇到的一点问题,就是弄了很久,按网上说的弄很久,发现问题原来是在,路由 ...
- WP开发笔记——去除 HTML 标签
获取到一段HTML类型的信息,显示在WP的webbrowser控件中,如果不加处理的话,会显示出各种神烦的HTML标签. 这时,需要我们将这HTML类型的信息进行处理去除HTML标签后再显示出来,这里 ...
- 身处IT的你对身边人都有哪些影响
前不久,跟外甥一起吃饭:他明年就要中考了,我就想,这马上就到人生的关键路口了,看他自己对将来有什么想法没:就问了句:勇勇,你以后想学习哪些方面的东西或者想从事什么工作呢?他简单的说了句:我要跟你一样学 ...
- phpStudy 2016 更新下载,新版支持php7.0
目标:让天下没有难配的php环境. phpStudy Linux版&Win版同步上线 支持Apache/Nginx/Tengine/Lighttpd/IIS7/8/6 『软件简介』该程序包集成 ...
- Stanford parser学习:LexicalizedParser类分析
上次(http://www.cnblogs.com/stGeekpower/p/3457746.html)主要是对应于javadoc写了下LexicalizedParser类main函数的功能,这次看 ...
- 51nod1270 数组的最大代价(简单dp)
---恢复内容开始--- 1270 数组的最大代价 题目来源: HackerRank 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 数组A包含N个 ...
- Win7构造wifi热点【Written By KillerLegend】
1:Win+R打开DOS,输入以下命令然后回车,不要关闭该DOS窗口: netsh wlan set hostednetwork mode = allow ssid =你的wifi热点名字 key ...
- mysql 的数据类型
mysql 的数据类型(描述的是字段)三大类:一.整型:1.tinyint(M),其中M是显示宽度,需要配合zerofill,就是前面0填充,存储单位为1个字节(8位),无符文是最大能存储范围0000 ...
- sql2012安装过程中出现个一个问题
最近安装了一次SQLSERVER2012,遇到了一个小问题,截图如下: 就是上图中状态为失败的项,点开之后,会弹出下面的一个框: 在网上搜了之后,有了这样的答案: http://www.cnblogs ...