struts-2.3.16.3+hibernate-4.2.14+spring-framework-4.0.6整合


一.首先列一些需要的jar包

框架版本jar包截图
strutsstruts-2.3.16.3
hibernatehibernate-4.2.14
springspring-framework-4.0.6
proxoolproxool-0.9.1
c3p0c3p0-0.9.2.1
log4j2log4j2
mysqlmysql
   

二.数据库脚本

DROP TABLE IF EXISTS `systemuser`;
CREATE TABLE `systemuser` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) DEFAULT NULL,
  `loginid` varchar(50) DEFAULT NULL,
  `password` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


三.eclipse目录结构


四.配置文件

  1.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SSHitegration20140714</display-name>
  
  <!-- 系统启动后第一个页面 -->
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
  
  
  <!-- session超时时间 -->
  <session-config>
    <session-timeout>30</session-timeout>
  </session-config>
  
  
  <!-- Spring 刷新Introspector防止内存泄露 -->  
  <listener>  
      <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  
  </listener>  
    
  <!-- 添加对spring的支持 -->
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 著名 Character Encoding filter--> 
  <filter>  
      <filter-name>encodingFilter</filter-name>  
      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
      <init-param>  
          <param-name>encoding</param-name>  
          <param-value>UTF-8</param-value>  
      </init-param>  
  </filter> 

  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>
    
  <!-- 添加对struts2的支持 -->  
  <filter>
      <filter-name>struts2</filter-name>
      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
  
</web-app>


  2.applicationContext.xml

<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/spring-aop-2.5.xsd"
       xmlns:context="http://www.springframework.org/schema/context/spring-context-2.5.xsd"
       xmlns:flow="http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd"
       xmlns:jms="http://www.springframework.org/schema/jms/spring-jms-2.5.xsd"
       xmlns:jee="http://www.springframework.org/schema/jee/spring-jee-2.5.xsd"
       xmlns:lang="http://www.springframework.org/schema/lang/spring-lang-2.5.xsd"
       xmlns:osgi="http://www.springframework.org/schema/osgi/spring-osgi.xsd"
       xmlns:tx="http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
       xmlns:util="http://www.springframework.org/schema/util/spring-util-2.5.xsd"
       xmlns:p="http://www.springframework.org/schema/p"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
          http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/aop/spring-aop-2.5.xsd/spring-spring-aop-2.5.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/context/spring-context-2.5.xsd/spring-spring-context-2.5.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd/spring-spring-webflow-config-1.0.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/jms/spring-jms-2.5.xsd http://www.springframework.org/schema/jms/spring-jms-2.5.xsd/spring-spring-jms-2.5.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/jee/spring-jee-2.5.xsd/spring-spring-jee-2.5.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/lang/spring-lang-2.5.xsd http://www.springframework.org/schema/lang/spring-lang-2.5.xsd/spring-spring-lang-2.5.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/osgi/spring-osgi.xsd http://www.springframework.org/schema/osgi/spring-osgi.xsd/spring-spring-osgi.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/tx/spring-tx-2.5.xsd/spring-spring-tx-2.5.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/util/spring-util-2.5.xsd http://www.springframework.org/schema/util/spring-util-2.5.xsd/spring-spring-util-2.5.xsd-3.1.1.RELEASE.xsd
">
    <!-- 引入spring事务管理配置文件-->
    <import resource="/spring_config/spring_hibernate.xml"></import>
    <!-- 引入spring依赖注入管理配置文件-->
    <import resource="/beans_config/spring_beans.xml"></import>
</beans>

  3.spring_hibernate.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:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="    
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
    
    <!-- 数据库连接池proxool配置   目前还未找到原因,不知道为什么不能加载ProxoolMysqlConf.xml,报错“no suitable driver for ProxoolMysqlConf.xml”-->  
    <!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.logicalcobwebs.proxool.ProxoolDriver"></property>
        <property name="url" value="ProxoolMysqlConf.xml"></property>   
    </bean> -->
    
    <!-- 加载数据库属性配置文件 -->  
    <context:property-placeholder location="classpath:db.properties" />  
  
    <!-- 数据库连接池c3p0配置 -->  
    <!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"  
        destroy-method="close">  
        <property name="jdbcUrl" value="${db.url}"></property>  
        <property name="driverClass" value="${db.driverClassName}"></property>  
        <property name="user" value="${db.username}"></property>  
        <property name="password" value="${db.password}"></property>  
        <property name="maxPoolSize" value="40"></property>  
        <property name="minPoolSize" value="1"></property>  
        <property name="initialPoolSize" value="1"></property>  
        <property name="maxIdleTime" value="20"></property>  
    </bean> -->
    
    <!-- 数据库连接池proxool配置 -->  
    <bean id="dataSource"   class="org.logicalcobwebs.proxool.ProxoolDataSource">  
        <property name="driver" value="${db.driverClassName}"/>  
        <property name="driverUrl" value="${db.url}"/> 
        <property name="user" value="${db.username}"/>  
        <property name="password" value="${db.password}"/>  
        <property name="alias" value="Pool_dbname"/>  
        <property name="maximumActiveTime" value="90000"/>  
        <property name="prototypeCount" value="5"/>  
        <property name="maximumConnectionCount" value="100"/>  
        <property name="minimumConnectionCount" value="5"/>  
        <property name="simultaneousBuildThrottle" value="20"/>  
        <property name="houseKeepingTestSql" value="select CURRENT_DATE"/>  
     </bean>
    
    <!-- 实体bean采用注解进行映射 annotation  -->
    <bean id="sf" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource">  
            <ref bean="dataSource" />  
        </property>
        <property name="configLocation" value="classpath:hibernate.cfg.mysql.xml" />
        <property name="packagesToScan">
            <list>
                <value>com.sshintegration.entities</value>
            </list>
        </property>

    </bean>
    
    <!-- 会话模板 -->
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
        <property name="sessionFactory">
            <ref bean="sf" />
        </property>
    </bean>
    
    <!-- 事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sf" />
    </bean>
    
    <!-- 拦截器 -->
    <bean id="transInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
        <property name="transactionManager">
            <ref bean="transactionManager" />
        </property>
        <property name="transactionAttributes">
            <props >
                <prop key="save*">PROPAGATION_REQUIRED</prop>
                <prop key="update*">PROPAGATION_REQUIRED</prop>
                <prop key="del*">PROPAGATION_REQUIRED</prop>
                <prop key="remove*">PROPAGATION_REQUIRED</prop>
                <prop key="load*">PROPAGATION_REQUIRED</prop>
                <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
            </props>
        </property>
    </bean>
    
    <!--事务代理 -->
    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="proxyTargetClass">
            <value>true</value>
        </property>
        <property name="beanNames">
            <!-- 凡是通过数据源进行数据库操作的类,加入下面的列表就可以实现自动事务管理 -->
            <list>
                <value>systemuserService</value>
            </list>
        </property>
        <property name="interceptorNames">
            <!-- 拦截器列表 -->
            <list>
                <value>transInterceptor</value>
            </list>
        </property>
    </bean>
    
