首页 > 代码库 > 基于注解的struts2+spring+hibernate集成
基于注解的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="http://www.mamicode.com/${user}"></property> <property name="password" value="http://www.mamicode.com/${password}"></property> <property name="driverClass" value="http://www.mamicode.com/${driverClass}"></property> <property name="jdbcUrl" value="http://www.mamicode.com/${jdbcUrl}"></property> <property name="minPoolSize" value="http://www.mamicode.com/${minPoolSize}"></property> <property name="maxPoolSize" value="http://www.mamicode.com/${maxPoolSize}"></property> <property name="initialPoolSize" value="http://www.mamicode.com/${initialPoolSize}"></property> <property name="acquireIncrement" value="http://www.mamicode.com/${acquireIncrement}"></property> <property name="maxStatements" value="http://www.mamicode.com/${maxStatements}"></property> </bean> <!-- 集成 Hibernate: 配置 SessionFactory 实例 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="configLocation" value="http://www.mamicode.com/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="http://www.mamicode.com/false" /> <!-- 打开开发调试模式, 使错误提示更加丰富, 但在项目提交时, 需关闭该功能 --> <constant name="struts.devMode" value="http://www.mamicode.com/true" /> <!-- 使用 simple 主题, 自定义布局, 而不使用 Struts2 默认提供的布局方式 --> <constant name="struts.ui.theme" value="http://www.mamicode.com/simple"></constant> <!-- 配置使用的国际化资源文件 --> <constant name="struts.custom.i18n.resources" value="http://www.mamicode.com/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()); } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。