新浪微博 登陆  注册   设为首页 加入收藏

学PHP >> 数据库 >> SpringMVC整合Spring和Hibernate

SpringMVC整合Spring和Hibernate

查看次数4029 发表时间2013-06-10 00:00:58

一、SpringMVC简介Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,可以选择是使用内置的 ...

一、SpringMVC简介

Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,可以选择是使用内置的 Spring Web 框架还可以是 Struts 这样的 Web 框架。通过策略接口,Spring 框架是高度可配置的,而且包含多种视图技术,例如 JavaServer Pages(JSP)技术、Velocity、Tiles、iText 和 POI。Spring MVC 框架并不知道使用的视图,所以不会强迫您只使用 JSP 技术。Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。

Lifecycle for overriding binding, validation, etc.;易于同其它View框架(Tiles等)无缝集成,采用IOC便于测试。
它是一个典型的教科书式的mvc构架,而不像struts等都是变种或者不是完全基于mvc系统的框架,对于初学者或者想了解mvc的人来说我觉得 spring是最好的,它的实现就是教科书!第二它和tapestry一样是一个纯正的servlet系统,这也是它和tapestry相比 struts所没有的优势。而且框架本身有代码,而且看起来也不费劲比较简单可以理解。

二、源码案例

1、项目图



2、web.xml

<?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" 
		 xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
		 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"> 
  <display-name>json_test</display-name> 
  <welcome-file-list> 
    <welcome-file>login.jsp</welcome-file> 
  </welcome-file-list> 
   
  <!-- 加载所有的配置文件 --> 
  <context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath*:spring-*.xml</param-value> 
  </context-param> 
   
  <!-- 配置Spring监听 --> 
  <listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
  </listener> 
   
  <!-- 配置SpringMVC --> 
  <servlet> 
    <servlet-name>springMVC</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
        <param-name>contextConfigLocation</param-name> 
        <param-value>classpath*:spring-mvc.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
  </servlet> 
  <servlet-mapping> 
    <servlet-name>springMVC</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>
   
  <!-- 配置Session --> 
  <filter> 
    <filter-name>openSession</filter-name> 
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> 
  </filter> 
  <filter-mapping> 
    <filter-name>openSession</filter-name> 
    <url-pattern>/*</url-pattern> 
  </filter-mapping> 
  
  <welcome-file-list>
  	<welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app> 


3、spring-common.xml

<?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:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans.xsd"> 
     
    <!-- 配置数据源 --> 
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" > 
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 
        <property name="url" value="jdbc:mysql://localhost:3306/springmvc"></property> 
        <property name="username" value="root"></property> 
        <property name="password" value="bailong123"></property> 
    </bean> 
     
    <!-- 配置SessionFactory --> 
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
        <property name="dataSource" ref="dataSource" /> 
        <property name="hibernateProperties"> 
            <props> 
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
                <prop key="hibernate.hbm2ddl.auto">update</prop> 
                <prop key="hibernate.show_sql">true</prop> 
                <prop key="hibernate.format_sql">true</prop> 
            </props> 
        </property> 
        <property name="annotatedClasses"> 
            <list> 
                <value>com.tgb.entity.User</value> 
            </list> 
        </property> 
    </bean> 
    
    <!-- 配置hibernateTemplate实例 -->
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

	<!--配置一个JdbcTemplate实例 -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource" />
	</bean>
     
    <!-- 配置一个事务管理器 --> 
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
        <property name="sessionFactory" ref="sessionFactory"/> 
    </bean> 
     
    <!-- 配置事务,使用代理的方式 --> 
    <bean id="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">   
        <property name="transactionManager" ref="transactionManager"></property>   
        <property name="transactionAttributes">   
            <props>   
                <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>   
                <prop key="modify*">PROPAGATION_REQUIRED,-myException</prop>   
                <prop key="del*">PROPAGATION_REQUIRED</prop>   
                <prop key="*">PROPAGATION_REQUIRED</prop>   
            </props>   
        </property>   
    </bean>  
</beans> 



4、spring-mvc.xml

<?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:mvc="http://www.springframework.org/schema/mvc" 
    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-3.2.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> 
     
    <!-- 注解扫描包 --> 
    <context:component-scan base-package="com.tgb" /> 
 
    <!-- 开启注解 --> 
    <mvc:annotation-driven /> 
     
    <!-- 静态资源(js/image)的访问 --> 
    <mvc:resources location="/js/" mapping="/js/**"/> 
 
    <!-- 定义视图解析器 -->   
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <property name="prefix" value="/"></property> 
        <property name="suffix" value=".jsp"></property> 
    </bean> 
</beans> 



5、 User.java

package com.tgb.entity; 
 
import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
 
@Entity 
public class User implements Serializable { 
 
    /**
	 * 
	 */
	private static final long serialVersionUID = -850902142118911451L;

	@Id 
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id; 
    
    private String name;
    
    private String pwd;
    
    private String age;

	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 getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getAge() {
		return age;
	}

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



6、QueryResult.java

package com.tgb.utils;

import java.util.List;

public class QueryResult<T> {
	
	private List<T> resultlist;
	
	private Long totalrecord;

	public List<T> getResultlist() {
		return resultlist;
	}

	public void setResultlist(List<T> resultlist) {
		this.resultlist = resultlist;
	}

	public Long getTotalrecord() 
	{
		return totalrecord;
	}

	public void setTotalrecord(Long totalrecord) {
		this.totalre

(转发请注明转自:学PHP)    


  相关推荐




  发表评论
昵称:
(不超过20个字符或10个汉字)
内容: