码迷,mamicode.com
首页 > 编程语言 > 详细

基于注解的struts2+spring+hibernate集成

时间:2014-06-04 12:51:48      阅读:476      评论:0      收藏:0      [点我收藏+]

标签:struts2   spring   hibernate   ssh   

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: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.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

	<!-- 导入数据源的资源文件 -->
	<context:property-placeholder location="classpath:jdbc.properties"/>

	<!-- 配置自动扫描的包 -->
	<context:component-scan base-package="com.atguigu.survey.service, 
		com.atguigu.survey.action,
		com.atguigu.survey.dao">
	</context:component-scan>

	<!-- 配置 C3P0 数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${user}"></property>
		<property name="password" value="${password}"></property>
		<property name="driverClass" value="${driverClass}"></property>
		<property name="jdbcUrl" value="${jdbcUrl}"></property>
		
		<property name="minPoolSize" value="${minPoolSize}"></property>
		<property name="maxPoolSize" value="${maxPoolSize}"></property>
		<property name="initialPoolSize" value="${initialPoolSize}"></property>
		<property name="acquireIncrement" value="${acquireIncrement}"></property>
		<property name="maxStatements" value="${maxStatements}"></property>	
	</bean>
	
	<!-- 集成 Hibernate: 配置 SessionFactory 实例 -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<!-- 配置启用基于注解的事务支持 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
	
</beans>

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
		"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	
	<session-factory>
		
		<!-- Hibernate 的基本属性配置 -->
		<property name="show_sql">true</property>
		<property name="format_sql">true</property>

		<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
		<property name="hbm2ddl.auto">update</property>
		
		<property name="jdbc.batch_size">30</property>	
		<property name="jdbc.fetch_size">100</property>	
		
		<mapping resource="com/atguigu/survey/domain/User.hbm.xml"/>
			
	</session-factory>
	
</hibernate-configuration>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
	
	<!-- 关闭 Struts2 的动态方法调用的功能 userAciotn!save.action -->
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <!-- 打开开发调试模式, 使错误提示更加丰富, 但在项目提交时, 需关闭该功能 -->
    <constant name="struts.devMode" value="true" />
    <!-- 使用 simple 主题, 自定义布局, 而不使用 Struts2 默认提供的布局方式 -->
	<constant name="struts.ui.theme" value="simple"></constant>
	<!-- 配置使用的国际化资源文件 -->
	<constant name="struts.custom.i18n.resources" value="i18n"></constant>
	
    <package name="default" namespace="/" extends="struts-default">

		<action name="toPage_*">
			<result>/{1}.jsp</result>
		</action>
		
		<action name="UserAction_*" 
			class="com.atguigu.survey.action.UserAction" method="{1}">
			<exception-mapping result="input" 
				exception="com.atguigu.survey.exception.EmailIsUsedException"></exception-mapping>
			<result>/success.jsp</result>
			<result name="input">/reg.jsp</result>
		</action>
       
    </package>

</struts>

jdbc.properties文件内容:

user=root

driverClass=com.mysql.jdbc.Driver

jdbcUrl=jdbc:mysql:///survey3

password=root

minPoolSize=5

maxPoolSize=10

initialPoolSize=5

acquireIncrement=2

maxStatements=5


测试文件

package com.atguigu.survey.test;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.hibernate.SessionFactory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.atguigu.survey.domain.User;
import com.atguigu.survey.service.UserService;

public class HibernateTest {
	
	private ApplicationContext ctx= null;
	
	{
		ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
	}
	
	@Test
	public void testTransactional(){
		User user = new User();
		user.setMail("abcd@atguigu.com");
		user.setNickName("abcd");
		user.setPassword("123456");
		
		UserService userService = (UserService) ctx.getBean("userService");
		userService.save(user);
	}
	
	@Test
	public void testSessionFactory(){
		SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");
		System.out.println(sessionFactory.openSession());
	}
	
	@Test
	public void testDataSource() throws SQLException{
		DataSource dataSource = (DataSource) ctx.getBean("dataSource");
		System.out.println(dataSource.getConnection());
	}
	
}


基于注解的struts2+spring+hibernate集成,布布扣,bubuko.com

基于注解的struts2+spring+hibernate集成

标签:struts2   spring   hibernate   ssh   

原文地址:http://andyma.blog.51cto.com/2968761/1421563

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!