/**
*SSH框架的配置文件
*Spring.XML
*/
<!-- 下面是spring 容器中的DataSource对象,使用C3P0数据源(数据池) -->
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!-- 配置数据源所使用的数据库服务的URL -->
<property name="jdbcUrl" value="jdbc:mysql://172.17.1.16/skyBook?useUnicode=true&characterEncoding=gbk"/>
<!--<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/dojo_tree"/>-->
<!-- 配置数据源所使用的数据库驱动 -->
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<!--<property name="driverClass" value="com.mysql.jdbc.Driver"/>-->
<!-- 配置数据源所使用的数据库用户名 -->
<property name="user" value="root"/>
<!-- 配置数据源所使用的数据库密码 -->
<property name="password" value="root"/>
<!-- 配置数据源连接池的最大连接数 -->
<property name="maxPoolSize" value="300"/>
<!-- 配置数据源连接池的最小连接数 -->
<property name="minPoolSize" value="10"/>
<!-- 配置数据源连接池的初始连接数 -->
<property name="initialPoolSize" value="10"/>
<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="10"/>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="1"/>
<!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
<property name="acquireRetryAttempts" value="10"/>
<!--两次连接中间隔时间,单位毫秒。Default: 1000 -->
<property name="acquireRetryDelay" value="1000"/>
<!--连接关闭时默认将所有未提交的操作回滚。Default: false -->
<property name="autoCommitOnClose" value="false"></property>
<property name="testConnectionOnCheckin" value="true"/>
<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="10"/>
<property name="checkoutTimeout" value="3000"/>
</bean>
<!-- ***************** 定义Hibernate的SessionFactory,必须为其注入一个数据源 **************** -->
<!-- 定义Hibernate的SessionFactory,必须为其注入一个数据源 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 依赖注入数据源 -->
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="sql_show">true</prop>
<prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
<!-- ***************** 配置所有的持久化映射文件 **************** -->
<property name="mappingResources">
<list>
<!--CRMDB数据文件 -->
<value>crm/entity/Employees.hbm.xml</value>
<value>crm/entity/Infoexch.hbm.xml</value>
<value>crm/entity/Contract.hbm.xml</value>
<value>crm/entity/Infoexchcontent.hbm.xml</value>
<value>crm/entity/Project.hbm.xml</value>
</list>
</property>
</bean>
<bean id="hibernateTemplete" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!-- ***************** 配置DAO组件 hibernate.class **************** -->
<bean id="comm" class="hk.com.magenta.common.Common">
<property name="hibernateTemplate">
<ref bean="hibernateTemplete"/>
</property>
</bean>
<bean id="Dao" abstract="true">
<property name="comm">
<ref bean="comm"/>
</property>
</bean>
<!-- ***************** CRM-DAO-实现 **************** -->
<!-- 联系人 -->
<bean id="contactInfoDAO" parent="Dao" class="crm.dao.impl.ContactInfoDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- ****************** CRM-Service ********************* -->
<!-- 联系人Service -->
<bean id="contactInfoServiceImpl" class="crm.service.impl.ContactInfoServiceImpl">
<property name="contactInfoDao" ref="contactInfoDAO"></property>
</bean>
<!-- 关闭SEASS的AOP -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<!-- 切入点指明了在执行hk.magenta.com.service的所有方法时产生事务拦截操作 -->
<aop:pointcut id="daoMethods" expression="execution(* hk.com.magenta.service.impl.*.*(..))"/>
<aop:pointcut id="daoMethod2s" expression="execution(* crm.service.impl.*.*(..))"/>
<!-- 定义了将采用何种拦截操作,这里引用到 txAdvice -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethods" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethod2s" />
</aop:config>
<!-- ****************** CRM-Action ********************* -->
<!-- 联系人Action -->
<bean name="/contactInfo" class="crm.web.action.ContactInfoAction">
<property name="contactInfoService" ref="contactInfoServiceImpl"></property>
</bean>
<!-- 产品-->
<bean name="/product" class="crm.web.action.ProductsAction">
<property name="productTypeService" ref="productTypeServiceImpl"></property>
<property name="productsService" ref="productsServicesImpl"></property>
<property name="houseService" ref="houseServiceImpl"></property>
<property name="joinHouseService" ref="joinHouseServiceImpl"></property>
</bean>
/**
*SSH框架的配置文件
*Struts.XML
*/
<!-- CRM-Action -->
<!-- 联系人Action -->
<action path="/contactInfo" parameter="method"
type="org.springframework.web.struts.DelegatingActionProxy">
</action>
/**
*公用方法接口
*ICommon
*/
package hk.com.magenta.common;
import java.io.Serializable;
import java.sql.Connection;
import java.util.List;
import org.hibernate.Query;
import hk.com.magenta.exception.CommonException;
public interface ICommon<POJO> {
/***
* 执行原生态的sql 语句
* @param sql 需要执行的sql语句
* @return boolean 值
* @throws CommonException
*/
public Object excuteHQL(final String sql)throws CommonException;
/**
* 添加一个对象
* @param pojo
* @throws CommonException 如果添加失败,抛出此异常
*/
public abstract void insertObject(POJO pojo) throws CommonException;
/**
* 修改对象
* @param pojo
* @throws CommonException
*/
public abstract void updateObject(POJO pojo) throws CommonException;
/**
* 根据OID删除指定对象
* @param clazz POJO类
* @param oid 对象标识符
* @throws CommonException
*/
public abstract void deleteObject(Class clazz, Serializable oid)
throws CommonException;
/**
* 根据ID加载一个对象
* @param clazz
* @param oid
* @return
* @throws CommonException
*/
public abstract POJO loadPojoByOid(Class clazz, Serializable oid)
throws CommonException;
/**
* HQL查询--query
* @param hqlexe HqlExecute对象
* @return
* @throws CommonException
*/
public abstract List queryHQL(String hql) throws CommonException;
/**
* HQL查询--find
* @param hqlexe HqlExecute对象
* @return
* @throws CommonException
*/
public abstract List findHQL(String hql,String[] str) throws CommonException;
/**
* HQL查询--findByAll
* @param hqlexe HqlExecute对象
* @return
* @throws CommonException
*/
//public abstract List findByAll(Class clazz) throws CommonException;
/**
* SQL查询
* @param hqlexe HqlExecute对象
* @return
* @throws CommonException
*/
public abstract List querySQL(String hql) throws CommonException;
/**
* HQL分页查询
* @param hqlexe HqlExecute对象
* @return
* @throws CommonException
*/
public abstract List selectPageHQL(String hql,int size,int pagesize) throws CommonException;
/**
* SQL分页查询
* @param hqlexe HqlExecute对象
* @return
* @throws CommonException
*/
public abstract List selectPageSQL(String sql,int size,int pagesize) throws CommonException;
public abstract Connection getConnection() throws CommonException;
public abstract void returnConnection() throws CommonException;
public abstract void deleteObject(POJO pojo) throws CommonException;
}
/**
*公用方法实现接口的类
*Common
*/
package hk.com.magenta.common;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.ejb.CreateException;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import hk.com.magenta.exception.CommonException;
import hk.com.magenta.util.LogUtil;
/**
* 添加一个对象
* 修改一个对象
* 根据OID删除一个对象
* 根据OID查询一个对象
* HQL查询--query
* HQL查询--find
* HQL查询--findByAll
* SQL查询
* HQL分页查询
* SQL分页查询
* @author 戴佳
*
* @param <POJO>
*/
public class Common<POJO> extends HibernateDaoSupport implements ICommon<POJO> {
private static Logger logger = Logger.getLogger("common");
/* (non-Javadoc)
* @see com.aptech.common.ICommon#insertObject(POJO)
* 添加
*/
public void insertObject(POJO pojo) throws CommonException{
if(logger.isDebugEnabled()){
logger.debug("正在添加,用户线程:" + Thread.currentThread());
}
try {
if(logger.isDebugEnabled()){
//logger.debug("session的哈希码:" + this.getHibernateTemplate().getSessionFactory().getCurrentSession().hashCode() + "(" + this.getHibernateTemplate().getSessionFactory().getCurrentSession() + ")");
logger.debug("新增一条记录(dao->insertObject)");
}
this.getHibernateTemplate().save(pojo);
} catch (HibernateException e) {
e.printStackTrace();
logger.error("添加对象异常,信息:" + LogUtil.getLogStack(e));
throw new CommonException(e);
} catch(Exception e){
e.printStackTrace();
logger.error("添加对象异常,信息:" + LogUtil.getLogStack(e));
throw new CommonException(e);
}
}
public void deleteObject(POJO pojo) throws CommonException {
if(logger.isDebugEnabled()){
logger.debug("正在删除,用户线程:" + Thread.currentThread());
}
try {
if(logger.isDebugEnabled()){
//logger.debug("session的哈希码:" + this.getHibernateTemplate().getSessionFactory().getCurrentSession().hashCode() + "(" + this.getHibernateTemplate().getSessionFactory().getCurrentSession() + ")");
logger.debug("删除一条记录(dao->deleteObject)");
}
this.getHibernateTemplate().delete(pojo);
} catch (HibernateException e) {
e.printStackTrace();
logger.error("删除对象异常,信息:" + LogUtil.getLogStack(e));
throw new CommonException(e);
} catch(Exception e){
e.printStackTrace();
logger.error("删除对象异常,信息:" + LogUtil.getLogStack(e));
throw new CommonException(e);
}
}
/* (non-Javadoc)
* @see com.aptech.common.ICommon#updateObject(POJO)
* 修改
*/
public void updateObject(POJO pojo)throws CommonException{
if(logger.isDebugEnabled()){
logger.debug("正在修改,用户线程:" + Thread.currentThread());
}
try {
if(logger.isDebugEnabled()){
//logger.debug("session的哈希码:" + this.getHibernateTemplate().getSessionFactory().getCurrentSession().hashCode() + "(" + this.getHibernateTemplate().getSessionFactory().getCurrentSession() + ")");
logger.debug("更新一条记录(dao->updateObject)");
}
this.getHibernateTemplate().update(pojo);
} catch (HibernateException e) {
e.printStackTrace();
logger.error("修改对象异常,信息:" + LogUtil.getLogStack(e));
throw new CommonException(e);
}catch(Exception e){
e.printStackTrace();
logger.error("修改对象异常,信息:" + LogUtil.getLogStack(e));
throw new CommonException(e);
}
}
/* (non-Javadoc)
* @see com.aptech.common.ICommon#deleteObject(java.lang.Class, java.io.Serializable)
* 删除
*/
public void deleteObject(Class clazz, Serializable oid) throws CommonException{
if(logger.isDebugEnabled()){
logger.debug("正在删除,用户线程:" + Thread.currentThread());
}
try {
if(logger.isDebugEnabled()){
//logger.debug("session的哈希码:" + this.getHibernateTemplate().getSessionFactory().getCurrentSession().hashCode() + "(" + this.getHibernateTemplate().getSessionFactory().getCurrentSession() + ")");
logger.debug("删除一条记录(dao->deleteObject)");
}
Object obj = this.getHibernateTemplate().load(clazz, oid);
this.getHibernateTemplate().delete(obj);
} catch (HibernateException e) {
e.printStackTrace();
logger.error("删除对象异常,信息:" + LogUtil.getLogStack(e));
throw new CommonException(e);
} catch(Exception e){
e.printStackTrace();
logger.error("删除对象异常,信息:" + LogUtil.getLogStack(e));
throw new CommonException(e);
}
}
/* (non-Javadoc)
* @see com.aptech.common.ICommon#loadPojoByOid(java.lang.Class, java.io.Serializable)
* ID查询
*/
public POJO loadPojoByOid(Class clazz, Serializable oid) throws CommonException{
if(logger.isDebugEnabled()){
logger.debug("正在加载对象,用户线程:" + Thread.currentThread());
}
try {
if(logger.isDebugEnabled()){
//logger.debug("session的哈希码:" + this.getHibernateTemplate().getSessionFactory().getCurrentSession().hashCode() + "(" + this.getHibernateTemplate().getSessionFactory().getCurrentSession() + ")");
logger.debug("主建查找记录(dao->loadPojoByOid)");
}
Object obj = this.getHibernateTemplate().get(clazz, oid);
return (POJO) obj;
} catch (HibernateException e) {
e.printStackTrace();
logger.error("对象加载异常,信息:" + LogUtil.getLogStack(e));
throw new CommonException(e);
}catch(Exception e){
e.printStackTrace();
logger.error("添加对象异常,信息:" + LogUtil.getLogStack(e));
throw new CommonException(e);
}
}
/* (non-Javadoc)
* @see com.aptech.common.ICommon#query(com.aptech.query.HqlExecute)
* query hql 查询
*/
public List queryHQL(String hql) throws CommonException{
if(logger.isDebugEnabled()){
logger.debug("正在加载对象,用户线程:" + Thread.currentThread());
}
try {
if(logger.isDebugEnabled()){
//logger.debug("session的哈希码:" + this.getHibernateTemplate().getSessionFactory().getCurrentSession().hashCode() + "(" + this.getHibernateTemplate().getSessionFactory().getCurrentSession() + ")");
logger.debug("HQL-Query语句查找记录(dao->queryHQL)");
}
System.out.println("hql: " + hql);
//return getHibernateTemplate().find(hql, str);
return this.getSession().createQuery(hql).list();
} catch (HibernateException e) {
e.printStackTrace();
logger.error("对象加载异常,信息:" + LogUtil.getLogStack(e));
throw new CommonException(e);
}catch(Exception e){
e.printStackTrace();
logger.error("添加对象异常,信息:" + LogUtil.getLogStack(e));
throw new CommonException(e);
}
}
/* (non-Javadoc)
* @see com.aptech.common.ICommon#query(com.aptech.query.HqlExecute)
* find hql 查询
*/
public List findHQL(String hql,String[] str) throws CommonException{
if(logger.isDebugEnabled()){
logger.debug("正在加载对象,用户线程:" + Thread.currentThread());
}
try {
if(logger.isDebugEnabled()){
//logger.debug("session的哈希码:" + this.getHibernateTemplate().getSessionFactory().getCurrentSession().hashCode() + "(" + this.getHibernateTemplate().getSessionFactory().getCurrentSession() + ")");
logger.debug("HQL-Find语句查找记录(dao->findHQL)");
}
//return getHibernateTemplate().find(hql, str);
return getHibernateTemplate().find(hql, str);
} catch (HibernateException e) {
e.printStackTrace();
logger.error("对象加载异常,信息:" + LogUtil.getLogStack(e));
throw new CommonException(e);
}catch(Exception e){
e.printStackTrace();
logger.error("添加对象异常,信息:" + LogUtil.getLogStack(e));
throw new CommonException(e);
}
}
/* (non-Javadoc)
* @see com.aptech.common.ICommon#query(com.aptech.query.HqlExecute)
* find hql 查询
*/
public List findByAll(Class clazz) throws CommonException{
if(logger.isDebugEnabled()){
logger.debug("正在加载对象,用户线程:" + Thread.currentThread());
}
try {
if(logger.isDebugEnabled()){
//logger.debug("session的哈希码:" + this.getHibernateTemplate().getSessionFactory().getCurrentSession().hashCode() + "(" + this.getHibernateTemplate().getSessionFactory().getCurrentSession() + ")");
logger.debug("查询这个对象的所有记录(dao->findByAll)");
}
//return getHibernateTemplate().find(hql, str);
return getHibernateTemplate().findByExample(clazz);
} catch (HibernateException e) {
e.printStackTrace();
logger.error("对象加载异常,信息:" + LogUtil.getLogStack(e));
throw new CommonException(e);
}catch(Exception e){
e.printStackTrace();
logger.error("添加对象异常,信息:" + LogUtil.getLogStack(e));
throw new CommonException(e);
}
}
/* (non-Javadoc)
* @see com.aptech.common.ICommon#query(com.aptech.query.HqlExecute)
* query sql 查询
*/
public List querySQL(String sql) throws CommonException{
if(logger.isDebugEnabled()){
logger.debug("正在加载对象,用户线程:" + Thread.currentThread());
}
try {
if(logger.isDebugEnabled()){
//logger.debug("session的哈希码:" + this.getHibernateTemplate().getSessionFactory().getCurrentSession().hashCode() + "(" + this.getHibernateTemplate().getSessionFactory().getCurrentSession() + ")");
logger.debug("SQL语句查找记录(dao->querySQL)");
}
return this.getSession().createSQLQuery(sql).list();
} catch (HibernateException e) {
e.printStackTrace();
logger.error("对象加载异常,信息:" + LogUtil.getLogStack(e));
throw new CommonException(e);
}catch(Exception e){
e.printStackTrace();
logger.error("添加对象异常,信息:" + LogUtil.getLogStack(e));
throw new CommonException(e);
}
}
public Connection getConnection() throws CommonException {
if(logger.isDebugEnabled()){
logger.debug("正在加载对象,用户线程:" + Thread.currentThread());
}
try {
if(logger.isDebugEnabled()){
//logger.debug("session的哈希码:" + this.getHibernateTemplate().getSessionFactory().getCurrentSession().hashCode() + "(" + this.getHibernateTemplate().getSessionFactory().getCurrentSession() + ")");
logger.debug("SQL语句查找记录(dao->querySQL)");
}
return this.getSession().connection();
} catch (HibernateException e) {
e.printStackTrace();
logger.error("对象加载异常,信息:" + LogUtil.getLogStack(e));
throw new CommonException(e);
}catch(Exception e){
e.printStackTrace();
logger.error("添加对象异常,信息:" + LogUtil.getLogStack(e));
throw new CommonException(e);
}
}
public void returnConnection() throws CommonException {
if(logger.isDebugEnabled()){
logger.debug("正在加载对象,用户线程:" + Thread.currentThread());
}
try {
if(logger.isDebugEnabled()){
//logger.debug("session的哈希码:" + this.getHibernateTemplate().getSessionFactory().getCurrentSession().hashCode() + "(" + this.getHibernateTemplate().getSessionFactory().getCurrentSession() + ")");
logger.debug("SQL语句查找记录(dao->querySQL)");
}
this.getSession().close();
} catch (HibernateException e) {
e.printStackTrace();
logger.error("对象加载异常,信息:" + LogUtil.getLogStack(e));
throw new CommonException(e);
}catch(Exception e){
e.printStackTrace();
logger.error("添加对象异常,信息:" + LogUtil.getLogStack(e));
throw new CommonException(e);
}
}
/* (non-Javadoc)
* @see com.aptech.common.ICommon#query(com.aptech.query.HqlExecute)
* 使用HQL语句分页 查询
*/
public List selectPageHQL(String hql,int size,int pagesize) throws CommonException{
if(logger.isDebugEnabled()){
logger.debug("正在加载对象,用户线程:" + Thread.currentThread());
}
try {
if(logger.isDebugEnabled()){
//logger.debug("session的哈希码:" + this.getHibernateTemplate().getSessionFactory().getCurrentSession().hashCode() + "(" + this.getHibernateTemplate().getSessionFactory().getCurrentSession() + ")");
logger.debug("SQL语句查找记录(dao->querySQL)");
}
List list =new ArrayList();
Query query = this.getSession().createQuery(hql);
// 得到总条数
// int all_page = query.list().size()/pagesize;
// list.add(all_page);
query.setFirstResult(size); //从第几条数据开始
query.setMaxResults(pagesize); //取几条数据
return query.list();
} catch (HibernateException e) {
e.printStackTrace();
logger.error("对象加载异常,信息:" + LogUtil.getLogStack(e));
throw new CommonException(e);
}catch(Exception e){
e.printStackTrace();
logger.error("添加对象异常,信息:" + LogUtil.getLogStack(e));
throw new CommonException(e);
}
}
/* (non-Javadoc)
* @see com.aptech.common.ICommon#query(com.aptech.query.HqlExecute)
* 使用SQL语句分页 查询
*/
public List selectPageSQL(String sql,int size,int pagesize) throws CommonException{
if(logger.isDebugEnabled()){
logger.debug("正在加载对象,用户线程:" + Thread.currentThread());
}
try {
if(logger.isDebugEnabled()){
//logger.debug("session的哈希码:" + this.getHibernateTemplate().getSessionFactory().getCurrentSession().hashCode() + "(" + this.getHibernateTemplate().getSessionFactory().getCurrentSession() + ")");
logger.debug("SQL语句查找记录(dao->querySQL)");
}
List list =new ArrayList();
Query query = this.getSession().createSQLQuery(sql);
// 得到总条数
// int all_page = query.list().size()/pagesize;
// list.add(all_page);
query.setFirstResult(size); //从第几条数据开始
query.setMaxResults(pagesize); //取几条数据
return query.list();
} catch (HibernateException e) {
e.printStackTrace();
logger.error("对象加载异常,信息:" + LogUtil.getLogStack(e));
throw new CommonException(e);
}catch(Exception e){
e.printStackTrace();
logger.error("添加对象异常,信息:" + LogUtil.getLogStack(e));
throw new CommonException(e);
}
}
public Object excuteHQL(final String sql)throws CommonException{
System.out.println(sql);
return getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Connection conn = session.connection();
PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql);
boolean flag =ps.execute();
ps.close();
session.flush();
return flag;
}
});
//return null;
}
}