spring-web整合(学生类增删改查web的实现)

spring与web整合实现增删改查

在WEB-INF下导入jar包

ant-1.9.6.jar
ant-launcher-1.9.6.jar
asm-5.1.jar
cglib-3.2.4.jar
com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging-1.2.jar
javassist-3.21.0-GA.jar
log4j-1.2.17.jar
log4j-api-2.3.jar
log4j-core-2.3.jar
mybatis-3.4.2.jar
mybatis-spring-1.3.1.jar
mysql-connector-java-5.1.7-bin.jar
ognl-3.1.12.jar
slf4j-api-1.7.22.jar
slf4j-log4j12-1.7.22.jar
spring-aop-4.3.6.RELEASE.jar
spring-aspects-4.3.6.RELEASE.jar
spring-beans-4.3.6.RELEASE.jar
spring-context-4.3.6.RELEASE.jar
spring-core-4.3.6.RELEASE.jar
spring-expression-4.3.6.RELEASE.jar
spring-jdbc-4.3.6.RELEASE.jar
spring-tx-4.3.6.RELEASE.jar
spring-web-4.3.6.RELEASE.jar

(我导入jar包版本为4的  连接池c3p0,目前最火的为druid)


1.student类

package com.abc.beans;


public class Student {
private Integer id;
private String name;
private int age;


public Student() {
super();
// TODO Auto-generated constructor stub
}


public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}


public Integer getId() {
return id;
}


public void setId(Integer id) {
this.id = id;
}


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public int getAge() {
return age;
}


public void setAge(int age) {
this.age = age;
}


@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
}


}


2.service类以及实现类

package com.abc.service;


import java.util.List;


import com.abc.beans.Student;


public interface IStudentService {
void saveStudent(Student student);
void removeStudentById(int id);
void modifyStudent(Student student);

Student findStudentById(int id);
List findAllStudents();
}


package com.abc.service;


import java.util.List;


import com.abc.beans.Student;
import com.abc.dao.IStudentDao;


public class StudentServiceImpl implements IStudentService {
private IStudentDao dao;

public void setDao(IStudentDao dao) {
this.dao = dao;
}
                                                                     引入dao层。

@Override
public void saveStudent(Student student) {
dao.insertStudent(student);
}


@Override
public void removeStudentById(int id) {
dao.deleteStudentById(id);
}


@Override
public void modifyStudent(Student student) {
dao.updateStudent(student);
}


@Override
public Student findStudentById(int id) {
return dao.selectStudentById(id);
}


@Override
public List findAllStudents() {
return dao.selectAllStudents();
}


}

3.dao层,以及dao层的实现类,在映射文件mapper实现增删改查

package com.abc.dao;


import java.util.List;


import com.abc.beans.Student;


public interface IStudentDao {
void insertStudent(Student student);
void deleteStudentById(int id);
void updateStudent(Student student);

Student selectStudentById(int id);
List selectAllStudents();
}


PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">


insert into student(name,age) values(#{name}, #{age})



delete from student where id=#{xxx}



update student set name=#{name}, age=#{age} where id=#{id}






4.index登录页面发出请求

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here



姓名:

年龄:





5.servlet获取请求  将所传数据实现增的功能。


package com.abc.servlets;


import java.io.IOException;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;


import com.abc.beans.Student;
import com.abc.service.IStudentService;


public class RegisterServlet extends HttpServlet {


protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

// 1. 获取请求参数
String name = request.getParameter("name");
String ageStr = request.getParameter("age");
Integer age = Integer.valueOf(ageStr);

// 2. 将请求参数封装为Student对象
Student student = new Student(name, age);

// 3. 获取Spring容器对象
String key = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
WebApplicationContext ac = (WebApplicationContext) this.getServletContext().getAttribute(key);
System.out.println("ac = " + ac);

// 4. 从容器中获取Service对象
IStudentService service = (IStudentService) ac.getBean("studentService");

// 5. 调用Service的saveStudent()方法将对象写入到DB
service.saveStudent(student);


// 6. 页面跳转
// request.getRequestDispatcher("/welcome.jsp").forward(request, response);
response.sendRedirect(request.getContextPath() + "/welcome.jsp");

}
}

6.web下配置文件的更改


xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">


index.jsp





contextConfigLocation
classpath:resources/spring-*.xml


 

org.springframework.web.context.ContextLoaderListener





RegisterServlet
RegisterServlet
com.abc.servlets.RegisterServlet


RegisterServlet
/registerServlet





xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd">




















































spring-web整合(学生类增删改查web的实现)_第1张图片spring-web整合(学生类增删改查web的实现)_第2张图片


总结:对于web来说,路径较为麻烦,重点在与servlet编写以及配置方面较麻烦,这里我只实现了增save 的功能。



你可能感兴趣的:(spring,mybatis)