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>
效果图:
相关推荐
事务处理,ssh事务处理
ssh2+注解OA系统ssh2+注解OA系统ssh2+注解OA系统ssh2+注解OA系统ssh2+注解OA系统
ssh注解案例。主要是jar包的齐全,对于初学者是个很好的东西。
ssh集成的OA系统 便捷而有方便的操作软件!!!
适合ssh初学者,分享促进进步
这是一个基于ssh的项目,后台技术,前端不太好看,基础功能很不错,有树状结构,权限管理等功能,还可以论坛聊天,附带数据库。可以拿来直接用,是代码。
myeclipse开发的简单ssh架构实现的oa系统,由于太大分了两个包,解压导入即可以使用(jar包也在里面了),实现了:个人通讯录,日程安排,工作日志,短消息管理,公告管理,会议管理功能,可能功能比较少,不需要的就...
SSH整合OA项目,数据库都有
一个基于SSH注解进行开发的小实例。非常适合初学者。~
声明式事务处理声明式事务处理声明式事务处理
flex4+ssh做的一个oa系统;flex4+ssh做的一个oa系统flex4+ssh做的一个oa系统
java oa SSH+jbmp开发的小oa,含丰富的文档
关于ssh的注解配置,通过学习此文档,了解ssh的配置问题
ssh全注解注释,hibernate+struts2+spring注解的案例,框架的搭建
SSH笔记-事务管理,包含通过注解的方式配置事务和通过xml的方式配置事务
用ssh框架写的OA项目,仅供参考
2016年javaWeb学习SSH注解整合例子demo
使用SSH开发oa办公自动化系统 包含可运行源码 数据库及脚本 用户手册 需求说明
SSH整合,纯注解,包含事务,零配置,代码高度复用