java Spring-3.2.0+Struts-2.3.4+Hibernate-4.1.6整合<转>

我的前一篇博文讲了Spring-3.2.0+Struts2.3.4+JPA2.0整合,其所需的jar包与Spring-3.2.0 +Struts-2.3.4+Hibernate-4.1.6所需的jar包基本相同.下面列出SSH整合所需要的jar包.


 

 如上图所示是Spring-3.2.0 +Struts-2.3.4+Hibernate-4.1.6的jar包,这些jar包在各官网上都有提供下载:

Spring的jar包下载地址:http://www.springsource.org/spring-community-download(需要填写一些信息)

Hibernate的jar包下载地址:http://www.hibernate.org/downloads.html

Struts的jar包下载地址:http://struts.apache.org/download.cgi

 

注意在jar包整合,由于spring的jar已经相应独立开,还有其它jar也不同,所以要注意相关jar包的版本,不要以为包引多了没关系,jar冲突和版本不同也会导致整合失败.另外用MyEclipse(Eclipse)新建项目时会自动引入J2EE相关的jar包,建议删除,手动引入.

 

 

下面使用MySQL做演示:

首先我们在(src)下新建jdbc.properties文件用来配置数据库基本信息,其内容如下:

 

Xml代码   收藏代码
  1. jdbc.driverClassName=com.mysql.jdbc.Driver  
  2. jdbcjdbc.url=jdbc:mysql://127.0.0.1:3306/shopping  
  3. jdbc.username=root  
  4. jdbc.password=619100  

 

 

然后我们新建Spring配置文件beans.xml,将其放入在classpath(src)下,其内容如下:

 

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:context="http://www.springframework.org/schema/context"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop"  
  6.     xmlns:tx="http://www.springframework.org/schema/tx"  
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
  9.            http://www.springframework.org/schema/context  
  10.            http://www.springframework.org/schema/context/spring-context-3.2.xsd  
  11.            http://www.springframework.org/schema/aop  
  12.            http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
  13.            http://www.springframework.org/schema/tx  
  14.            http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">  
  15.       
  16.     <context:annotation-config/>  
  17.     <!-- 声明Spring要管理的范围 -->  
  18.     <context:component-scan base-package="gd.hz.shopping" />  
  19.       
  20.     <!-- 读取jdbc.properties配置文件 -->  
  21.     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  22.         <property name="locations">  
  23.             <value>classpath:jdbc.properties</value>  
  24.         </property>  
  25.     </bean>  
  26.     <bean id="dataSource" destroy-method="close"  
  27.         class="org.apache.commons.dbcp.BasicDataSource">  
  28.         <property name="driverClassName"  
  29.             value="${jdbc.driverClassName}" />  
  30.         <property name="url" value="${jdbc.url}" />  
  31.         <property name="username" value="${jdbc.username}" />  
  32.         <property name="password" value="${jdbc.password}" />  
  33.     </bean>  
  34.   
  35.     <!-- 创建sessionFactory -->  
  36.     <bean id="sessionFactory"  
  37.         class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
  38.         <property name="dataSource" ref="dataSource" />  
  39.         <property name="packagesToScan">  
  40.             <list>  
  41.                 <!-- 需要映射的实体类,指定到实体类所在的包即可 -->  
  42.                 <value>gd.hz.shopping.model</value>  
  43.             </list>  
  44.         </property>  
  45.         <property name="hibernateProperties">  
  46.             <props>  
  47.                 <prop key="hibernate.format_sql">true</prop>  
  48.               
  49.                 <prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>  
  50.                   
  51.                 <prop key="hibernate.show_sql">true</prop>  
  52.                   
  53.                 <prop key="current_session_context_class">thread</prop>  
  54.                   
  55.                 <prop key="connection.pool_size">3</prop>  
  56.                   
  57.                 <prop key="hibernate.hbm2ddl.auto">update</prop>  
  58.                   
  59.                 <prop key="javax.persistence.validation.mode">none</prop>  
  60.                   
  61.                 <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>  
  62.             </props>  
  63.         </property>  
  64.     </bean>  
  65.       
  66.     <!-- 开启事务管理 -->  
  67.     <bean id="txManager"  
  68.         class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
  69.         <property name="sessionFactory" ref="sessionFactory" />  
  70.     </bean>  
  71.     <aop:config>  
  72.         <aop:pointcut id="bussinessService"  
  73.             expression="execution(public * gd.hz.shopping.service.*.*(..))" />  
  74.         <aop:advisor pointcut-ref="bussinessService"  
  75.             advice-ref="txAdvice" />  
  76.     </aop:config>  
  77.       
  78.     <!-- 使用声明式事务 -->  
  79.     <tx:advice id="txAdvice" transaction-manager="txManager">  
  80.         <tx:attributes>  
  81.             <tx:method name="find*" read-only="true" propagation="REQUIRED" />  
  82.             <tx:method name="save*" propagation="REQUIRED"/>  
  83.             <tx:method name="modify*" propagation="REQUIRED"/>  
  84.             <tx:method name="remove*" propagation="REQUIRED"/>  
  85.         </tx:attributes>  
  86.     </tx:advice>  
  87.        
  88. </beans>  

 

 

 