</beans>


  4.spring_beans.xml

<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/spring-aop-2.5.xsd"
       xmlns:context="http://www.springframework.org/schema/context/spring-context-2.5.xsd"
       xmlns:flow="http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd"
       xmlns:jms="http://www.springframework.org/schema/jms/spring-jms-2.5.xsd"
       xmlns:jee="http://www.springframework.org/schema/jee/spring-jee-2.5.xsd"
       xmlns:lang="http://www.springframework.org/schema/lang/spring-lang-2.5.xsd"
       xmlns:osgi="http://www.springframework.org/schema/osgi/spring-osgi.xsd"
       xmlns:tx="http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
       xmlns:util="http://www.springframework.org/schema/util/spring-util-2.5.xsd"
       xmlns:p="http://www.springframework.org/schema/p"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
          http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/aop/spring-aop-2.5.xsd/spring-spring-aop-2.5.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/context/spring-context-2.5.xsd/spring-spring-context-2.5.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd/spring-spring-webflow-config-1.0.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/jms/spring-jms-2.5.xsd http://www.springframework.org/schema/jms/spring-jms-2.5.xsd/spring-spring-jms-2.5.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/jee/spring-jee-2.5.xsd/spring-spring-jee-2.5.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/lang/spring-lang-2.5.xsd http://www.springframework.org/schema/lang/spring-lang-2.5.xsd/spring-spring-lang-2.5.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/osgi/spring-osgi.xsd http://www.springframework.org/schema/osgi/spring-osgi.xsd/spring-spring-osgi.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/tx/spring-tx-2.5.xsd/spring-spring-tx-2.5.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/util/spring-util-2.5.xsd http://www.springframework.org/schema/util/spring-util-2.5.xsd/spring-spring-util-2.5.xsd-3.1.1.RELEASE.xsd
">
     
     <import resource="/beans/spring-dao-beans.xml"></import>
     <import resource="/beans/spring-service-beans.xml"></import>
     <import resource="/beans/spring-action-beans.xml"></import>
     <import resource="/beans/spring-other-beans.xml"></import>

</beans>

  5.spring-dao-beans.xml

<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/spring-aop-2.5.xsd"
       xmlns:context="http://www.springframework.org/schema/context/spring-context-2.5.xsd"
       xmlns:flow="http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd"
       xmlns:jms="http://www.springframework.org/schema/jms/spring-jms-2.5.xsd"
       xmlns:jee="http://www.springframework.org/schema/jee/spring-jee-2.5.xsd"
       xmlns:lang="http://www.springframework.org/schema/lang/spring-lang-2.5.xsd"
       xmlns:osgi="http://www.springframework.org/schema/osgi/spring-osgi.xsd"
       xmlns:tx="http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
       xmlns:util="http://www.springframework.org/schema/util/spring-util-2.5.xsd"
       xmlns:p="http://www.springframework.org/schema/p"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
          http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/aop/spring-aop-2.5.xsd/spring-spring-aop-2.5.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/context/spring-context-2.5.xsd/spring-spring-context-2.5.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd/spring-spring-webflow-config-1.0.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/jms/spring-jms-2.5.xsd http://www.springframework.org/schema/jms/spring-jms-2.5.xsd/spring-spring-jms-2.5.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/jee/spring-jee-2.5.xsd/spring-spring-jee-2.5.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/lang/spring-lang-2.5.xsd http://www.springframework.org/schema/lang/spring-lang-2.5.xsd/spring-spring-lang-2.5.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/osgi/spring-osgi.xsd http://www.springframework.org/schema/osgi/spring-osgi.xsd/spring-spring-osgi.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/tx/spring-tx-2.5.xsd/spring-spring-tx-2.5.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/util/spring-util-2.5.xsd http://www.springframework.org/schema/util/spring-util-2.5.xsd/spring-spring-util-2.5.xsd-3.1.1.RELEASE.xsd
">    
      <!-- DAO -->
      <bean name="systemuserDao" class="com.sshintegration.dao.impl.SystemuserDao">
         <property name="hibernateTemplate" ref="hibernateTemplate" />
      </bean>
  
</beans>

  6.spring-service-beans.xml

