Spring与SpringMVC的整合

1、配置全局web.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <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_3_0.xsd" id="WebApp_ID" version="3.0">
 3   <display-name>SpringAndSpringMVC</display-name>
 4   <!-- Spring的配置,底层是个listener,读取application.xml文件 -->
 5     <context-param>
 6         <param-name>contextConfigLocation</param-name>
 7         <param-value>classpath:application.xml</param-value>
 8     </context-param>    
 9     <listener>
10         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
11     </listener>
12     <!-- SpringMVC的配置,底层是个servlet,读取springmvc.xml文件 -->
13     <servlet>
14         <servlet-name>springDispatcherServlet</servlet-name>
15         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
16         <init-param>
17             <param-name>contextConfigLocation</param-name>
18             <param-value>classpath:springmvc.xml</param-value>
19         </init-param>
20         <load-on-startup>1</load-on-startup>
21     </servlet>
22 
23     <!-- Map all requests to the DispatcherServlet for handling -->
24     <servlet-mapping>
25         <servlet-name>springDispatcherServlet</servlet-name>
26         <url-pattern>/</url-pattern>
27     </servlet-mapping>
28     <!-- 乱码处理 -->
29     <filter>
30     <filter-name>CharacterEncodingFilter</filter-name>
31     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
32     <init-param>
33       <param-name>encoding</param-name>
34       <param-value>UTF-8</param-value>
35     </init-param>
36   </filter>
37   <filter-mapping>
38     <filter-name>CharacterEncodingFilter</filter-name>
39     <url-pattern>/*</url-pattern>
40   </filter-mapping>
41   <!-- Rest风格的,将POST请求转化为DELETE,PUT -->
42   <filter>
43     <filter-name>HiddenHttpMethodFilter</filter-name>
44     <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
45   </filter>
46   <filter-mapping>
47     <filter-name>HiddenHttpMethodFilter</filter-name>
48     <url-pattern>/*</url-pattern>
49   </filter-mapping>
50   <welcome-file-list>
51     <welcome-file>index.html</welcome-file>
52     <welcome-file>index.htm</welcome-file>
53     <welcome-file>index.jsp</welcome-file>
54     <welcome-file>default.html</welcome-file>
55     <welcome-file>default.htm</welcome-file>
56     <welcome-file>default.jsp</welcome-file>
57   </welcome-file-list>
58 </web-app>

2、配置Spring的配置文件application.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:tx="http://www.springframework.org/schema/tx"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
 9         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
10         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
11 <!-- 扫描包,不扫描Controller和异常处理ControllerAdvice两个注解,交由SpringMVC的ioc容器管理 -->
12 <context:component-scan base-package="com.xy">
13 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
14 <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
15 </context:component-scan>
16 <!-- 加载配置文件 -->
17 <context:property-placeholder location="classpath:jdbc.properties"/>
18 <!-- 设置数据源 -->
19 <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
20         <property name="driverClass">
21             <value>${jdbc.driver}</value>
22         </property>
23         <property name="jdbcUrl">
24             <value>${jdbc.url}</value>
25         </property>
26         <property name="user">
27             <value>${jdbc.username}</value>
28         </property>
29         <property name="password">
30             <value>${jdbc.password}</value>
31         </property>  
32   </bean> 
33 <!-- 使用jdbcTemplate连接数据源,操作数据库 -->  
34  <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
35  <property name="dataSource" ref="comboPooledDataSource"></property> 
36   </bean>
37 </beans>

3、配置SpringMVC的配置文件springmvc.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:mvc="http://www.springframework.org/schema/mvc"
 7     xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
 8         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 9         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
10         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
11   <!-- 扫描包,管理Controller和异常处理 -->    
12   <context:component-scan base-package="com.xy" use-default-filters="false">
13   <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
14   <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
15   </context:component-scan>
16   <!-- 视图解析器的配置 -->
17   <bean
18         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
19         <!-- 配置jsp路径的前缀 -->
20         <property name="prefix" value="/WEB-INF/views/" />
21         <!-- 配置jsp路径的后缀 -->
22         <property name="suffix" value=".jsp" />
23     </bean>
24     <!-- 标配 -->
25     <mvc:default-servlet-handler/>
26     <!-- 静态资源的访问 -->
27     <mvc:annotation-driven/>
28    <!--  <mvc:view-controller path="/test" view-name="success"/> -->
29 </beans>

这样配置完之后变可以进行SS的操作

附带Dao层代码

package com.xy.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import com.xy.bean.Student;

@Repository
public class StudentDao {
    
     @Autowired
     private JdbcTemplate jdbcTemplate;
     private RowMapper<Student> rowMapper=new BeanPropertyRowMapper<Student>(Student.class);
    
     public List<Student> getStudentList(){
        
         String sql="SELECT * FROM users";       
         List<Student> list = jdbcTemplate.query(sql, rowMapper);
         return list;                 
         }
     public void deleteStudent(int id){
         String sql="DELETE FROM users WHERE id=?";
         jdbcTemplate.update(sql, id);         
         }
     /*public void insertStudent(Student stu){
         String sql="INSERT INTO users(`name`,`password`,'gender') VALUES(:name,:password,:gender)";
         SqlParameterSource source = new BeanPropertySqlParameterSource(stu);
         jdbcTemplate.update(sql, source);
         }*/
     public void insertStudent(Student stu){
         String sql="INSERT INTO users VALUES(NULL,?,?,?)";
         jdbcTemplate.update(sql, stu.getName(),stu.getPassword(),stu.getGender());
         }
     public Student findById(int id){
         String sql="SELECT * FROM users where id=?";
         Student student = jdbcTemplate.queryForObject(sql, rowMapper, id);
         return student;
         
     }
     public void updateStudent(Student stu){
         String sql = "UPDATE users SET name=?,password=?,gender=? WHERE id = ?";
         jdbcTemplate.update(sql, stu.getName(),stu.getPassword(),stu.getGender(),stu.getId());
         
         
     }
}

 

posted @ 2017-09-02 22:27  super_xueyi  阅读(229)  评论(0编辑  收藏  举报