package cn.dao.impl; import java.io.Serializable; import java.util.List; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.stereotype.Repository; @Repository public class BaseDao<T> extends HibernateDaoSupport { /** * 将会话工厂注入给 DAO * @param sessionFactory */ @Autowired public void injectSessionFactory(SessionFactory sessionFactory){ super.setSessionFactory(sessionFactory); } public List<T> findAll(Class<T> entity){ return super.getHibernateTemplate().loadAll(entity); } public Serializable save(T entity){ return super.getHibernateTemplate().save(entity); } }
EmployeeDaoImpl 数据访问层实现
package cn.dao.impl; import java.sql.SQLException; import java.util.List; import org.hibernate.Criteria; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.criterion.DetachedCriteria; import org.hibernate.criterion.MatchMode; import org.hibernate.criterion.Order; import org.hibernate.criterion.Restrictions; import org.springframework.orm.hibernate3.HibernateCallback; import org.springframework.stereotype.Repository; import cn.dao.EmployeeDao; import cn.entity.Employee; @SuppressWarnings("unchecked") @Repository("employeeDao") public class EmployeeDaoImpl extends BaseDao<Employee> implements EmployeeDao { public List<Employee> findAll() { return super.findAll(Employee.class); } public String saveEmployee(Employee employee) { return (String)super.save(employee); } public int deleteEmployee(String id) { return super.getHibernateTemplate().bulkUpdate("delete from Employee e where e.sn=?",id); } public List<Employee> findEmployees( final String name) { //使用Criytera Query return super.getHibernateTemplate().executeFind(new HibernateCallback<List<Employee>>(){ //内部 public List<Employee>doInHibernate(Session session)throws HibernateException, SQLException{ Criteria criteria=session.createCriteria(Employee.class); criteria.add(Restrictions.like("name",name,MatchMode.ANYWHERE)); return criteria.list(); } }); } public long findCount() { return super.getHibernateTemplate().execute(new HibernateCallback<Long>(){ public Long doInHibernate(Session session) throws HibernateException, SQLException { return (Long) session.createQuery("select count(*) from Employee").uniqueResult(); } }); } public List<Employee> findPage(int first, int max) { DetachedCriteria criteria =DetachedCriteria.forClass(Employee.class); criteria.addOrder(Order.desc("sn")); return getHibernateTemplate().findByCriteria(criteria,first,max); } public Employee getEmployee(String sn) { return super.getHibernateTemplate().get(Employee.class, sn); } }
EmployeeBizImpl 业务逻辑层实现
package cn.biz.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import cn.dao.EmployeeDao; import cn.entity.Employee; import cn.biz.EmployeeBiz; @Service("employeeBiz") public class EmployeeBizImpl implements EmployeeBiz{ @Autowired private EmployeeDao employeeDao; //可以不用 set 方法 public Employee login(String sn, String password) { Employee employee=employeeDao.getEmployee(sn); if(employee!=null){ if(employee.getPassword().equals(password)){ return employee; } } return null; } }
EmployeeAction 控制器
package cn.action; import java.util.Map; import javax.annotation.Resource; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import cn.util.Consist; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import cn.biz.EmployeeBiz; import cn.entity.Employee; /** * 由 Spring 管理 * */ @SuppressWarnings("serial") @Controller("employeeAction") @Scope("request") //表示每个请求都会创建一个新的 Action 对象 public class EmployeeAction extends ActionSupport{ @Resource //由 java 提供自动注入注解与 @Autowired 功能相同 //@Qualifier("employeeBiz") 如果名字不同,则用 Qualifier 指定名字 private EmployeeBiz employeeBiz; private Employee employee; public void setEmployeeBiz(EmployeeBiz employeeBiz) { this.employeeBiz = employeeBiz; } public Employee getEmployee() { return employee; } public void setEmployee(Employee employee) { this.employee = employee; } public String login(){ Map<String,Object> session=ActionContext.getContext().getSession(); Employee emp=employeeBiz.login(employee.getSn(), employee.getPassword()); if(emp==null){ addActionError("用户名或密码不正确!"); }else{ if(Consist.EMP_NORMAL.equals(emp.getStatus())){ session.put("employee",emp); return SUCCESS; }else{ addActionError("该用户已经离职,登录失效!"); } } return INPUT; } }
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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 数据源配置 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="oracle.jdbc.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:oracle11" /> <property name="username" value="jboa" /> <property name="password" value="123456" /> </bean> <!-- 会话工厂 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.Oracle10gDialect </prop> <prop key="hibernate.show_sql"> true </prop> </props> </property> <property name="mappingResources"> <list> <value>cn/entity/Employee.hbm.xml</value> <value>cn/entity/Department.hbm.xml</value> <value>cn/entity/Position.hbm.xml</value> </list> </property> </bean> <!-- 指定哪些包需要由 Spring 进行管理 --> <context:component-scan base-package="cn"></context:component-scan> </beans>
web.xml 配置
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <listener> <description>Spring的配置</description> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <description>当 Action 配置成 request 或 session 的时候需要加上这个监听器</description> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <filter> <description>解决Hibernate延迟加载的配置</description> <filter-name>OpenSessionInView</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInView</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <description>struts2的配置</description> <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>