<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/spring-aop-2.5.xsd"
       xmlns:context="http://www.springframework.org/schema/context/spring-context-2.5.xsd"
       xmlns:flow="http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd"
       xmlns:jms="http://www.springframework.org/schema/jms/spring-jms-2.5.xsd"
       xmlns:jee="http://www.springframework.org/schema/jee/spring-jee-2.5.xsd"
       xmlns:lang="http://www.springframework.org/schema/lang/spring-lang-2.5.xsd"
       xmlns:osgi="http://www.springframework.org/schema/osgi/spring-osgi.xsd"
       xmlns:tx="http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
       xmlns:util="http://www.springframework.org/schema/util/spring-util-2.5.xsd"
       xmlns:p="http://www.springframework.org/schema/p"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
          http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/aop/spring-aop-2.5.xsd/spring-spring-aop-2.5.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/context/spring-context-2.5.xsd/spring-spring-context-2.5.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd/spring-spring-webflow-config-1.0.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/jms/spring-jms-2.5.xsd http://www.springframework.org/schema/jms/spring-jms-2.5.xsd/spring-spring-jms-2.5.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/jee/spring-jee-2.5.xsd/spring-spring-jee-2.5.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/lang/spring-lang-2.5.xsd http://www.springframework.org/schema/lang/spring-lang-2.5.xsd/spring-spring-lang-2.5.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/osgi/spring-osgi.xsd http://www.springframework.org/schema/osgi/spring-osgi.xsd/spring-spring-osgi.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/tx/spring-tx-2.5.xsd/spring-spring-tx-2.5.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/util/spring-util-2.5.xsd http://www.springframework.org/schema/util/spring-util-2.5.xsd/spring-spring-util-2.5.xsd-3.1.1.RELEASE.xsd
">
     <!-- 基本service bean -->
     <bean name="systemuserService" class="com.sshintegration.service.impl.SystemuserService">
        <property name="systemuserDao" ref="systemuserDao"/>
     </bean>
     
     
     <!-- function service bean -->
     
</beans>


  7.spring-action-beans.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/spring-aop-2.5.xsd"
       xmlns:context="http://www.springframework.org/schema/context/spring-context-2.5.xsd"
       xmlns:flow="http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd"
       xmlns:jms="http://www.springframework.org/schema/jms/spring-jms-2.5.xsd"
       xmlns:jee="http://www.springframework.org/schema/jee/spring-jee-2.5.xsd"
       xmlns:lang="http://www.springframework.org/schema/lang/spring-lang-2.5.xsd"
       xmlns:osgi="http://www.springframework.org/schema/osgi/spring-osgi.xsd"
       xmlns:tx="http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
       xmlns:util="http://www.springframework.org/schema/util/spring-util-2.5.xsd"
       xmlns:p="http://www.springframework.org/schema/p"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/aop/spring-aop-2.5.xsd/spring-spring-aop-2.5.xsd-3.0.xsd
          http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/context/spring-context-2.5.xsd/spring-spring-context-2.5.xsd-3.0.xsd
          http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd/spring-spring-webflow-config-1.0.xsd-3.0.xsd
          http://www.springframework.org/schema/jms/spring-jms-2.5.xsd http://www.springframework.org/schema/jms/spring-jms-2.5.xsd/spring-spring-jms-2.5.xsd-3.0.xsd
          http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/jee/spring-jee-2.5.xsd/spring-spring-jee-2.5.xsd-3.0.xsd
          http://www.springframework.org/schema/lang/spring-lang-2.5.xsd http://www.springframework.org/schema/lang/spring-lang-2.5.xsd/spring-spring-lang-2.5.xsd-3.0.xsd
          http://www.springframework.org/schema/osgi/spring-osgi.xsd http://www.springframework.org/schema/osgi/spring-osgi.xsd/spring-spring-osgi.xsd-3.0.xsd
          http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/tx/spring-tx-2.5.xsd/spring-spring-tx-2.5.xsd-3.0.xsd
          http://www.springframework.org/schema/util/spring-util-2.5.xsd http://www.springframework.org/schema/util/spring-util-2.5.xsd/spring-spring-util-2.5.xsd-3.0.xsd
">
    <!-- login -->
    <bean name="loginAction" class="com.sshintegration.action.LoginAction" scope="prototype" >
        <property name="systemuserService" ref="systemuserService" />
    </bean>
    
</beans>

  8.spring-other-beans.xml

<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/spring-aop-2.5.xsd"
       xmlns:context="http://www.springframework.org/schema/context/spring-context-2.5.xsd"
       xmlns:flow="http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd"
       xmlns:jms="http://www.springframework.org/schema/jms/spring-jms-2.5.xsd"
       xmlns:jee="http://www.springframework.org/schema/jee/spring-jee-2.5.xsd"
       xmlns:lang="http://www.springframework.org/schema/lang/spring-lang-2.5.xsd"
       xmlns:osgi="http://www.springframework.org/schema/osgi/spring-osgi.xsd"
       xmlns:tx="http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
       xmlns:util="http://www.springframework.org/schema/util/spring-util-2.5.xsd"
       xmlns:p="http://www.springframework.org/schema/p"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
          http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/aop/spring-aop-2.5.xsd/spring-spring-aop-2.5.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/context/spring-context-2.5.xsd/spring-spring-context-2.5.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd/spring-spring-webflow-config-1.0.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/jms/spring-jms-2.5.xsd http://www.springframework.org/schema/jms/spring-jms-2.5.xsd/spring-spring-jms-2.5.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/jee/spring-jee-2.5.xsd/spring-spring-jee-2.5.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/lang/spring-lang-2.5.xsd http://www.springframework.org/schema/lang/spring-lang-2.5.xsd/spring-spring-lang-2.5.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/osgi/spring-osgi.xsd http://www.springframework.org/schema/osgi/spring-osgi.xsd/spring-spring-osgi.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/tx/spring-tx-2.5.xsd/spring-spring-tx-2.5.xsd-3.1.1.RELEASE.xsd
          http://www.springframework.org/schema/util/spring-util-2.5.xsd http://www.springframework.org/schema/util/spring-util-2.5.xsd/spring-spring-util-2.5.xsd-3.1.1.RELEASE.xsd
">
     
</beans>

  9. db.properties

db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/sshintegration
db.username=root
db.password=123456

  10.hibernate.cfg.mysql.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>        
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.format_sql">true</property>
        <property name="connection.autoReconnect">true</property>
        <property name="connection.autoReconnectForPools">true</property>
        <property name="connection.is-connection-validation-required">true</property>        
    </session-factory>
</hibernate-configuration>


  11.log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>  
