实体类:Student.java
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
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
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
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
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
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
Java代码
- jdbc.driverClassName=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql://localhost:3306/demo?characterEncoding=utf-8
- jdbc.username=root
- jdbc.password=root
servlet.xml
Java代码
- <?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
Java代码
- <?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
Java代码
- <?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
Java代码
- <?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
Java代码
- <%@ 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>
时间: 2024-10-14 00:22:39