SSH複用代碼最終版

web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:bean.xml</param-value>
    </context-param>
    <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>*.action</url-pattern>
    </filter-mapping>
</web-app>

bean.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <import resource="classpath:zhongfucheng/test/config/test-spring.xml"/>

    <!-- 導入外部的properties配置文件 -->
    <context:property-placeholder location="classpath:db.properties" />

    <!-- 配置c3p0數據源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="jdbcUrl" value="${jdbcUrl}"></property>
        <property name="driverClass" value="${driverClass}"></property>
        <property name="user" value="${user}"></property>
        <property name="password" value="${password}"></property>
        <!--初始化時獲取三個鏈接,取值應在minPoolSize與maxPoolSize之間。Default: 3 -->
        <property name="initialPoolSize" value="${initialPoolSize}"></property>
        <!--鏈接池中保留的最小鏈接數。Default: 3 -->
        <property name="minPoolSize" value="3"></property>
        <!--鏈接池中保留的最大鏈接數。Default: 15 -->
        <property name="maxPoolSize" value="${maxPoolSize}"></property>
        <!--當鏈接池中的鏈接耗盡的時候c3p0一次同時獲取的鏈接數。Default: 3 -->
        <property name="acquireIncrement" value="3"></property>
        <!--最大空閒時間,1800秒內未使用則鏈接被丟棄,若爲0則永不丟棄。Default: 0 -->
        <property name="maxIdleTime" value="1800"></property>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="javax.persistence.validation.mode">none</prop>
            </props>
        </property>
        <property name="mappingLocations">
            <list>
                <value>classpath:zhongfucheng/test/entity/*.hbm.xml</value>
            </list>
        </property>
    </bean>


    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="find*" read-only="true" />
            <tx:method name="get*" read-only="true" />
            <tx:method name="load*" read-only="true" />
            <tx:method name="list*" read-only="true" />
            <tx:method name="search*" read-only="true" />
            <tx:method name="*" rollback-for="Throwable" />
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut id="serviceOperation" expression="bean(*Service*)" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
    </aop:config>

    <!-- 全部業務dao的parent -->
    <bean id="baseDao" abstract="true">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
</beans>

log4j.properties文件:

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p [%t] %c{1}:%L - %m%n

#默認從warn開始
log4j.rootLogger=warn, stdout, R

log4j.logger.zhongfucheng.test=debug

log4j.appender.R=org.apache.log4j.DailyRollingFileAppender

#R.File是把日誌信息輸出到哪裏
log4j.appender.R.File=D:/itcast/itcast.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d [%t] %5p  %c - %m%n

db.properties文件

--------------------mysql---------------------- jdbcUrl=jdbc:mysql://localhost:3306/SSH03?useUnicode=true&characterEncoding=utf8 driverClass=com.mysql.jdbc.Driver user=root password=root initialPoolSize=10 maxPoolSize=30 --------------------sqlserver---------------------- jdbcUrl=jdbc:sqlserver://localhost:1433;DatabaseName=zhongfucheng driverClass=com.microsoft.sqlserver.jdbc.SQLServerDriver user=sa password=sql initialPoolSize=10 maxPoolSize=30 

Struts2.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>

    <!-- 禁用動態方法訪問 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <!-- 配置成開發模式 -->
    <constant name="struts.devMode" value="true" />
    <!-- 配置拓展名爲action -->
    <constant name="struts.action.extention" value="action" />
    <!-- 把主題配置成simple -->
    <constant name="struts.ui.theme" value="simple" />

    <include file="zhongfucheng/test/config/test-Struts2.xml"/>

</struts>

BaseDao.java

package zhongfucheng.core.dao; import java.io.Serializable; import java.util.List; public interface BaseDao<T> { //新增 public void save(T entity); //更新 public void update(T entity); //根據id刪除 public void delete(Serializable id); //根據id查找 public T findObjectById(Serializable id); //查找列表 public List<T> findObjects(); } 

baseDaoImpl.java

package zhongfucheng.core.dao.impl;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import zhongfucheng.core.dao.BaseDao;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;


public abstract class BaseDaoImpl<T> extends HibernateDaoSupport implements BaseDao<T> {

    Class<T> clazz;

    public BaseDaoImpl(){
        ParameterizedType pt =  (ParameterizedType)this.getClass().getGenericSuperclass();//BaseDaoImpl<User>
        clazz = (Class<T>)pt.getActualTypeArguments()[0];
    }

    @Override
    public void save(T entity) {
        getHibernateTemplate().save(entity);
    }

    @Override
    public void update(T entity) {
        getHibernateTemplate().update(entity);
    }

    @Override
    public void delete(Serializable id) {
        getHibernateTemplate().delete(findObjectById(id));
    }

    @Override
    public T findObjectById(Serializable id) {
        return getHibernateTemplate().get(clazz, id);
    }

    @Override
    public List<T> findObjects() {
        Query query = getSession().createQuery("FROM " + clazz.getSimpleName());
        return query.list();
    }

}
<script type="text/javascript"> $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split('\n').length; var $numbering = $('<ul/>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('<li/>').text(i)); }; $numbering.fadeIn(1700); }); }); </script>
相關文章
相關標籤/搜索