ssh整合

1.导入hibernate jar包,配置hibernate.cfg.xml,ehcache配置,利用hibernate逆向工程生成两个domain类

<!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>
	<property name="show_sql">true</property>
	<property name="hbm2ddl.auto">update</property>
	<property name="myeclipse.connection.profile">Mysql</property>
	<!-- 
	<property name="connection.url">jdbc:mysql://127.0.0.1:3306/testj?useUnicode=true&amp;characterEncoding=utf-8</property>
	<property name="connection.username">root</property>
	<property name="connection.password">123456</property>
	<property name="connection.driver_class">
		com.mysql.jdbc.Driver
	</property>
	
	 -->
	<property name="dialect">
		org.hibernate.dialect.MySQLDialect
	</property>
	<!-- 3.0的 配置 -->
	<property name="hibernate.cache.use_second_level_cache">true</property>
	<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
	
	<!-- 4.0以后的配置 -->
	<!-- 
	<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
	 -->
	<!-- 查询缓存是相对于oid,普通缓存的是id -->
	<property name="cache.use_query_cache">true</property>

	
        <mapping resource="cn/domain/Department.hbm.xml" />
	<mapping resource="cn/domain/Employee.hbm.xml" />
	
	<class-cache usage="read-write" class="cn.domain.Department"/>
	<!-- 集合缓存的类,也要开启缓存 -->
	<class-cache usage="read-write" class="cn.domain.Employee"/>
	<collection-cache usage="read-write" collection="cn.domain.Department.employees"/>

	
</session-factory>
</hibernate-configuration>

 测试hibernate,新建单元测试类,加入如下代码

	private static SessionFactory sessionFactory=null;
	static{
		Configuration cfg=new Configuration().configure();
//        hibernate 4.0的用法
//        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();  
//        sessionFactory = cfg.buildSessionFactory(serviceRegistry);  
		sessionFactory= cfg.buildSessionFactory();
		System.out.println("statickkkkkk");
	}
	
	//测试hibernate,
	@Test
	public void test(){
		System.out.println(sessionFactory.openSession());
	}

 2.导入spring jar包,配置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: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-scan base-package="cn.web"></context:component-scan>


	<!-- 加载外部的properties配置文件 -->
	<context:property-placeholder location="classpath:jdbc.properties"/>


	  <!-- 配置数据库连接池(c3p0) -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<!-- 基本信息 -->
		<property name="jdbcUrl" value="${jdbcUrl}"></property>
		<property name="driverClass" value="${driverClass}"></property>
		<property name="user" value="${username}"></property>
		<property name="password" value="${password}"></property>
		<!-- 其他配置 -->
		<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
		<property name="initialPoolSize" value="3"></property>
		<!--连接池中保留的最小连接数。Default: 3 -->
		<property name="minPoolSize" value="3"></property>
		<!--连接池中保留的最大连接数。Default: 15 -->
		<property name="maxPoolSize" value="5"></property>
		<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
		<property name="acquireIncrement" value="3"></property>
		<!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
		<property name="maxStatements" value="8"></property>
		<!-- maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
		<property name="maxStatementsPerConnection" value="5"></property>
		<!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
		<property name="maxIdleTime" value="1800"></property>
	</bean>

	
	<!-- 配置SessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
	</bean>


	<!-- 配置声明式的事务管理(采用基于注解的方式) -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager"/>
	


</beans>

 测试spring 和hibernate整合 代码如下:

	//测试spring整合hibernate
	@Test
	public void test2(){
	      ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

	       SessionFactory sf = (SessionFactory) applicationContext.getBean("sessionFactory");
	       System.out.println(sf.openSession());
	       Session session= sf.openSession();
	       
	       Employee emp= (Employee) session.get(Employee.class, 5);
	       System.out.println(emp.getName());
	}

 3.spring 和struts2整合,导入struts2 jar包 struts.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" />
	<!-- 配置扩展名为action -->
	<constant name="struts.action.extension" value="action" />
	<constant name="struts.ui.theme" value="simple" />

	<package name="default" namespace="/" extends="struts-default">
		<!-- 测试用的action,当与Spring整合后,class属性写的就是Spring中bean的名称 -->
		<action name="test" class="testAction">
			<result name="success">/test.jsp</result>
		</action>

		<!-- 部門管理 
		<action name="departmentAction_*" class="departmentAction"
			method="{1}">
			<result name="list">/WEB-INF/jsp/departmentAction/list.jsp</result>
			<result name="saveUI">/WEB-INF/jsp/departmentAction/saveUI.jsp</result>
			<result name="toList" type="redirectAction">departmentAction_list?parentId=${parentId}
			</result>
		</action>
		-->

	</package>
</struts>

 测试:

package cn.web.test;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
//将action交给spring管理
@Controller
public class TestAction extends ActionSupport {
	@Resource
	private SessionFactory sessionFactory;
	@Override
	public String execute() throws Exception {

		Session session= sessionFactory.openSession();
		System.out.println(session);
		return "success";
	}
}

 注:spring 和struts整合要struts2-spring-pluging.jar

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值