日期:2014-05-16  浏览次数:20492 次

Spring+Spring MVC+JDBC增删改查小例子
此篇文章,也是作为入门时的一个小练习例子,使用的是JDBC完成的增删改查,相对比较简单一点,只要Spirng MVC的控制器部分熟悉就可以拿下了,另外现在练手是基于xml的方式配置实体类的,增删改查是分别对应一个控制器,实际上是完全可以放在一个控制器里面实现的,像Struts一样,这个后面散仙会基于注解的方式给出例子。


web.xml里面的配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	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_3_0.xsd">
  <display-name></display-name>	
  
  <servlet>
  <servlet-name>qin</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <!-- 配置拦截  action为后缀名的请求 -->
 <servlet-mapping>
  <servlet-name>qin</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
 
 <filter>
        <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

     
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

控制器的servlet里面的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- HandlerMapping -->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    
    <!-- HandlerAdapter -->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
 
<property name="mappings">
 
<props>
 
<prop key="save.do">save</prop>
<prop key="myadd">add</prop>
 
</props>
 
</property>
 
</bean>
    <!-- ViewResolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    
    <!-- 处理器 -->
    <bean name="/hellow" class="com.qin.controller.HellowController"/>
    
    <bean name="/indexAll" class="com.qin.controller.QueryAllUserController"></bean>
    
    <bean name="/delete" class="com.qin.controller.DeleteController"></bean>
  
    <bean name="/update" class="com.qin.controller.UpdateController"></bean>
   
    <bean id="save"   class="com.qin.controller.SaveController"></bean>
    <bean id="add"   class="com.qin.controller.AddControll