您的位置:首页 > 编程语言

SSH复用代码最终版

2017-05-22 21:02 337 查看

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();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: