环境:oracle11g、myeclipse2014
首先在web项目中添加spring框架
现在已经添加完spring框架了
然后我们开始添加Hibernate框架
到这一步Hibernate框架就添加完成了
applicationContext.xml配置文件
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
org.hibernate.dialect.Oracle9Dialect
true
true
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
然后开始配置struts2框架
到现在struts2框架也整合进来了
然后使用Hibernate的反向工程创建实体类
点击finish,然后实体类就创建好了,接下来就可以开始写代码了
dao层接口
packagecom.bdqn.dao;importjava.util.List;importcom.bdqn.pojo.Emp;public interfaceEmpDao {public ListfindAll();
}
dao层实现
packagecom.bdqn.dao.impl;importjava.util.List;importorg.hibernate.SessionFactory;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.beans.factory.annotation.Qualifier;importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;importorg.springframework.stereotype.Repository;importcom.bdqn.dao.EmpDao;importcom.bdqn.pojo.Emp;
@Repository("empDao")public class EmpDaoImpl extends HibernateDaoSupport implementsEmpDao {
@Autowiredpublic EmpDaoImpl(@Qualifier("sessionFactory") SessionFactory sessionFactory) {this.setSessionFactory(sessionFactory);
}publicEmpDaoImpl() {
}
@Overridepublic ListfindAll() {//TODO Auto-generated method stub
return this.getHibernateTemplate().find("from Emp");
}
}
service层接口
packagecom.bdqn.service;importjava.util.List;importcom.bdqn.pojo.Emp;public interfaceEmpService {public ListfindAll();
}
service层实现
packagecom.bdqn.service.impl;importjava.util.List;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importorg.springframework.transaction.annotation.Transactional;importcom.bdqn.dao.EmpDao;importcom.bdqn.pojo.Emp;importcom.bdqn.service.EmpService;
@Service("empService")
@Transactionalpublic class EmpServiceImpl implementsEmpService {
@AutowiredprivateEmpDao empDao;publicEmpDao getEmpDao() {returnempDao;
}public voidsetEmpDao(EmpDao empDao) {this.empDao =empDao;
}
@Override
@Transactional(readOnly= true)public ListfindAll() {//TODO Auto-generated method stub
returnempDao.findAll();
}
}
Action(web):
packagecom.bdqn.web;importjava.util.List;importjava.util.Map;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;importcom.bdqn.pojo.Emp;importcom.bdqn.service.EmpService;importcom.opensymphony.xwork2.ActionContext;importcom.opensymphony.xwork2.ActionSupport;
@Controllerpublic class EmpAction extendsActionSupport {
@AutowiredprivateEmpService empService;publicEmpService getEmpService() {returnempService;
}public voidsetEmpService(EmpService empService) {this.empService =empService;
}publicString execute(){
Map request = (Map) ActionContext.getContext().get("request");
List emps =empService.findAll();
request.put("emps", emps);returnSUCCESS;
}
}
struts.xml配置文件
/index.jsp
jsp页面:
String basePath= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
My JSP 'index.jsp' starting page${emp.ename }
到这一步恭喜,你的ssh项目搭建完了
然后部署项目发布