• 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;
    	}
    
    }
    

      

      

  • 相关阅读:
    C# 操作ACCESS数据库
    装饰模式(Decorator Pattern)
    桥接模式(Bridge Pattern)
    单件模式(Singleton Pattern)
    横竖不能重复的9个数,,,,,
    C#操作Access数据库的例子
    组合模式(Composite Pattern)
    建造者模式(Builder Pattern)
    工厂方法模式(Factory Method)
    外观模式(Façade Pattern)
  • 原文地址:https://www.cnblogs.com/estellez/p/4154729.html
Copyright © 2020-2023  润新知