最近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>
<!-- 其他配置 -->
<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。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数量。如果maxStatements与maxStatementsPerConnection均为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
- 最近S2SH框架整合和简单测试 Struts2-2.3.14+Spring-4.0.6+Hibernate-4.3.6
- (struts2+spring+hibernate)s2sh整合
- S2SH框架整合(注解)Struts2+Spring+Hibernate+MySql
- SSH框架整合(Spring整合Struts2,和Spring整合Hibernate)
- s2sh框架整合所需包,struts2+spring+hibernate所需的jar包下载
- spring和mybatis ,hibernate, struts2框架整合
- struts2+hibernate+spring框架整合
- Struts2+Hibernate+spring框架整合
- hibernate+struts2+spring框架整合
- 整合开发Struts2,Hibernate,Spring简单框架的搭建详解
- 基于Maven的S2SH(Struts2+Spring+Hibernate)框架搭建
- struts2+spring+hibernate s2sh结合方式框架搭建开发
- 基于maven的S2SH(Struts2+Spring+Hibernate)的框架DEMO
- S2SH---(Struts2+Spring+Hibernate)
- 最新版SSH框架简单整合(Struts 2.3.15.2 + Spring 4.0.0 + Hibernate 4.2.6)与整合手记
- S2SH框架集成(Struts 2.3.16 + Spring 3.2.6 + Hibernate 3.6.10)
- Struts2,Hibernate和Spring之间的框架整合
- spring,hibernate和struts2三大框架整合初印象
- Android SDK下载和更新失败的解决方法
- ExecutorSevice & FutureTask
- 这一年的八月三十一号
- 过河
- 【Python】爬图片,Py2exe打包成EXE,并用inno setup生成安装文件
- 最近S2SH框架整合和简单测试 Struts2-2.3.14+Spring-4.0.6+Hibernate-4.3.6
- TableLayout 实现动态新增一行,与之前行宽度一致
- vector的clear操作的内部过程
- 字符转换
- BZOJ 1708: [Usaco2007 Oct]Money奶牛的硬币
- [UVA]11524 - InCircle
- URAL 1019. Line Painting 线段树 区间合并 离散化
- Codeforces 319B. Psychos in a Line【栈】
- linux中命令的返回值