<configuration status="error">
    <!--先定义所有的appender-->
    <appenders>
        <!--这个输出控制台的配置-->
        <Console name="Console" target="SYSTEM_OUT">
            <!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
            <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/>
            <!--这个都知道是输出日志的格式-->
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
        </Console>
        <!--文件会打印出所有信息,这个log每次运行程序会自动清空,由append属性决定,这个也挺有用的,适合临时测试用-->
        <File name="log" fileName="log/test.log" append="false">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
        </File>

        <!--这个会打印出所有的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档-->
        <RollingFile name="RollingFile" fileName="c:/logs/app.log" filePattern="log/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/>
            <SizeBasedTriggeringPolicy size="50MB"/>
        </RollingFile>
    </appenders>
    <!--然后定义logger,只有定义了logger并引入的appender,appender才会生效-->
    <loggers>
        <!--建立一个默认的root的logger-->
        <root level="trace">
            <appender-ref ref="RollingFile"/>
            <appender-ref ref="Console"/>
        </root>
    </loggers>
</configuration>


  12.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>
    <!-- 启用spring管理struts2 -->
    <constant name="struts.objectFactory" value="spring"/>
    <!-- 启用开发模式 -->
    <constant name="struts.devMode" value="true"/>
    <!-- 配置编码格式为UTF-8 -->
    <constant name="struts.i18n.encoding" value="UTF-8"/>
    <constant name="struts.custom.i18n.resources" value="globalMessages" />
    <!-- URL后自动加入参数不启用 -->
    <constant name="struts.url.includeParams" value="none" /> 
    <!-- 不启用Struts2自带主题样式 -->
    <!--<constant name="struts.ui.theme" value="simple"/>-->
    <!-- 加入Struts默认加载的文件 -->
    <include file="struts-default.xml"/>  
    <include file="struts-plugin.xml" />
    <!-- 引入用户Action xml -->
    <!-- <include file="struts-crud.xml" />
    <include file="struts-function.xml" />
    <include file="struts-tree.xml" />
    <include file="struts-combobox.xml" /> -->
    
   <package name="global" extends="struts-default"> 	   	 
    	<!-- 全局页面 -->
        <global-results> 
            <result name="globalError">/error.jsp</result>
            <result name="globalLogin" >/login.jsp</result>
            <result name="globalLoginTimeout" >/logintimeout.jsp</result> 
        </global-results>

        <global-exception-mappings>
            <exception-mapping result="error" exception="java.lang.Exception"/>
        </global-exception-mappings> 
    </package>
    <package name="jsonBase" extends="json-default"> 	   	 
    	<!-- 全局页面 -->
        <global-results> 
            <result name="globalError">/error.jsp</result>
            <result name="globalLogin" >/login.jsp</result>
            <result name="globalLoginTimeout" >/logintimeout.jsp</result> 
        </global-results>

        <global-exception-mappings>
            <exception-mapping result="error" exception="java.lang.Exception"/>
        </global-exception-mappings> 
    </package>
    
    <!-- login -->
    <package name="login" namespace="/login" extends="struts-default"> 	   	 
    	<!-- 全局页面 -->
        <global-results> 
            <result name="globalError">/error.jsp</result>
            <result name="globalLogin" >/login.jsp</result>
            <result name="globalLoginTimeout" >/logintimeout.jsp</result> 
        </global-results>
        
        <action name="login*" class="loginAction"  method="{1}">
             <result name="success" >/WEB-INF/jsp/mainindex.jsp</result>
        </action>
    </package>
</struts>

五.Java类

  1.Systemuser.java

package com.sshintegration.entities;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "systemuser")
public class Systemuser {

	private int id;
	private String username;
	private String loginid;
	private String password;
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	@Column(name = "username")
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	
	@Column(name = "loginid")
	public String getLoginid() {
		return loginid;
	}
	public void setLoginid(String loginid) {
		this.loginid = loginid;
	}
	
	@Column(name = "password")
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	
}

  2.iSystemuser.java

/**   
 * @Title: iSystemuser.java 
 * @Package com.sshintegration.dao.i 
 * @Description: 系统用户dao接口 
 * @author jason   
 * @date 2014年7月15日 下午3:04:05 
 * @version V1.0.0  
 */
package com.sshintegration.dao.i;

import java.io.Serializable;
import java.util.List;

import com.sshintegration.entities.Systemuser;

/** 
 * @ClassName: iSystemuser 
 * @Description: 系统用户dao接口 
 * @author jason
 * @date 2014年7月15日 下午3:04:05 
 *  
 */
public interface iSystemuser {

	/**
	 * 
	 * @Title: getAllSystemuser
	 * @author jason 
	 * @date 2014年7月15日 下午3:10:53 
	 * @Description: 查询所有系统登录用户 
	 * @return List<Systemuser>
	 * @throws
	 */
	public List<Systemuser> getAllSystemuser();
	
	/**
	 * 
	 * @Title: getSystemuserById
	 * @author jason 
	 * @date 2014年7月15日 下午3:12:39 
	 * @Description: 通过id查询系统登录用户
	 * @param id
	 * @return Systemuser
	 * @throws
	 */
	public Systemuser getSystemuserById(Serializable id);
	
	/**
	 * 
	 * @Title: saveORupdateSystemuser
	 * @author jason 
	 * @date 2014年7月15日 下午3:47:21 
	 * @Description: 保存或者更新一个系统用户
	 * @param systemuser
	 * @return boolean
	 * @throws
	 */
	public boolean saveORupdateSystemuser(Systemuser systemuser);
	
	/**
	 * 
	 * @Title: removeSystemuser
	 * @author jason 
	 * @date 2014年7月15日 下午3:48:04 
	 * @Description: 删除一个系统用户 
	 * @param systemuser
	 * @return boolean
	 * @throws
	 */
	public boolean removeSystemuser(Systemuser systemuser);
	
