一.导入jar包
1. jar包地址:http://pan.baidu.com/s/1o7RjVJk
二。配置
1.hibernate.cfg.xml
<!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 name="foo">
<!-- 数据库信息 -->
<!-- mysql方言 -->
<!--
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/kjstoa</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
-->
<!-- 其他配置 -->
<!-- 显示sql语句 -->
<property name="show_sql">true</property>
<!-- 可以自动建表,可以自动更新表结构 -->
<property name="hbm2ddl.auto">update</property>
<!-- 声明映射文件 -->
<mapping resource="com/kjst/oa/model/user.hbm.xml" />
</session-factory>
</hibernate-configuration>
2.strtus.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 设定为开发模式 -->
<constant name="struts.devMode" value="true"/>
<!-- -->
<!-- -->
<package name="default" namespace="/" extends="struts-default">
<action name="testAction" class="com.kjst.oa.test.TestAction">
<result name="success"> test.jsp</result>
</action>
</package>
</struts>
3.applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvn="http://www.springframework.org/schema/mvn"
xsi:schemaLocation="
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/p http://www.springframework.org/schema/p/spring-p.xsd
http://www.springframework.org/schema/mvn http://www.springframework.org/schema/mvn/spring-mvn.xsd">
<!-- 自动扫描与装配bean,配置注解 -->
<context:component-scan base-package="com.kjst.oa" />
<!-- jdbc.properties位置 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置SessionFactory(与Hibernate整合) -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 配置hibernate的配置文件的位置 -->
<property name="configLocation" value="classpath:hibernate/hibernate.cfg.xml"/>
<!-- 配置数据源 -->
<property name="dataSource">
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 配置数据库连接基本信息 -->
<property name="jdbcUrl" value="${jdbcUrl}"/>
<property name="driverClass" value="${driverClass}"/>
<property name="user" value="${userName}"/>
<property name="password" value="${password}"/>
<!-- 配置其他 -->
<!-- 初始化时获取三个链接,去只在minPoolSize与maxPoolSize之间。default:3 -->
<property name="initialPoolSize" value="3" />
<!-- 链接池中保留的最小连接数,Default:3 -->
<property name="minPoolSize" value="3"/>
<!-- 链接池中保留的最大连接数。Default:15 -->
<property name="maxPoolSize" value="15"/>
<!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default:3 -->
<property name="acquireIncrement" value="3"/>
<!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存关闭。Default:0 -->
<property name="maxStatements" value="8"/>
<!-- maxStatementsPerConnection定义了两个链接池内单个连接所拥有的最大缓存statements数,Default:0 -->
<property name="maxStatementsPerConnection" value="5"/>
<!-- 最大空闲时间,1800秒内未使用则连接被丢弃。若为0则用不丢弃。Default:0 -->
<property name="maxIdleTime" value="1800"/>
</bean>
</property>
</bean>
<!-- 配置声明式事务,基于注解的方式 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 配置事务管理器 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
4.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" 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">
<display-name>kjstoa</display-name>
<!-- 配置spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置struts2核心过滤器 -->
<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.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
三.测试
1.测试Spring
package com.kjst.oa.test;
import org.hibernate.SessionFactory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring {
private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
/**
*
*测试sessionFactory
*/
@Test
public void testSessionFactory() {
SessionFactory sessionFactory = (SessionFactory) ac.getBean("sessionFactory");
System.out.println(sessionFactory.openSession());
}
//测试事务
@Test
public void testTransaction() {
// TODO Auto-generated method stub
TestService testService = (TestService) ac.getBean("testService");
testService.addUsers();
}
}
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.kjst.oa.model.User;
@Component("testService")
public class TestService {
@Resource
private SessionFactory sessionFactory;
@Transactional
public void addUsers(){
Session session = sessionFactory.getCurrentSession();
User user = new User();
user.setName("lisi");
user.setPassword("1111");
User user2 = new User();
user2.setName("zhangsan");
user2.setPassword("2222");
session.save(user);
// int a = 1/0;
session.save(user2);
}
}
public class User{
private int id;
private String name;
private String password;
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 getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.kjst.oa.model">
<class name="User" table="t_user">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name"></property>
<property name="password"></property>
</class>
</hibernate-mapping>
四.源码:http://pan.baidu.com/s/1hs3VLWg
整合的原因是最近心血来潮,想尝试整合Spring+struts+hibernate框架,后续还有ssm框架整合spring+struts+mybatis 或者spring+springmvc+mybatis
当然还有后续的JFinal框架整合的小Demo
效果: