hibernate4+spring3+struts2搭建框架实例

1.所需要的JAR包

2.web.xml配置文件,这个和平时的配置是一样的

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  <!-- spring -->
  <context-param>
  	<param-name>springConfigLocation</param-name>
  	<param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>
  	<!-- contentloader -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

  <!-- struts -->
  <filter>
  	<filter-name>struts</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter>
  	<filter-name>struts-cleanup</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
  </filter>

  <filter-mapping>
		<filter-name>struts</filter-name>
		<url-pattern>*.do</url-pattern>
		<dispatcher>REQUEST</dispatcher>
		<dispatcher>FORWARD</dispatcher>
	</filter-mapping>

	<filter-mapping>
		<filter-name>struts-cleanup</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

3.applicationContex.xml文件配置,sessionFactory的配置类是:org.springframework.orm.hibernate4.LocalSessionFactoryBean

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	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"
	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/aop
        http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	<!-- onload hibernate.properties file -->
	<bean id="propertyConfig"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
        	<value>/WEB-INF/jdbc.properties</value>
        </property>
	</bean>

    <!--c3p0 连接池-->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
		<property name="driverClass">
			<value>${hibernate.connection.driver_class}</value>
		</property>
		<property name="jdbcUrl">
			<value>${hibernate.connection.url}</value>
		</property>
			<property name="user">
		 		<value>${hibernate.connection.username}</value>
			</property>
		<property name="password">
			<value>${hibernate.connection.password}</value>
		</property>

		<!-- 连接池中保留的最小连接数. -->
		<property name="minPoolSize">
			<value>5</value>
		</property>
		<!-- 连接池中保留的最大连接数。Default: 15 -->
		<property name="maxPoolSize">
		 <value>100</value>
		</property>
		<!-- 初始化时获得的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
		<property name="initialPoolSize">
		 <value>5</value>
		</property>
		<!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
		<property name="maxIdleTime">
		 <value>60</value>
		</property>
		<!-- 当连接池中的连接耗尽的时候c3p0一次同时获得的连接数。Default: 3 -->
		<property name="acquireIncrement">
		 <value>3</value>
		</property>
	</bean>

	<!--create sessionFactory-->
	<bean id="sessionFactory"  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<!-- config hibernate Field -->
		<property name="hibernateProperties">
		<props>
		<prop key="hibernate.dialect"> ${hibernate.dialect}</prop>
		<prop key="hibernate.generate_statistics">true</prop>
		<prop key="hibernate.jdbc.fetch_size"> ${hibernate.jdbc.fetch_size}</prop>
		<prop key="hibernate.jdbc.batch_size"> ${hibernate.jdbc.batch_size}</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
                <prop key="hibernate.bytecode.use_reflection_optimizer">false</prop>
                <prop key="hibernate.query.substitutions">true</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.max_fetch_depth">3</prop>
                <prop key="hibernate.hibernate.use_outer_join">true</prop>
                <prop key="hibernate.jdbc.batch_size">50</prop>
		</props>
		</property>
		<!--mapping hibernate model po class-->
		<property name="mappingDirectoryLocations">
			<list>
				<value>/WEB-INF/classes/com/fit/core/pojo</value>
			</list>
		</property>
	</bean>

	<!--config transaction manager-->
	<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>   

	<bean id="basedao" class="com.fit.core.dao.impl.BaseDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<bean id="basebean" class="com.fit.core.bean.impl.BaseBeanImpl">
		<property name="basedao" ref="basedao"></property>
	</bean>

</beans>

4.jdbc.properties配置文件

  

5.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>
	<package name="default" extends="struts-default">
		<action name="findUserList" class = "com.fit.core.action.UserAction" method="findUserList">
			<result name="success">/WEB-INF/jsp/list.jsp</result>
		</action>
	</package>
</struts>

6.Daoimp.java

package com.fit.core.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.fit.core.dao.BaseDao;

public class BaseDaoImpl implements BaseDao {
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public boolean deleteObject(Object object) {
		boolean flag = false;
		try{
			Session session = sessionFactory.openSession();
			session.delete(object);
			flag = true;
		}catch(Exception e ){
			e.printStackTrace();
		}
		return flag;
	}	

	public Object findObject(Class c, int id) {
		boolean flag = false;
		try{
			Session session = sessionFactory.openSession();
			session.get(c, id);
			flag = true;
		}catch(Exception e ){
			e.printStackTrace();
		}
		return flag;
	}

	public List listQuery(String hql) {
		// TODO Auto-generated method stub
		List list = null ;
		try{
			Session session = sessionFactory.openSession();
			list = session.createQuery(hql).list();
		}catch(Exception e){
			e.printStackTrace();
		}
		return list;
	}

	public boolean save(Object obj) {
		boolean flag = false;
		try{
			Session session = sessionFactory.openSession();
			session.save(obj);
			flag = true;
		}catch(Exception e ){
			e.printStackTrace();
		}
		return flag;
	}

	public boolean updateObject(Object obj) {
		boolean flag = false;
		try{
			Session session = sessionFactory.openSession();
			session.update(obj);
			flag = true;
		}catch(Exception e ){
			e.printStackTrace();
		}
		return flag;
	}

	public boolean updateObject(String hql) {
		boolean flag = false;
		Session session = sessionFactory.openSession();
		Query query = session.createQuery(hql);
		if(query != null){
			int a = query.executeUpdate();
			if(a > 0){
				flag = true;
				session.getTransaction().commit();//提交事物
			}
		}
		return flag;
	}

}

  

  

时间: 2024-08-09 10:44:30

hibernate4+spring3+struts2搭建框架实例的相关文章

SpringMVC+Spring3+hibernate4 开发环境搭建以及一个开发实例教程

刚刚接触了SpringMVC这个框架,因此有必要把它拿过来同hibernate.Spring框架进行集成和开发一个实例,在真正企业从头开发的项目中往往一个稳定的开发环境至关重要,开发一个项目选择什么样的搭建平台也需要有经验才可以,并不是说你会搭建一个开发平台然后公司就会用你搭建的开发平台,一个项目或者一个公司看中的也不是你可以搭出框架,而是在这个框架使用过程中出现的各种问题你可以解决掉. 也就是说呢,无论开发什么项目要做到稳定.环境稳定.开发成本稳定.技术稳定.换句话说就是"风险可控"

SpringMVC笔记——SSM框架搭建简单实例

落叶枫桥 博客园 首页 新随笔 联系 订阅 管理 SpringMVC笔记——SSM框架搭建简单实例 简介 Spring+SpringMVC+MyBatis框架(SSM)是比较热门的中小型企业级项目开发的框架,对于新手来说也是比较容易学习入门的.虽说容易,但在框架搭建过程中仍然遇到了许多问题,因此用实例记录下来吧. 实例 第一步——导包 Spring框架包及其依赖包 MyBatis框架包及其依赖包 MyBatis-EhCache架包 C3P0架包 MySql数据库驱动包 项目架包如下: 项目结构如

Spring3.2 MVC框架搭建

Spring3.2 MVC框架搭建 Java开发 > 分享 > Java框架 转自:http://www.liutime.com/javainfo/2255/ ————————————————————————————————————————————————— 最近struts2曝的漏洞比较严重,特别写上Spring MVC框架搭建及使用教程 最新的jar包请到Spring官方网站下载:http://www.springsource.org/spring-community-download 1

spring4.06 hibernate4.31 struts2.3.16 全注解MAVEN环境搭建

最近使用目录最新版本的SPRING.HIBERNATE.STRUTS使用MAVEN搭建了一个全注解的环境.记录一下 POM文件: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 htt

Spring+Struts2+Mybatis框架搭建时的常见典型问题

搭建SSM框架时,总是遇到这样那样的问题,有的一眼就能看出来,有的需要经验的积累.现将自己搭建SSM框架时遇到的典型问题总结如下: 一.Struts2框架下的action中无法使用@Autowired自动注入Bean(运行时bean总是为null) 常见原因: A.Spring的配置文件中组件扫描路径错误(即<context:component-scan base-package="xx.xx" />配置错误). B.@Autowired声明的beanName错误,bean

基于Docker的TensorFlow机器学习框架搭建和实例源码解读

概述:基于Docker的TensorFlow机器学习框架搭建和实例源码解读,TensorFlow作为最火热的机器学习框架之一,Docker是的容器,可以很好的结合起来,为机器学习或者科研人员提供便捷的机器学习开发环境,探索人工智能的奥秘,容器随开随用方便快捷.源码解析TensorFlow容器创建和示例程序运行,为热爱机器学者降低学习难度. 默认机器已经装好了Docker(Docker安装和使用可以看我另一篇博文:Ubuntu16.04安装Docker1.12+开发实例+hello world+w

Struts2.0 框架搭建步骤详解

实现了MVC思想的struts框架,主要分三层结构,即:view->controller->model,三者互相传递数据,实现了数据在前台和后台的转换,验证,展示,存储. 搭建struts框架的步骤如下: 1.在myeclipse中新建Java项目,删除项目中的源文件src,建立普通文件,将Struts解压后的jar包复制在新建的普通文件夹中. 2.新建web项目,右键选择build path->add jars(不是导入外部第三方的jar包) 选择第一步新建的Java项目,将里面的Ja

(六)Spring4 整合Hibernate4,Struts2

第一节:S2SH 整合所需Jar 包 Struts2.3.16,Spring4.0.6,Hibernate4.3.5 整合所需jar 包: Struts2.3.16 jar 包 Spring4.0.6 jar 包 Hibernate4.3.5 jar 包 第二节:Spring4 整合Hibernate4 Spring4 接管Hibernate4 所有Bean 实例,以及SessionFactory,事务管理器: 泛型注入: 第三节:Spring4 整合Struts2 Spring4 接管Stru

hibernate4 spring3.2 事务不提交分析

最近在做微信项目,我搭建了一个基于servlet,spring3.2,hibernate4.1的框架.因为基于消息的servlet和基于业务层是分开做的,也就是先把业务层做了,再去将所有的请求转到业务层处理.所以一开始开发就用junit做测试,模拟的消息保存数据库也都能正常进行.下面列出某一个junit 的 testcase,在这个测试的例子中,我为junit配置了事务,事务也能正常提交.所以,后面的业务层写法就一直这么开发下去,也没什么问题 package com.cpic.inf.tools