最近S2SH框架整合和简单测试 Struts2-2.3.14+Spring-4.0.6+Hibernate-4.3.6

来源:互联网 发布:qq游戏大厅mac版下载 编辑:程序博客网 时间:2024/05/22 11:54
第一步引包:

Strut2:


Hibernate:


Spring:


Other:


 

第二步:

配置web.xml:

<web-app version="2.4"xmlns="http://java.sun.com/xml/ns/j2ee"

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

   <!-- 配置Spring的监听器,用于初始化ApplicationContext对象 -->

   <listener>

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

   </listener>

   <context-param>

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

      <param-value>classpath:applicationContext*.xml</param-value>

   </context-param>

   <filter>

      <filter-name>struts2</filter-name>

   <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

   </filter>

 

   <filter-mapping>

      <filter-name>struts2</filter-name>

      <url-pattern>/*</url-pattern>

   </filter-mapping>

 

   <welcome-file-list>

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

   </welcome-file-list>

 

</web-app>

 

Strut2:

<?xml version="1.0"encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC

    "-//ApacheSoftware Foundation//DTD Struts Configuration 2.0//EN"

    "http://struts.apache.org/dtds/struts-2.0.dtd">

 

<struts>

 

   <!-- 配置为开发模式 -->

   <constantname="struts.devMode"value="true"/>

   <!-- 配置扩展名为action -->

   <constantname="struts.action.extension"value="action"/>

   <packagename="default"namespace="/"extends="struts-default">

 

 

      <actionname="testAction"class="testAction">

        <resultname="success">/test.jsp</result>

      </action>

 

   </package>

</struts>

 

Spring:

<?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:tx="http://www.springframework.org/schema/tx"

   xsi:schemaLocation="http://www.springframework.org/schema/beans

   http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

   http://www.springframework.org/schema/context

   http://www.springframework.org/schema/context/spring-context-2.5.xsd

   http://www.springframework.org/schema/tx

   http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

 

 

   <!-- 自动扫描与装配bean -->

   <context:component-scanbase-package="cn.shu.oa"></context:component-scan>

 

 

   <!-- 加载外部的properties配置文件 -->

   <context:property-placeholderlocation="classpath:jdbc.properties"/>

 

   <!-- 配置数据库连接池(c3p0 -->

   <beanid="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource">

      <!-- 基本信息 -->

      <propertyname="jdbcUrl"value="${jdbcUrl}"></property>

      <propertyname="driverClass"value="${driverClass}"></property>

      <propertyname="user"value="${username}"></property>

      <propertyname="password"value="${password}"></property>

      <!-- 其他配置 -->

      <!--初始化时获取三个连接,取值应在minPoolSizemaxPoolSize之间。Default: 3 -->

      <propertyname="initialPoolSize"value="3"></property>

      <!--连接池中保留的最小连接数。Default: 3 -->

      <propertyname="minPoolSize"value="3"></property>

      <!--连接池中保留的最大连接数。Default: 15 -->

      <propertyname="maxPoolSize"value="5"></property>

      <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default:3 -->

      <propertyname="acquireIncrement"value="3"></property>

      <!-- 控制数据源内加载的PreparedStatements数量。如果maxStatementsmaxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->

      <propertyname="maxStatements"value="8"></property>

      <!-- maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->

      <propertyname="maxStatementsPerConnection"value="5"></property>

      <!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->

      <propertyname="maxIdleTime"value="1800"></property>

   </bean>

 

   <!-- 配置SessionFactory -->

   <beanid="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

      <propertyname="dataSource"ref="dataSource"></property>

      <propertyname="configLocation"value="classpath:hibernate.cfg.xml"></property>

   </bean>

 

   <!-- 配置声明式的事务管理(采用基于注解的方式) -->

   <beanid="transactionManager"class="org.springframework.orm.hibernate4.HibernateTransactionManager">

      <propertyname="sessionFactory"ref="sessionFactory"></property>

   </bean>

   <tx:annotation-driventransaction-manager="transactionManager"/>

 

</beans>

 

Hibernate:

<!DOCTYPE hibernate-configuration PUBLIC

   "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

   "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

 

<hibernate-configuration>

<session-factory>

 

   <!-- 数据库信息(连接信息写到spring的配置文件中) -->

   <propertyname="dialect">

      org.hibernate.dialect.MySQL5Dialect

   </property>

   <!--

      <propertyname="connection.url">jdbc:mysql:///itcastoa_20120216</property>

      <propertyname="connection.driver_class">com.mysql.jdbc.Driver</property>

      <propertyname="connection.username">root</property>

      <propertyname="connection.password">root</property>

   -->

 

   <!-- 其他配置 -->

   <propertyname="show_sql">true</property>

   <propertyname="hbm2ddl.auto">update</property>

 

   <!-- 导入映射配置 -->

   <mappingresource="cn/shu/oa/domain/User.hbm.xml"/>

 

 

 

</session-factory>

</hibernate-configuration>

 

Jdbc.proerties:

jdbcUrl    = jdbc\:mysql\:///test

driverClass = com.mysql.jdbc.Driver

username = root

password= zyy123

 

第三步写测试类和业务逻辑:

package cn.shu.oa.test;

 

importorg.hibernate.SessionFactory;

import org.junit.Test;

importorg.springframework.context.ApplicationContext;

importorg.springframework.context.support.ClassPathXmlApplicationContext;

 

public class SpringTest {

   privateApplicationContext ac = new ClassPathXmlApplicationContext(

        "applicationContext.xml");

 

   @Test

   publicvoid testSessionFactory() throws Exception {

      SessionFactorysessionFactory = (SessionFactory) ac

           .getBean("sessionFactory");

      System.out.println(sessionFactory);

   }

 

   @Test

   publicvoid testTransaction() throws Exception {

      TestServicetestService = (TestService) ac.getBean("testService");

      testService.saveTwoUsers();

   }

 

}

 

 

packagecn.shu.oa.test;

 

importjavax.annotation.Resource;

 

importorg.springframework.context.annotation.Scope;

importorg.springframework.stereotype.Controller;

 

importcom.opensymphony.xwork2.ActionSupport;

 

@Controller

@Scope("prototype")

public classTestAction extends ActionSupport {

     

      @Resource

      private TestService testService;

     

      @Override

      public String execute() throws Exception {

           System.out.println("----------->TestAction");

           testService.saveTwoUsers();

           return "success";

      }

 

}

 

packagecn.shu.oa.test;

 

importjavax.annotation.Resource;

 

importorg.hibernate.Session;

importorg.hibernate.SessionFactory;

importorg.springframework.stereotype.Service;

importorg.springframework.transaction.annotation.Transactional;

 

importcn.shu.oa.domain.User;

 

@Service("testService")

public classTestService {

      @Resource

      private SessionFactory sessionFactory;

     

      @Transactional

      public void saveTwoUsers(){

           Session session =sessionFactory.getCurrentSession();

           session.save(new User());

           session.save(new User());

      }

 

}

 

packagecn.shu.oa.domain;

 

publicclass User {

  

  

  

   publicLong getId() {

      returnid;

   }

   publicvoidsetId(Longid) {

      this.id = id;

   }

   public String getName() {

      returnname;

   }

   publicvoidsetName(String name) {

      this.name = name;

   }

   private Longid;

   private Stringname;

  

 

}

 

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/HibernateMapping DTD 3.0//EN"

        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

 

<hibernate-mappingpackage="cn.shu.oa.domain">

 

   <classname="User"table="shuoa_user">

      <idname="id">

            <generatorclass="native"/>

      </id>

      <propertyname="name"/>

   </class>

  

</hibernate-mapping>

 

结构框架如图:


第四步:

 

测试Spring:


测试Spring和Hibernate整合:


测试Spring,Hibernate和struts2整合

输入:http://localhost:8080/S2SH/test.action


结果如图:



 


程序源码:http://download.csdn.net/detail/yjl543986547/7846607
0 0
原创粉丝点击