	/**
	 * 
	 * @Title: getSystemuserByPropertys
	 * @author jason 
	 * @date 2014年7月15日 下午6:02:16 
	 * @Description: 根据多个属性查询系统用户 
	 * @param propertyNames
	 * @param values
	 * @return List<Systemuser>
	 * @throws
	 */
	public List<Systemuser> getSystemuserByPropertys(String[] propertyNames, Object[] values);
}


  3.SystemuserDao.java

/**   
 * @Title: SystemuserDao.java 
 * @Package com.sshintegration.dao.impl 
 * @Description: 系统用户dao类操作数据库的各种方法
 * @author jason   
 * @date 2014年7月15日 下午3:48:57 
 * @version V1.0.0  
 */
package com.sshintegration.dao.impl;

import java.io.Serializable;
import java.util.List;

import ssh.dao.impl.BaseDao;

import com.sshintegration.dao.i.iSystemuser;
import com.sshintegration.entities.Systemuser;

/** 
 * @ClassName: SystemuserDao 
 * @Description: 系统用户dao类
 * @author jason
 * @date 2014年7月15日 下午3:48:57 
 *  
 */
public class SystemuserDao extends BaseDao implements iSystemuser {

	/** 
	 * @Title: getAllSystemuser
	 * @author jason 
	 * @date 2014年7月15日 下午3:48:57 
	 * @Description: 查询所有系统登录用户 
	 * @return 
	 */
	@Override
	public List<Systemuser> getAllSystemuser() {
		return this.findAll(Systemuser.class);
	}

	/** 
	 * @Title: getSystemuserById
	 * @author jason 
	 * @date 2014年7月15日 下午3:48:57 
	 * @Description: 通过id查询系统登录用户
	 * @param id
	 * @return 
	 */
	@Override
	public Systemuser getSystemuserById(Serializable id) {
		return (Systemuser)this.findById(Systemuser.class, id);
	}

	/** 
	 * @Title: saveORupdateSystemuser
	 * @author jason 
	 * @date 2014年7月15日 下午3:48:57 
	 * @Description: 保存或者更新一个系统用户
	 * @param systemuser
	 * @return 
	 */
	@Override
	public boolean saveORupdateSystemuser(Systemuser systemuser) {
		return this.saveORupdate(systemuser);
	}

	/** 
	 * @Title: removeSystemuser
	 * @author jason 
	 * @date 2014年7月15日 下午3:48:57 
	 * @Description: 删除一个系统用户 
	 * @param systemuser
	 * @return 
	 */
	@Override
	public boolean removeSystemuser(Systemuser systemuser) {
		return this.delete(systemuser);
	}

	/** 
	 * @Title: getSystemuserByPropertys
	 * @author jason 
	 * @date 2014年7月15日 下午6:03:05 
	 * @Description: 根据多个属性查询系统用户 
	 * @param propertyNames
	 * @param values
	 * @return 
	 */
	@Override
	public List<Systemuser> getSystemuserByPropertys(String[] propertyNames,
			Object[] values) {
		return this.findByPropertys(propertyNames, values, Systemuser.class);
	}

}

  4.iSystemuserService.java

/**   
 * @Title: iSystemuserService.java 
 * @Package com.sshintegration.service.i 
 * @Description: 系统用户业务接口 
 * @author jason   
 * @date 2014年7月15日 下午4:23:46 
 * @version V1.0.0  
 */
package com.sshintegration.service.i;

/** 
 * @ClassName: iSystemuserService 
 * @Description: 系统用户业务接口
 * @author jason
 * @date 2014年7月15日 下午4:23:46 
 *  
 */
public interface iSystemuserService {

	/**
	 * 
	 * @Title: validateSystemuser
	 * @author jason 
	 * @date 2014年7月15日 下午5:20:58 
	 * @Description: 验证系统登录用户是否存在
	 * @param userid
	 * @param password
	 * @return boolean
	 * @throws
	 */
	public boolean validateSystemuser(String userid,String password);
}


  5.SystemuserService.java

/**   
 * @Title: SystemuserService.java 
 * @Package com.sshintegration.service.impl 
 * @Description: 系统用户业务实现类
 * @author jason   
 * @date 2014年7月15日 下午4:24:25 
 * @version V1.0.0  
 */
package com.sshintegration.service.impl;

import java.util.List;

import com.sshintegration.dao.i.iSystemuser;
import com.sshintegration.entities.Systemuser;
import com.sshintegration.service.i.iSystemuserService;

/** 
 * @ClassName: SystemuserService 
 * @Description: 系统用户业务实现类 
 * @author jason
 * @date 2014年7月15日 下午4:24:25 
 *  
 */
public class SystemuserService implements iSystemuserService {
	
	/**
	 * systemuserDao
	 */
	private iSystemuser systemuserDao;

	public iSystemuser getSystemuserDao() {
		return systemuserDao;
	}

	public void setSystemuserDao(iSystemuser systemuserDao) {
		this.systemuserDao = systemuserDao;
	}

	/** 
	 * @Title: validateSystemuser
	 * @author jason 
	 * @date 2014年7月15日 下午5:44:34 
	 * @Description: 验证系统登录用户是否存在
	 * @param userid
	 * @param password
	 * @return 
	 */
	@Override
	public boolean validateSystemuser(String userid, String password) {
		String[] propertyNames = {"loginid","password"};
		String[] values = {userid,password};
		List<Systemuser> suList = this.systemuserDao.getSystemuserByPropertys(propertyNames, values);
		if(suList.size() == 1) {
			return true;
		} else {
			return false;
		}		
	}

}

  6.LoginAction.java

/**   
 * @Title: LoginAction.java 
 * @Package com.sshintegration.action 
 * @Description: 登录action
 * @author jason   
 * @date 2014年7月16日 上午9:54:32 
 * @version V1.0.0  
 */
package com.sshintegration.action;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.opensymphony.xwork2.ActionSupport;
import com.sshintegration.service.i.iSystemuserService;

/** 
 * @ClassName: LoginAction 
 * @Description: 登录action 
 * @author jason
 * @date 2014年7月16日 上午9:54:32 
 *  
 */