新建我们的struts配置文件struts.xml,内容如下:

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">  
  3.   
  4. <struts>  
  5.     <constant name="struts.devMode" value="true" />  
  6.     <constant name="struts.i18n.encoding" value="UTF-8" />  
  7. </struts>  
  8.       

 

 

 

然后配置我们的web.xml文件:

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  5.     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  
  6.   
  7.     <!-- 加载Spring配置文件 -->  
  8.     <context-param>  
  9.         <param-name>contextConfigLocation</param-name>  
  10.         <param-value>classpath:beans.xml</param-value>  
  11.     </context-param>  
  12.       
  13.     <!-- ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。 -->  
  14.     <listener>  
  15.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  16.     </listener>  
  17.   
  18.     <!-- 指定字符集 -->  
  19.     <filter>  
  20.         <filter-name>encodingFilter</filter-name>  
  21.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  22.         <init-param>  
  23.             <param-name>encoding</param-name>  
  24.             <param-value>UTF-8</param-value>  
  25.         </init-param>  
  26.     </filter>  
  27.     <filter-mapping>  
  28.         <filter-name>encodingFilter</filter-name>  
  29.         <url-pattern>/*</url-pattern>  
  30.     </filter-mapping>  
  31.   
  32.     <!-- 保持session到页面 -->  
  33.     <filter>  
  34.         <filter-name>openSessionInView</filter-name>  
  35.         <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>  
  36.         <init-param>  
  37.             <param-name>sessionFactoryBeanName</param-name>  
  38.             <param-value>sessionFactory</param-value>  
  39.         </init-param>  
  40.     </filter>  
  41.     <filter-mapping>  
  42.         <filter-name>openSessionInView</filter-name>  
  43.         <url-pattern>/*</url-pattern>  
  44.     </filter-mapping>  
  45.   
  46.     <!-- 声明struts管理请求 -->  
  47.     <filter>  
  48.         <filter-name>struts2</filter-name>  
  49.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  50.     </filter>  
  51.     <filter-mapping>  
  52.         <filter-name>struts2</filter-name>  
  53.         <url-pattern>/*</url-pattern>  
  54.     </filter-mapping>  
  55.   
  56. </web-app>  

 

 

 

最后我们做一个示例:

新建实体类:

 

Java代码   收藏代码
  1. package gd.hz.shopping.model;  
  2.   
  3. import javax.persistence.Entity;  
  4. import javax.persistence.GeneratedValue;  
  5. import javax.persistence.GenerationType;  
  6. import javax.persistence.Id;  
  7.   
  8. @Entity  
  9. public class ProductType {  
  10.     private int productId ;  
  11.       
  12.     private String name ;  
  13.   
  14.     @Id  
  15.     @GeneratedValue(strategy=GenerationType.AUTO)  
  16.     public int getProductId() {  
  17.         return productId;  
  18.     }  
  19.     public void setProductId(int productId) {  
  20.         this.productId = productId ;  
  21.     }  
  22.       
  23.     public String getName() {  
  24.         return name;  
  25.     }  
  26.     public void setName(String name) {  
  27.         this.name = name;  
  28.     }  
  29. }  

 

 

 

新建Dao接口:

 

Java代码   收藏代码
  1. package gd.hz.shopping.dao;  
  2.   
  3. import gd.hz.shopping.model.ProductType;  
  4.   
  5. public interface ProductTypeDao {  
  6.       
  7.     public ProductType selProductType(int id);  
  8. }  

 

 

实现Dao接口:

 

Java代码   收藏代码
  1. package gd.hz.shopping.dao.impl;  
  2.   
  3. import javax.annotation.Resource;  
  4.   
  5. import org.hibernate.Session;  
  6. import org.hibernate.SessionFactory;  
  7. import org.springframework.stereotype.Component;  
  8.   
  9. import gd.hz.shopping.dao.ProductTypeDao;  
  10. import gd.hz.shopping.model.ProductType;  
  11.   
  12. @Component("productTypeDao")  
  13. public class ProductTypeDaoImpl implements ProductTypeDao {  
  14.       
  15.     private SessionFactory sessionFactory = null ;  
  16.       
  17.     @Resource(name="sessionFactory")  
  18.     public void setSessionFactory(SessionFactory sessionFactory) {  
  19.         this.sessionFactory = sessionFactory;  
  20.     }  
  21.   
  22.     @Override  
  23.     public ProductType selProductType(int id) {  
  24.         Session session = sessionFactory.getCurrentSession() ;  
  25.         ProductType productType = (ProductType)session.get(ProductType.class , id);  
  26.         return productType ;  
  27.     }  
  28. }  

 

 

Manager层接口:

 

Java代码   收藏代码
  1. package gd.hz.shopping.service;  
  2.   
  3. import gd.hz.shopping.model.ProductType;  
  4.   
  5. public interface ProductTypeMnager {  
  6.     public ProductType findProductType(int id) ;  
  7. }  

 

 

 

Manager层接口实现:

 

Java代码   收藏代码
  1. package gd.hz.shopping.service.impl;  
  2.   
  3. import javax.annotation.Resource;  
  4.   
  5. import org.springframework.stereotype.Component;  
  6.   
  7. import gd.hz.shopping.dao.ProductTypeDao;  
  8. import gd.hz.shopping.model.ProductType;  
  9. import gd.hz.shopping.service.ProductTypeMnager;  
  10.   
  11. @Component("productTypeMnager")  
  12. public class ProductTypeMnagerImpl implements ProductTypeMnager {  
  13.   
  14.     private ProductTypeDao productTypeDao = null ;  
  15.       
  16.     @Resource(name="productTypeDao")  
  17.     public void setProductTypeDao(ProductTypeDao productTypeDao) {  
  18.         this.productTypeDao = productTypeDao;  
  19.     }  
  20.   
  21.     @Override  
  22.     public ProductType findProductType(int id) {  
  23.         return productTypeDao.selProductType(id) ;  
  24.     }  
  25.   
  26. }  

 

 

 

 

 新建控制类ProductTypeAction:

 

Java代码   收藏代码
  1. package gd.hz.shopping.action;  
  2.   
  3. import javax.annotation.Resource;  
  4.   
  5. import gd.hz.shopping.model.ProductType;  
  6. import gd.hz.shopping.service.ProductTypeMnager;  
  7.   
  8. import org.springframework.context.annotation.Scope;  
  9. import org.springframework.stereotype.Component;  
  10.   
  11. import com.opensymphony.xwork2.ActionSupport;  
  12.   
  13. @Component("productTypeAction")  
  14. @Scope("prototype")  
  15. public class ProductTypeAction extends ActionSupport {  
  16.       
  17.     private static final long serialVersionUID = 1L;  
  18.       
  19.     private ProductTypeMnager productTypeMnager = null ;  
  20.     private ProductType productType = null ;  
  21.       
  22.     @Resource(name="productTypeMnager")  
  23.     public void setProductTypeMnager(ProductTypeMnager productTypeMnager) {  
  24.         this.productTypeMnager = productTypeMnager;  
  25.     }  
  26.       
  27.     public ProductType getProductType() {  
  28.         return productType;  
  29.     }  
  30.     public void setProductType(ProductType productType) {  
  31.         this.productType = productType;  
  32.     }  
  33.       
  34.     public String test()  
  35.     {  
  36.         productType = productTypeMnager.findProductType(1) ;  
  37.         return "test" ;  
  38.     }  
  39. }  

 

 

 

 

配置struts.xml文件:

Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">  
  3.   
  4. <struts>  
  5.     <constant name="struts.devMode" value="true" />  
  6.     <constant name="struts.i18n.encoding" value="UTF-8" />  
  7.       
  8.     <package name="productType" namespace="/" extends="OA_default">  
  9.         <action name="productTypeAction" class="productTypeAction">  
  10.             <result name="test">/org/add_input.jsp</result>  
  11.         </action>  
  12.     </package>  
  13. </struts>  
  14.       

 

启动服务器,然后手动插入测试数据:

insert into ProductType(name) values('lfd');



 

 

新建jsp页面:

引入jstl 标签:<%@ taglib prefix="s"  uri="/struts-tags" %>

 

Java代码   收藏代码
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <%@ taglib prefix="s"  uri="/struts-tags" %>   
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  8. <title>ssh整合测试</title>  
  9. </head>  
  10. <body>  
  11.   <%--使用OGNL表达式--%>    
  12.    Hello:<s:property value="productType.name"/><br>   
  13. </body>  
  14. </html>  

 

显示为:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值