基於註解的struts2+spring+hibernate集成

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("[email protected]");
		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());
	}
	
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章