SSH项目搭建步骤

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

SSH项目搭建步骤

按顺序添加功能支持:

1、Spring3------------配置文件applicationContext.xml

2、Hibernate3.3------配置文件hibernate.cfg.xml

3、Struts2-------------配置文件struts.xml

一、建立web项目

二、添加spring支持

选择spring所需的jar包

下一步产生spring配置文件applicationContext.xml

三、给项目添加Hibernate支持

这里询问是否将

hibernate和spring的配

置文件合并

四、给项目添加struts2支持

产生的配置文件:

五、配置启动环境:

a)配置web.xml,让启动时能加载spring容器环境

b)配置struts.xml,让spring来管理其Action

c)配置hibernate.cfg.xml(非必须)

六、创建包com.scce.bean存放实体类:

package com.scce.bean;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue; import javax.persistence.Id;

//标识为实体类

@Entity

public class Employee {

private int empId;

private int empDeptId;

private String empName;

private String empPwd;

//标识主键列及生成方式,这里自增

@Id

@GeneratedValue

public int getEmpId() {

return empId;

}

public void setEmpId(int empId) {

this.empId = empId;

}

public int getEmpDeptId() {

return empDeptId;

}

public void setEmpDeptId(int empDeptId) { this.empDeptId = empDeptId;

}

public String getEmpName() {

return empName;

}

public void setEmpName(String empName) { this.empName = empName;

}

public String getEmpPwd() {

return empPwd;

}

public void setEmpPwd(String empPwd) {

this.empPwd = empPwd;

}

}

然后在hibernate中增加加载该实体类:

第一种方式(注解方式)

第二种方式,给实体类增加Employee.hbm.xml文件,然后配置xml方式也可

七、建立EmployeeDao,这里为简化,方便看的一种

package com.scce.dao;

import org.hibernate.Session;

import

org.springframework.orm.hibernate3.support.HibernateDaoSupport; import java.util.*;

public class EmployeeDao extends HibernateDaoSupport {

// 登录判断

public boolean checkLogin(String empName, String empPwd) { String hql = "from Employee where empName = '" + empName + "' and empPwd='" + empPwd + "'";

List lst = this.getHibernateTemplate().find(hql);

if (lst.size() > 0) {

return true;

}

return false;

}

}

在spring中配置DAO,注入SessionFactory

将sessionFactory

注入到dao中

----------------------------特别注意,因为上面使用了注解实体类,所以sessionFactory的bean 必须更改为AnnotationSessionFactoryBean

class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFac toryBean">

-------------------------------------------如果是合并的方式,请注意看以下配置:

class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

org.hibernate.dialect.Oracle9Dialect

true

相关文档
最新文档