public class LoginAction extends ActionSupport{
	
	private static Logger log = LogManager.getLogger(LoginAction.class.getName());
	
	private String loginid;
	private String password;
	private iSystemuserService systemuserService;
	
	public String getLoginid() {
		return loginid;
	}


	public void setLoginid(String loginid) {
		this.loginid = loginid;
	}


	public String getPassword() {
		return password;
	}


	public void setPassword(String password) {
		this.password = password;
	}


	public iSystemuserService getSystemuserService() {
		return systemuserService;
	}


	public void setSystemuserService(iSystemuserService systemuserService) {
		this.systemuserService = systemuserService;
	}

	
	public String login(){
		log.debug("this is login action!");
		if(this.loginid != null && this.password != null
				&& !this.loginid.equals("") && !this.password.equals("")) {
			if(this.systemuserService.validateSystemuser(loginid, password)) {
				return "success";
			} else {
				return "failure";
			}
		} else {
			return "failure";
		}
	}
}


   7.iDao.java

package ssh.dao.i;

import java.io.Serializable;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;

/**
 * 
 * @author jason
 * @version 1.0.0
 * @since 2014-07-15
 * @function dao层接口
 *
 */
public interface iDao {

	/**
	 * 
	 * @Title: findById
	 * @author jason 
	 * @date 2014年7月15日 下午2:14:27 
	 * @Description: 根据Id 查询数据 
	 * @param @param clazz
	 * @param @param id
	 * @return Object
	 * @throws
	 */
	public Object findById(Class clazz,Serializable id);
	
	/**
	 * 
	 * @Title: findByProperty
	 * @author jason 
	 * @date 2014年7月15日 下午2:13:06 
	 * @Description: 根据条件属性查询 
	 * @param @param propertyName
	 * @param @param value
	 * @param @param clazz
	 * @return List
	 * @throws
	 */
	public List findByProperty(String propertyName, Object value, Class clazz);
	
	/**
	 * 
	 * @Title: findByPropertys
	 * @author jason 
	 * @date 2014年7月15日 下午2:23:26 
	 * @Description: 根据多个条件属性查询 
	 * @param propertyNames
	 * @param values
	 * @param clazz
	 * @return List
	 * @throws
	 */
	public List findByPropertys(String[] propertyNames, Object[] values, Class clazz);
	
	/**
	 * 
	 * @Title: findAll
	 * @author jason 
	 * @date 2014年7月15日 下午2:26:45 
	 * @Description: 根据具体的类查询数据 
	 * @param clazz
	 * @return List
	 * @throws
	 */
	public List findAll(Class clazz);
	
	/**
	 * 
	 * @Title: findByExample
	 * @author jason 
	 * @date 2014年7月15日 下午2:29:33 
	 * @Description: 根据对象查询 
	 * @param instance 
	 * @return List
	 * @throws
	 */
	public List findByExample(Object instance);
	
	/**
	 * 
	 * @Title: saveBatch
	 * @author jason 
	 * @date 2014年7月15日 下午2:30:50 
	 * @Description: 批量保存 
	 * @param instances 
	 * @return boolean
	 * @throws
	 */
	public boolean saveBatch(final List instances);
	
	/**
	 * 
	 * @Title: updateBatch
	 * @author jason 
	 * @date 2014年7月15日 下午2:34:15 
	 * @Description: 批量更新
	 * @param instances 
	 * @return boolean
	 * @throws
	 */
	public boolean updateBatch(final List instances);
	
	/**
	 * 
	 * @Title: deleteBatch
	 * @author jason 
	 * @date 2014年7月15日 下午2:37:23 
	 * @Description: 批量删除
	 * @param instances 
	 * @return boolean
	 * @throws
	 */
	public boolean deleteBatch(final List instances);
	
	/**
	 * 
	 * @Title: delete
	 * @author jason 
	 * @date 2014年7月15日 下午2:38:53 
	 * @Description: 删除数据
	 * @param instance 
	 * @return boolean
	 * @throws
	 */
	public boolean delete(Object instance);
	
	/**
	 * 
	 * @Title: saveORupdate
	 * @author jason 
	 * @date 2014年7月15日 下午2:41:06 
	 * @Description: 更新数据
	 * @param instance 
	 * @return void
	 * @throws
	 */
	public boolean saveORupdate(Object instance);
	
	/**
	 * 
	 * @Title: getCountByCriteria
	 * @author jason 
	 * @date 2014年7月15日 下午2:42:48 
	 * @Description: 统计条数 
	 * @param detachedCriteria
	 * @return int
	 * @throws
	 */
	public int getCountByCriteria(final DetachedCriteria detachedCriteria);
	
	/**
	 * 
	 * @Title: findByCriteria
	 * @author jason 
	 * @date 2014年7月15日 下午2:45:24 
	 * @Description: 分页查询 
	 * @param detachedCriteria
	 * @param firstResult
	 * @param maxResults
	 * @return List
	 * @throws
	 */
	public List findByCriteria(final DetachedCriteria detachedCriteria,final int firstResult,final int maxResults);
	
	/**
	 * 
	 * @Title: getCountByHql
	 * @author jason 
	 * @date 2014年7月15日 下午2:47:36 
	 * @Description: 根据HQL 查询记录数 
	 * @param hql
	 * @return int
	 * @throws
	 */
	public int getCountByHql(String hql);
	
	/**
	 * 
	 * @Title: findByHql
	 * @author jason 
	 * @date 2014年7月15日 下午2:49:01 
	 * @Description: 根据HQL 分页 
	 * @param hql
	 * @param firstResult
	 * @param maxResults
	 * @return List
	 * @throws
	 */
	public List findByHql(final String hql,final int firstResult,final int maxResults);
	
	/**
	 * 
	 * @Title: deleteByProperty
	 * @author jason 
	 * @date 2014年7月15日 下午2:57:20 
	 * @Description: 根据属性删除 
	 * @param propertyName
	 * @param value
	 * @param clazz 
	 * @return boolean
	 * @throws
	 */
	public boolean deleteByProperty(final String propertyName,final Object value,final Class clazz);
	
