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

  • Post author:
  • Post category:其他


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<Student> 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<Student> 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<Student> selectAllStudents();

}

<?xml version=”1.0″ encoding=”UTF-8″ ?>

<!DOCTYPE mapper

PUBLIC “-//mybatis.org//DTD Mapper 3.0//EN”

“http://mybatis.org/dtd/mybatis-3-mapper.dtd”>

<mapper namespace=”com.abc.dao.IStudentDao”>



<insert id=”insertStudent”>



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



</insert>






<delete id=”deleteStudentById”>



delete from student where id=#{xxx}



</delete>






<update id=”updateStudent”>



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



</update>






<select id=”selectStudentById” resultType=”Student”>



select id,name,age from student where id=#{xxx}



</select>






<select id=”selectAllStudents” resultType=”Student”>



select id,name,age from student



</select>

</mapper>

4.index登录页面发出请求

<%@ page language=”java” contentType=”text/html; charset=UTF-8″

pageEncoding=”UTF-8″%>

<!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=”${pageContext.request.contextPath }/registerServlet” method=”POST”>




姓名:<input type=”text” name=”name”/><br>



年龄:<input type=”text” name=”age”/><br>



<input type=”submit” value=”注册”/>



</form>

</body>

</html>

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下配置文件的更改

<?xml version=”1.0″ encoding=”UTF-8″?>

<web-app xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”



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″>






<welcome-file-list>



<welcome-file>index.jsp</welcome-file>



</welcome-file-list>





<!– 指定Spring配置文件的位置及名称 –>



<context-param>



<param-name>contextConfigLocation</param-name>



<param-value>classpath:resources/spring-*.xml</param-value>



</context-param>






<!– 注册监听器



监听器完成了两件重要工作:



1)在ServletContext初始化时创建了Spring容器



2)将创建好的Spring容器存放到了ServletContext全局域中



–>



<listener>



<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>



</listener>



<servlet>



<description></description>



<display-name>RegisterServlet</display-name>



<servlet-name>RegisterServlet</servlet-name>



<servlet-class>com.abc.servlets.RegisterServlet</servlet-class>



</servlet>



<servlet-mapping>



<servlet-name>RegisterServlet</servlet-name>



<url-pattern>/registerServlet</url-pattern>



</servlet-mapping>


</web-app>

<?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: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”>






<!– 注册DataSource:C3P0 –>



<bean id=”myDataSource” class=”com.mchange.v2.c3p0.ComboPooledDataSource”>



<property name=”driverClass” value=”${jdbc.driver}”/>



<property name=”jdbcUrl” value=”${jdbc.url}”/>



<property name=”user” value=”${jdbc.user}”/>



<property name=”password” value=”${jdbc.password}”/>



</bean>



<context:property-placeholder location=”classpath:resources/jdbc.properties”/>

</beans>






<!– 生成SqlSessionFactory –>



<bean id=”mySqlSessionFactory” class=”org.mybatis.spring.SqlSessionFactoryBean”>



<property name=”dataSource” ref=”myDataSource”/>



<property name=”configLocation” value=”classpath:resources/mybatis.xml”/>



</bean>






<!– 生成Dao –>



<bean id=”studentDao” class=”org.mybatis.spring.mapper.MapperFactoryBean”>



<property name=”sqlSessionFactory” ref=”mySqlSessionFactory”/>



<property name=”mapperInterface” value=”com.abc.dao.IStudentDao”/>



</bean>

</beans>

<!– 注册Service –>



<bean id=”studentService” class=”com.abc.service.StudentServiceImpl”>



<property name=”dao” ref=”studentDao”/>



</bean>

</beans>



<bean id=”myTransactionManager” class=”org.springframework.jdbc.datasource.DataSourceTransactionManager”>



<property name=”dataSource” ref=”myDataSource”/>



</bean>






<!– 注册事务通知 –>



<tx:advice id=”txAdvice” transaction-manager=”myTransactionManager”>



<tx:attributes>



<tx:method name=”save*” isolation=”DEFAULT” propagation=”REQUIRED”/>



<tx:method name=”remove*” isolation=”DEFAULT” propagation=”REQUIRED”/>



<tx:method name=”modify*” isolation=”DEFAULT” propagation=”REQUIRED”/>



<tx:method name=”find*” isolation=”DEFAULT” propagation=”REQUIRED” read-only=”true”/>



</tx:attributes>



</tx:advice>






<!– AOP配置 –>



<aop:config>



<aop:pointcut expression=”execution(* *..service.*.*(..))” id=”myPointcut”/>



<aop:advisor advice-ref=”txAdvice” pointcut-ref=”myPointcut”/>



</aop:config>




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



版权声明:本文为qq_39136928原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。