applicationContext.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:context="http://www.springframework.org/schema/context"
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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 配置EmployeeService对象 -->
<bean id="employeeService" class="com.service.imp.EmployeeService">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="departmentService" class="com.service.imp.DepartmentService">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="jdbc:sqlserver://localhost:1433"></property>
<property name="username" value="sa"></property>
<property name="password" value="hyj84884824"></property>
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<!-- 连接吃启动的初始值 -->
<property name="initialSize" value="3"/>
<!-- 连接池最大值 -->
<property name="maxActive" value="500"/>
<!-- 最大空闲值 -->
<property name="maxIdle" value="2"/>
<!-- 最小空闲值,当空闲的连接数少于阀值,连接池会再去申请一些连接,防止洪峰来时 -->
<property name="minIdle" value="1"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<!-- 接管了hibernate对象映射文件 -->
<property name="mappingResources">
<list>
<value>com/domain/Employee.hbm.xml</value>
<value>com/domain/Department.hbm.xml</value>
</list>
</property>
<!-- 方言等。 -->
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.SQLServerDialect
hibernate.hbm2ddl.auto=update
</value>
</property>
</bean>
<!-- 配置事务管理器,统一管理sessionFactory的事务 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 启用事务注解 -->
<tx:annotation-driven transaction-manager="txManager"/>
<!-- 配置action -->
<bean name="/login" class="com.yourcompany.struts.action.LoginAction" scope="prototype">
<property name="employeeServiceInter" ref="employeeService"/>
</bean>
<bean name="/addEmp" class="com.yourcompany.struts.action.EmployeeAction" scope="prototype">
<property name="departmentServicceInter" ref="departmentService"/>
<property name="employeeServiceInter" ref="employeeService"/>
</bean>
<bean name="/logout" class="com.yourcompany.struts.action.LoginAction" scope="prototype"/>
<bean name="/employee" class="com.yourcompany.struts.action.EmployeeAction"/>
<bean name="/goListEmpUi" class="com.yourcompany.struts.action.EmployeeAction">
<property name="employeeServiceInter" ref="employeeService"/>
</bean>
<bean name="/ChangeEmpUi" class="com.yourcompany.struts.action.EmployeeAction">
<property name="employeeServiceInter" ref="employeeService"/>
<property name="departmentServicceInter" ref="departmentService"/>
</bean>
<bean name="/GoChangeEmpUi" class="com.yourcompany.struts.action.EmployeeAction">
<property name="employeeServiceInter" ref="employeeService"/>
</bean>
<bean name="/delEmp" class="com.yourcompany.struts.action.EmployeeAction">
<property name="employeeServiceInter" ref="employeeService"/>
</bean>
</beans>
BasicService:
package com.basic;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;
//事务注解,spring统一管理事务的提交
@Transactional
public abstract class BasicService implements BasicServiceInter {
private SessionFactory sessionFactory;
@Override
public Object findById(Class clazz, Serializable id) {
// TODO Auto-generated method stub
return sessionFactory.getCurrentSession().get(clazz, id);
}
@Override
public List exrcuteQuery(String hql, Object[] parameter) {
// TODO Auto-generated method stub
System.out.println("得到hql为:"+hql);
Session session=sessionFactory.getCurrentSession();
Query query=session.createQuery(hql);
if(parameter!=null&¶meter.length>0){
for(int a=0;a<parameter.length;a++){
query.setParameter(a, parameter[a]);
}
}
return query.list();
}
@Override
public List executeQueryByPage(String hql, Object[] parameter, int PageNow, int PageSize) {
// TODO Auto-generated method stub
Query query=sessionFactory.getCurrentSession().createQuery(hql);
if(parameter!=null&¶meter.length>0){
for(int a=0;a<parameter.length;a++){
query.setParameter(a, parameter[a]);
}
}
query.setFirstResult((PageNow-1)*PageSize);
query.setMaxResults(PageSize);
return query.list();
}
@Override
public void add(Object obj) {
// TODO Auto-generated method stub
sessionFactory.getCurrentSession().save(obj);
}
@Override
public List executeUpdate(String hql, Object[] parameter) {
// TODO Auto-generated method stub
return null;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
System.out.println("往basicService注入SessionFactory");
this.sessionFactory = sessionFactory;
}
public Object uniqueQuery(String hql,Object []parameter){
Query query=sessionFactory.getCurrentSession().createQuery(hql);
if(parameter!=null&¶meter.length>0){
for(int a=0;a<parameter.length;a++){
query.setParameter(a, parameter[a]);
}
}
return query.uniqueResult();
}
//获取总页数
public Long getPageCount(String hql,String []parameter,int pageSize){
return (((Long) uniqueQuery(hql, null))-1)/pageSize+1;
}
//根据id删除对象
public void delById(Class clazz,Serializable id){
Session session=sessionFactory.getCurrentSession();
session.delete(this.findById(clazz, id));
}
}