基于S2SH框架开发项目
一、 创建项目包分别为:
manage 业务接口
manageImpl 业务接口实现类
servlet 操作数据库接口
servletImpl 操作数据库接口实现类
action action包
entity/model 实体Bean包
包命名规则:
为实现包名的唯一性,一般采用公司网站域名+项目名称+实现功能来命名例如:
www.xxx.com
包名就命名为(主要实现唯一性):
com. xxx.www.wscp.login.manage
com. xxx.www.wscp.login.manageImpl
com. xxx.www.wscp.login.servlet
com. xxx.www.wscp.login.servletImpl
com. xxx.www.wscp.login.action
com. xxx.www.wscp.login.entity
二、实体bean映射
现在我们以搭建好的S2SH框架开始编程
我们可以用Hibernate perspective来翻转映射实体类:如图
我们看到我们连接数据库的文件名字,打开,如图:
找到我们需要的表右键
设置映射:注意项目一定要选择正确了,需要映射到的包内
点击Next
选择对应表主键的规则来选择其中一项native是主键自增长,assigned是自定义
点击Finish
这是我们返回到Myeclipse就可以看到我们的实体类与映射文件就全部映射出来了同时我映射的时候也有加springDao文件,这个文件我们选用就可以。
到这里我们实体类映射完毕,根据自己的需求映射完毕的也许达不到你的实际要求,需要手动配置与修改。
三、 根据映射的表编写接口与接口实现类
新建接口
编写接口名称
StudentManage接口代码:
package com.xxx.www.wscp.login.manage;
import java.util.List;
import com.xxx.www.wscp.login.entity.Student;
public interface StudentManage {
public void addStudentManage(Student stu);
public void delStudentManage(Student stu);
public void updStudentManage(Student stu);
public Student selStudentManage(int id);
public List selAllStudentManage();
}
建立StudentManage接口实现类
接口选择我们刚刚自定义的接口,并且引用org.springframework.orm.hibernate3.support.HibernateDaoSupport中的
HibernateDaoSupport类
StudentManageImpl实现类代码:
package com.xxx.www.wscp.login.manageImpl;
import java.util.List;
importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.xxx.www.wscp.login.entity.Student;
import com.xxx.www.wscp.login.manage.StudentManage;
public class StudentManageImpl extends HibernateDaoSupport implements
StudentManage {
public void addStudentManage(Student stu) {
getHibernateTemplate().save(stu);
}
public void delStudentManage(Student stu) { getHibernateTemplate().delete(stu);
}
public void updStudentManage(Student stu) {
getHibernateTemplate().update(stu);
}
public Student selStudentManage(int id) {
List list =getHibernateTemplate().find("From Student where id="+id);
Student student=null;
for(int i = 0;i<list.size();i++){
student = (Student)list.get(i);
}
return student;
}
public List selAllStudentManage() {
return getHibernateTemplate().find("From Student");
}
}
这样我们实现了对实体类Student的增删改查的实现,查询分别有按照ID查到1个人与查找所有人的方法。
由于我们是框架整合开发,所以我们要把实现类交给Spring来管理。所以我们需要在applicationContext.xml中添加如下代码(<property name="sessionFactory"ref="sessionFactory"></property>这里是由于我们使用了Hibernate中的类,所以需要这样写与引用)。
<bean id="studentManage" class="com.xxx.www.wscp.login.manageImpl.StudentManageImpl">
<property name="sessionFactory"ref="sessionFactory"></property>
</bean>
业务接口的编写如下:
首先我们需要添加接口:
方法如上面添加接口相同,但是命名有区别加入的包不同。
接口加入到:com.xxx.www.wscp.login.servlet中
命名为:StudentServlet
StudentServlet接口代码如下
package com.xxx.www.wscp.login.servlet;
import java.util.List;
import com.xxx.www.wscp.login.entity.Student;
public interface StudentSerclet {
public boolean addStudentServlet(Student stu);
public boolean delStudentServlet(Student stu);
public boolean updStudentServlet(Student stu);
public Student getStudentServlet(int id);
public List listStudentServlet();
}
下面我们开始编写业务接口的实现类
这次我们只需要继承StudentServlet接口就可以
业务接口实现类加入到:com.xxx.www.wscp.login.servletImpl中
命名为:StudentServletImpl
其中我们需要使用在StudentManage接口中的实现方法。所以我们注入
StudentManage并且实现set方法。
StudentServletImpl代码如下:
package com.xxx.www.wscp.login.servletImpl;
import java.util.ArrayList;
import java.util.List;
importcom.xxx.www.wscp.login.entity.Student;
importcom.xxx.www.wscp.login.manage.StudentManage;
import com.xxx.www.wscp.login.servlet.StudentServlet;
public class StudentServletImpl implementsStudentServlet {
privateStudentManage studentManage;
publicvoid setStudentManage(StudentManage studentManage) {
this.studentManage= studentManage;
}
publicboolean addStudentServlet(Student stu) {
try{
studentManage.addStudentManage(stu);
returntrue;
}catch (Exception e) {
e.printStackTrace();
returnfalse;
}
}
publicboolean delStudentServlet(Student stu) {
try{
studentManage.delStudentManage(stu);
returntrue;
}catch (Exception e) {
e.printStackTrace();
returnfalse;
}
}
publicboolean updStudentServlet(Student stu) {
try{
studentManage.updStudentManage(stu);
returntrue;
}catch (Exception e) {
e.printStackTrace();
returnfalse;
}
}
publicStudent getStudentServlet(int id) {
Studentstudent = null;
try{
student= studentManage.selStudentManage(id);
}catch (Exception e) {
e.printStackTrace();
}
returnstudent;
}
publicList listStudentServlet() {
Listlist = new ArrayList();
try{
list= studentManage.selAllStudentManage();
}catch (Exception e) {
e.printStackTrace();
}
returnlist;
}
}
这里可多样写法并且可实现多个接口。现在所写的是最基本的增删改查操作。
由于我们是框架整合开发,所以我们要把业务实现类交给Spring来管理。所以我们需要在applicationContext.xml中添加如下代码
<!-- 业务实现类交给Spring来管理 -->
<bean id="studentServlet" class="com.xxx.www.wscp.login.servletImpl.StudentServletImpl">
<!-- name必须是在studentServletImpl类中注入的方法名字 ref必须是在Spring中所有的bean id所有的名字,所写不明白问我。我来口述。 -->
<property name="studentManage"ref="studentManage"></property>
</bean>
好了,至此。我们把接口与方法都写好了。下面我们开始写表示层,并且写Action来使用我们定义的这些方法。
四、 Action编写与jsp的配合
在com.xxx.www.wscp.login.action包中新建IndexAction
在struts.xml中添加
<action name="index" class="indexaction">
<result name="index">index.jsp</result>
</action>
在applicationContext.xml中添加
<bean id="indexaction" class="com.xxx.www.wscp.login.action.IndexAction">
</bean>
现在我们做查询全部的功能。
我们首先需要注入业务类
IndexAction代码如下:
package com.xxx.www.wscp.login.action;
import java.util.List;
import com.opensymphony.xwork2.ActionContext;
import com.xxx.www.wscp.login.servlet.StudentServlet;
public class IndexAction {
private StudentServlet studentServlet;
public void setStudentServlet(StudentServletstudentServlet) {
this.studentServlet = studentServlet;
}
public String execute(){
List list = studentServlet.listStudentServlet();
//此处是记录list。如同request传值。
ActionContext.getContext().put("studentall", list);
return "index";
}
}
我们在applicationContext.xml中添加(红的内容)<bean id="indexaction"class="com.xxx.www.wscp.login.action.IndexAction">
<propertyname="studentServlet" ref="studentServlet"></property>
</bean>
由于是查询功能并不需要事务的处理,所以我们没有做事务的处理。
接下来修改index.jsp页面来显示查询出来的内容
<body>
<s:form action="innaction"name="Form1">
<table border="1">
<tr>
<td>ID</td>
<td>姓名</td>
<td>地址</td>
<td>电话</td>
</tr>
<s:iterator value="studentall">
<tr>
<td><s:property value="stuId" /></td>
<td><s:property value="stuname" /></td>
<td><s:property value="address" /></td>
<td><s:property value="stuphone" /></td>
</tr>
</s:iterator>
</table>
</s:form>
</body>
然后这时我们部署项目运行http://127.0.0.1:8080/demos/index.action看看是不是已经显示出来表内所有的信息
好了。我们上面是查询因此我们没有用到事务,现在我们做一个添加成员来使用上事务处理。。
我们在index.jsp页面中做一个添加按钮然后传到add.jsp页面:
<a href="<%=basePath%>add.jsp">添加人员</a>
我们新建add.jsp页面
add.jsp页面代码:
<s:form name="from"action="addstuaction">
<s:textfield name="id"label="id" ></s:textfield>
<s:textfield name="username"label="username" ></s:textfield>
<s:textfield name="address"label="address" ></s:textfield>
<s:textfield name="phone"label="phone" ></s:textfield>
<s:submit></s:submit>
</s:form>
我们在add.jsp页面中定义了跳转action为addstuaction。
现在我们新建立Action。命名为:AddStudentAction 并且在struts.xml中添加applicationContext.xml中添加
applicationContext.xml添加代码:
<bean id="addStudent" class="com.xxx.www.wscp.login.action.AddStudentAction">
</bean>
struts.xml添加代码
<action name="addstuaction"class="addStudent">
<result name="ok" type="redirect">index.action</result>
<result name="no">add.jsp</result>
</action>
现在我们开始编写AddStudentAction的代码
代码如下:
package com.xxx.www.wscp.login.action;
import java.math.BigDecimal;
import com.xxx.www.wscp.login.entity.Student;
import com.xxx.www.wscp.login.servlet.StudentServlet;
public class AddStudentAction {
private BigDecimal id;
private String username;
private String address;
private String phone;
public BigDecimal getId() {
return id;
}
public void setId(BigDecimal id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
private StudentServlet studentServlet;
public void setStudentServlet(StudentServlet studentServlet) {
this.studentServlet = studentServlet;
}
public String execute(){
Student stu = new Student();
stu.setStuId(id);
stu.setStuname(username);
stu.setAddress(address);
stu.setStuphone(phone);
if(studentServlet.addStudentServlet(stu)){
return "ok";
}
else{
return "no";
}
}
}
我们这里使用了注入,private StudentServlet studentServlet;
public void setStudentServlet(StudentServlet studentServlet) {
this.studentServlet = studentServlet;
所以我们需要在applicationContext.xml中定义。并且我们做的是添加功能需要用到事务处理。所以我们现在开始修改applicationContext.xml
由于我们studentServlet这个注入在查询的时候也用到了,并且把studentServlet类交给Spring管理了现在我们直接使用就可以。
我们打开事务代码:
<bean id="fwxxBiz"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager"ref="transactionManager" />
<property name="target"ref="studentServlet" />
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED </prop>
<prop key="del*">PROPAGATION_REQUIRED </prop>
<prop key="update*">PROPAGATION_REQUIRED </prop>
<prop key="do*">PROPAGATION_REQUIRED </prop>
<prop key="*">PROPAGATION_REQUIRED </prop>
</props>
</property>
</bean>
上边为事务代码。红色并且带有下划线处是我们注意的。Ref是要求与以下注入的id同名的
<!-- 业务实现类交给Spring来管理 -->
<bean id="studentServlet" class="com.xxx.www.wscp.login.servletImpl.StudentServletImpl">
<!-- name必须是在studentServletImpl类中注入的方法名字 ref必须是在Spring中所有的bean id所有的名字,所写不明白问我。我来口述。 -->
<property name="studentManage"ref="studentManage"></property>
</bean>
这样就实现了添加功能并且使用了事务处理。其他方法使用都是此步骤。

1534

被折叠的 条评论
为什么被折叠?