	/**
	 * 
	 * @Title: deleteByHql
	 * @author jason 
	 * @date 2014年7月15日 下午2:59:13 
	 * @Description: 根据HQL 删除对应数据 
	 * @param hql
	 * @param clazz 
	 * @return boolean
	 * @throws
	 */
	public boolean deleteByHql(final String hql,final Class clazz);
	
	/**
	 * 
	 * @Title: deleteByPropertys
	 * @author jason 
	 * @date 2014年7月15日 下午3:01:34 
	 * @Description: 根据多个条件属性删除数据
	 * @param propertyNames
	 * @param values
	 * @param clazz 
	 * @return boolean
	 * @throws
	 */
	public boolean deleteByPropertys(final String[] propertyNames, final Object[] values, final Class clazz);
}

  8.BaseDao.java

package ssh.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import ssh.dao.i.iDao;

public class BaseDao extends HibernateDaoSupport implements iDao {
	
	private static Logger log = LogManager.getLogger(BaseDao.class.getName());

	/**
	 * @Title: findById
	 * @author jason 
	 * @date 2014年7月15日 下午2:20:41 
	 * @Description: 根据Id查询数据 
	 * @param clazz
	 * @param id
	 * @return object
	 */
	@Override
	public Object findById(Class clazz, Serializable id) {
		log.debug("getting instance with id: " + id);
		try {
			Object instance = this.getHibernateTemplate().get(clazz, id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/** 
	 * @Title: findByProperty
	 * @author jason 
	 * @date 2014年7月15日 下午2:20:09 
	 * @Description: 根据条件属性查询 
	 * @param propertyName
	 * @param value
	 * @param clazz
	 * @return List
	 */
	@Override
	public List findByProperty(String propertyName, Object value, Class clazz) {
		try {
			String queryString = "from " + clazz.getSimpleName() +" as model where model." + propertyName + "= ?";
			return this.getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/** 
	 * @Title: findByPropertys
	 * @author jason 
	 * @date 2014年7月15日 下午2:24:32 
	 * @Description: 根据多个条件属性查询 
	 * @param propertyNames
	 * @param values
	 * @param clazz
	 * @return List
	 */
	@Override
	public List findByPropertys(String[] propertyNames, Object[] values,
			Class clazz) {
		try {
			String queryString = "from " + clazz.getSimpleName() +" as model ";
			for(int i=0;i<propertyNames.length;i++){
				if(i==0){
					queryString += "where model." + propertyNames[i] + "= ? ";
				}else{
					queryString += "and model." + propertyNames[i] + "= ? ";
				}
			}
			
			return this.getHibernateTemplate().find(queryString, values);
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/** 
	 * @Title: findAll
	 * @author jason 
	 * @date 2014年7月15日 下午2:27:57 
	 * @Description: 根据具体的类查询数据 
	 * @param clazz
	 * @return List
	 */
	@Override
	public List findAll(Class clazz) {
		log.debug("finding instance by Class");
		try {
			List results = getHibernateTemplate().find("from " + clazz.getSimpleName()); 
			log.debug("find by Class successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by Class failed", re);
			throw re;
		}
	}

	/** 
	 * @Title: findByExample
	 * @author jason 
	 * @date 2014年7月15日 下午2:30:00 
	 * @Description: 根据对象查询 
	 * @param instance
	 * @return List
	 */
	@Override
	public List findByExample(Object instance) {
		// TODO Auto-generated method stub
		return null;
	}

	/** 
	 * @Title: saveBatch
	 * @author jason 
	 * @date 2014年7月15日 下午2:31:12 
	 * @Description: 批量保存
	 * @param instances 
	 * @return boolean
	 */
	@Override
	public boolean saveBatch(final List instances) {
		log.debug("saving batch instances");
		try {
			if(instances!=null&&instances.size()>0){
				getHibernateTemplate().execute(new HibernateCallback(){
			        public Object doInHibernate(Session session)throws HibernateException {
			        	for(Object instance:instances){
			        		session.save(instance);
			        	}
			            return null;
			        }
			    });
			}
		} catch (RuntimeException re) {
			log.error("save batch instances failed", re);
			//throw re;
			return false;
		}
		return true;
	}

	/** 
	 * @Title: updateBatch
	 * @author jason 
	 * @date 2014年7月15日 下午2:34:44 
	 * @Description: 批量更新
	 * @param instances 
	 * @return boolean
	 */
	@Override
	public boolean updateBatch(final List instances) {
		log.debug("updating batch instances");
		try {
			if(instances!=null&&instances.size()>0){
				getHibernateTemplate().execute(new HibernateCallback(){
			        public Object doInHibernate(Session session)throws HibernateException {
			        	for(Object instance:instances){
			        		session.update(instance);
			        	}
			            return null;
			        }
			    });
			}
		} catch (RuntimeException re) {
			log.error("update batch instances failed", re);
			//throw re;
			return false;
		}
		return true;
	}

	/** 
	 * @Title: deleteBatch
	 * @author jason 
	 * @date 2014年7月15日 下午2:37:46 
	 * @Description: 批量删除
	 * @param instances
	 * @return boolean 
	 */
	@Override
	public boolean deleteBatch(final List instances) {
		log.debug("deleting batch instances");
		try {
			if(instances!=null&&instances.size()>0){
				getHibernateTemplate().execute(new HibernateCallback() {
			        public Object doInHibernate(Session session)throws HibernateException {
			        	for(Object instance:instances){
			        		session.delete(instance);
			        	}
			            return null;
			        }
			    });
			}
		} catch (RuntimeException re) {
			log.error("delete batch instances failed", re);
			//throw re;
			return false;
		}
		return true;
	}

	/** 
	 * @Title: delete
	 * @author jason 
	 * @date 2014年7月15日 下午2:39:20 
	 * @Description: 删除数据 
	 * @param instance
	 * @return boolean 
	 */
	@Override
	public boolean delete(Object instance) {
		log.debug("deleting instance");
		try {
			getHibernateTemplate().delete(instance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			//throw re;
			return false;
		}
		return true;
	}

	/** 
	 * @Title: saveORupdate
	 * @author jason 
	 * @date 2014年7月15日 下午2:41:33 
	 * @Description: 更新数据
	 * @param instance 
	 */
	@Override
	public boolean saveORupdate(Object instance) {
		log.debug("saveORupdate instance");
		try{
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("saveORupdate successful");
		}catch (RuntimeException e){
			log.error("saveORupdate failed", e);
			//throw e;
			return false;
		}
		return true;
	}

	/** 
	 * @Title: getCountByCriteria
	 * @author jason 
	 * @date 2014年7月15日 下午2:43:28 
	 * @Description: 统计条数
	 * @param detachedCriteria
	 * @return int
	 */
	@Override
	public int getCountByCriteria(final DetachedCriteria detachedCriteria) {
		String sCount = (String)getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException {    
				Criteria criteria = detachedCriteria.getExecutableCriteria(session);    
				return criteria.setProjection(Projections.rowCount()).uniqueResult().toString();    
			}
		});    
		return Integer.parseInt(sCount);
	}

	/** 
	 * @Title: findByCriteria
	 * @author jason 
	 * @date 2014年7月15日 下午2:45:56 
	 * @Description: 分页查询 
	 * @param detachedCriteria
	 * @param firstResult
	 * @param maxResults
	 * @return List
	 */
	@Override
	public List findByCriteria(final DetachedCriteria detachedCriteria,
			final int firstResult, final int maxResults) {
		return (List) getHibernateTemplate().execute(new HibernateCallback() {    
            public Object doInHibernate(Session session) throws HibernateException {    
            	Criteria criteria = detachedCriteria.getExecutableCriteria(session);
            	if(firstResult>0)
            		criteria.setFirstResult(firstResult);
            	if(maxResults>0)
            		criteria.setMaxResults(maxResults);
                return criteria.list();    
             }    
         });
	}

	/** 
	 * @Title: getCountByHql
	 * @author jason 
	 * @date 2014年7月15日 下午2:48:04 
	 * @Description: 根据HQL 查询记录数
	 * @param hql
	 * @return int
	 */
	@Override
	public int getCountByHql(String hql) {
		String count = getHibernateTemplate().find(hql).listIterator().next().toString();
		return Integer.parseInt(count);
	}

	/** 
	 * @Title: findByHql
	 * @author jason 
	 * @date 2014年7月15日 下午2:49:29 
	 * @Description: 根据HQL 分页 
	 * @param hql
	 * @param firstResult
	 * @param maxResults
	 * @return List
	 */
	@Override
	public List findByHql(final String hql, final int firstResult, final int maxResults) {
		return getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)  
				throws HibernateException {  
				Query query = (Query) session.createQuery(hql);
				if(firstResult>0)
					query.setFirstResult(firstResult);
				
				if(maxResults>0)
					query.setMaxResults(maxResults);
				List list = query.list();  
				return list;  
			}  
		});
	}

	/** 
	 * @Title: deleteByProperty
	 * @author jason 
	 * @date 2014年7月15日 下午2:57:46 
	 * @Description: 根据属性删除 
	 * @param propertyName
	 * @param value
	 * @param clazz 
	 * @return boolean
	 */
	@Override
	public boolean deleteByProperty(final String propertyName,final Object value,final Class clazz) {
		try {
			getHibernateTemplate().execute(new HibernateCallback() {
		        public Object doInHibernate(Session session)throws HibernateException {
		            String hqlDelete = "delete " + clazz.getSimpleName() + " model where model." + propertyName + "= :" + propertyName;
		            session.createQuery( hqlDelete )
		            	.setParameter(propertyName, value).executeUpdate();
		            return null;
		        }
		    });
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			//throw re;
			return false;
		}
		return true;
	}

	/** 
	 * @Title: deleteByHql
	 * @author jason 
	 * @date 2014年7月15日 下午3:00:01 
	 * @Description: 根据HQL 删除对应数据
	 * @param hql
	 * @param clazz
	 * @return boolean 
	 */
	@Override
	public boolean deleteByHql(final String hql, final Class clazz) {
		try {
			getHibernateTemplate().execute(new HibernateCallback() {
		        public Object doInHibernate(Session session)throws HibernateException {
		            String hqlDelete = "delete " + clazz.getSimpleName() + " where " + hql;
		            session.createQuery( hqlDelete ).executeUpdate();
		            return null;
		        }
		    });
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			//throw re;
			return false;
		}
		return true;
	}

	/** 
	 * @Title: deleteByPropertys
	 * @author jason 
	 * @date 2014年7月15日 下午3:01:57 
	 * @Description: 根据多个条件属性删除数据
	 * @param propertyNames
	 * @param values
	 * @param clazz 
	 * @return boolean
	 */
	@Override
	public boolean deleteByPropertys(final String[] propertyNames, final Object[] values,
			final Class clazz) {
		try {
			getHibernateTemplate().execute(new HibernateCallback() {
		        public Object doInHibernate(Session session)throws HibernateException {
					String hqlDelete = "delete " + clazz.getSimpleName() +" model ";
					for(int i=0;i<propertyNames.length;i++){
						String propertyName = propertyNames[i];
						if(i==0){
							hqlDelete += "where model." + propertyName + "= :" + propertyName + " ";
						}else{
							hqlDelete += "and model." + propertyName + "= :" + propertyName + " ";
						}
					}
					Query query = session.createQuery(hqlDelete);
					for(int i=0;i<propertyNames.length;i++){
						String propertyName = propertyNames[i];
						if(values.length>=i){
							query.setParameter(propertyName, values[i]);
						}
						
					}
					query.executeUpdate();
		            return null;
		        }
		    });
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			//throw re;
			return false;
		}
		return true;
	}

}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值