版权声明:博客知识产权来源命运的信徒,切勿侵权 https://blog.csdn.net/qq_37591637/article/details/84973461
Hibernate部分
实体类Customer,同样是Action类
package cn.com.dao;
import java.util.List;
import java.util.Map;
import org.apache.struts2.interceptor.RequestAware;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.ValueStack;
import cn.com.util.Hibernate_Utils;
public class Customer implements RequestAware {
private Integer id;
private String name;
private String source;
private String industy;
private String level;
private String phone;
private String moblie;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getIndusty() {
return industy;
}
public void setIndusty(String industy) {
this.industy = industy;
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getMoblie() {
return moblie;
}
public void setMoblie(String moblie) {
this.moblie = moblie;
}
/*
* author:命运的信徒date:2018/12/12arm:hibernate+Struts2结合,在页面上对数据库的数据进行增删改查
*/
private Map<String, Object> requetsmap;
@Override
public void setRequest(Map<String, Object> arg0) {
// TODO Auto-generated method stub
requetsmap = arg0;
}
public String query() {
// 1.连接数据库
Session session = Hibernate_Utils.openSession();
Transaction tx = session.beginTransaction();
// 2.查询语句
Query query = session.createQuery("from Customer");
List<Customer> list = query.list();
requetsmap.put("list", list);
tx.commit();
session.close();
return "query";
}
public String add() {
return "add";
}
public String addinfo() {
// 增加信息
Session session = Hibernate_Utils.openSession();
Transaction tx = session.beginTransaction();
Customer c = new Customer(id, name, source, industy, level, phone,
moblie, requetsmap);
session.save(c);
tx.commit();
session.close();
return "addinfo";
}
public String delete() {
// 删除信息
Session session = Hibernate_Utils.openSession();
Transaction tx = session.beginTransaction();
//查询,由于页面
Customer c = session.get(Customer.class, id);
session.delete(c);
tx.commit();
session.close();
return "delete";
}
public String update() {
// 修改信息
Session session = Hibernate_Utils.openSession();
Transaction tx = session.beginTransaction();
//查询,由于页面
Query query= session.createQuery("from Customer where id=?");
query.setLong(0, id);
Customer c=(Customer) query.uniqueResult();
ValueStack vs=ActionContext.getContext().getValueStack();
vs.push(c);
return "update";
}
public String up() {
delete();
addinfo();
return "up";
}
public Customer(Integer id, String name, String source, String industy,
String level, String phone, String moblie,
Map<String, Object> requetsmap) {
super();
this.id = id;
this.name = name;
this.source = source;
this.industy = industy;
this.level = level;
this.phone = phone;
this.moblie = moblie;
this.requetsmap = requetsmap;
}
public Customer() {
super();
}
@Override
public String toString() {
return "Customer [id=" + id + ", name=" + name + ", source=" + source
+ ", industy=" + industy + ", level=" + level + ", phone="
+ phone + ", moblie=" + moblie + "]";
}
}
实体类的映射文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="cn.com.dao.Customer" table="customer" catalog="lf">
<id name="id" column="id" length="255" type="integer">
<generator class="native"></generator>
</id>
<property name="name"></property>
<property name="source"></property>
<property name="industy" column="industry"></property>
<property name="level"></property>
<property name="moblie"></property>
<property name="phone"></property>
</class>
</hibernate-mapping>
Struts部分
连接
<a href="query.action">查看人员信息并对其进行增删改查</a>
Strutx.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 这个是注明.action,和.do 结尾都可以应答 -->
<constant name="struts.action.extension" value="do,action,kk"></constant>
<package name="tjn" extends="struts-default">
<action name="gg" class="cn.com.action.Person" method="execute">
<!-- result="名称" 当名称是error的时候,代表着对应的是<result name="error"></result>这个信息;exception="异常全类名"-->
<!-- <exception-mapping result="error" exception="java.lang.ArithmeticException">
</exception-mapping>
<result name="error">/context.jsp</result> -->
<result name="success">/sort.jsp</result>
</action>
<action name="map" class="cn.com.action.RequestTest" >
<result>/one.jsp</result>
</action>
<action name="*" class="cn.com.dao.Customer" method="{1}">
<result name="{1}">/{1}.jsp</result>
</action>
</package>
</struts>
查询页面如下
<body>
<table>
<tr><td>id</td><td>name</td><td>source</td><td>industry</td><td>level</td><td>phone</td><td>moblie</td><td colspan="2"><a href="add.action">增加</a></td></tr>
<s:iterator value="#request.list" var="customer">
<tr>
<td><s:property value="id" /></td>
<td><s:property value="name" /></td>
<td><s:property value="source" /></td>
<td><s:property value="industy"/></td>
<td><s:property value="level" /></td>
<td><s:property value="phone" /></td>
<td><s:property value="moblie" /></td>
<td><a href="delete.action?id=${id}">删除</a></td>
<td><a href="update.action?id=${id}">修改</a></td>
</tr>
</s:iterator>
</table>
</body>
增加页面
<body>
<s:form action="addinfo">
<s:hidden name="id" ></s:hidden>
<s:textfield name="name" label="姓名"></s:textfield>
<s:textfield name="source" label="能量值"></s:textfield>
<s:textfield name="industy" label="种族"></s:textfield>
<s:textfield name="level" label="等级"></s:textfield>
<s:textfield name="phone" label="手机号码"></s:textfield>
<s:textfield name="moblie" label="区号"></s:textfield>
<s:submit></s:submit>
</s:form>
</body>
修改页面
<body>
<s:debug></s:debug>
<s:property value="#request.name" />
${name}
<s:form action="up">
<s:hidden value="%{id}" name="id"></s:hidden>
<s:textfield value="%{name}" label="name" name="name"></s:textfield>
<s:textfield value="%{source}" label="source" name="source"></s:textfield>
<s:textfield value="%{industy}" label="industy" name="industy">
</s:textfield>
<s:textfield value="%{level}" label="level" name="level"></s:textfield>
<s:textfield value="%{phone}" label="phone" name="phone">
</s:textfield>
<s:textfield value="%{moblie}" label="moblie" name="moblie"></s:textfield>
<s:submit></s:submit>
</s:form>
</body>
删除页面
直接删除,没有页面
不足之处:
1.实体类和Action不知道怎么分开,全部写在一起了?
2.把类放在rquest域里面,不知道怎么在<s:property>里面取出来,尝试用"#request.属性名",但是取不出来
3.不熟悉,导致这个小的例子做了大概4个小时左右