S2SH修改整合修改操作
其实我并不认为这就是级联修改,它只涉及到一个表的改动并未涉及到其他的表,我认为级联操作是涉及到多个表的操作。在这里我勉强地称之为级联修改。
Domain中的方法:
public class Orders implements java.io.Serializable {
/**
* @author DXL_xiaoli
*/
private static final long serialVersionUID = 1L;
//递增序列
private Integer id;
//支付方式
private Payments payments;
//员工
private Employees employees;
//客户
private Customers customers;
//订单编号
private String number;
//订单日期
private Date rdate;
//订单金额
private Double sum;
//送货方式
private String sendmode;
//订单状态
private String status;
//生成set(),get(),public Orders(),public Orders(….)方法
}
当我们点击修改时将其所对应的实体的 id 传到 action 中并通过配置文件找到其所对应的 update() 方法。由于 Orders 表与客户表 Customers 、员工表 Employees 、支付方式表 Payments 都有所关联,在修改定单表需要将这三个有关联表中的字段查询出来,所以定义了查询这三个表的方法 -----emplist(),customerslist(),payModelist() 。
public class OrdersAction extends ActionSupport {
/**
* @author DXL_xiaoli
*/
private static final long serialVersionUID = 1L;
private OrdersServiceImpl ordersServiceImpl;
private Orders entity;
private List<Orders> entities;
private Pagination pagination;
private List<Employees> emplist;
private List<Payments> payModelist;
private List<Customers> customerslist;
//构造器注入
public void setOrdersServiceImpl(OrdersServiceImpl ordersServiceImpl) {
this.ordersServiceImpl = ordersServiceImpl;
}
public String findById() {
entity = ordersServiceImpl.findById(entity.getId());
return "findById";
}
@SuppressWarnings("unchecked")
public String update() {
emplist = ordersServiceImpl.emplist();
customerslist = ordersServiceImpl.customerslist();
payModelist = ordersServiceImpl.payModelist();
entity = ordersServiceImpl.findById(entity.getId());
return "update";
}
public String updateOk() {
ordersServiceImpl.update(entity);
return "updateOk";
}
}
在服务层仍执行update()方法
public class OrdersServiceImpl implements OrdersService{
private OrdersDaoImpl ordersDaoImpl;
public void setOrdersDaoImpl(OrdersDaoImpl ordersDaoImpl) {
this.ordersDaoImpl = ordersDaoImpl;
}
public Orders findById(Integer id) {
return ordersDaoImpl.findById(id);
}
public void update(Orders entity) {
ordersDaoImpl.update(entity);
}
public List customerslist() {
return ordersDaoImpl.customerslist();
}
public List emplist() {
return ordersDaoImpl.emplist();
}
public List payModelist() {
return ordersDaoImpl.payModelist();
}
}
在最底层我们将实现其修改。根据页面传来的 id 将客户表、员工表、支付方式表中的对象获取到,然后将其对象 set 到 Orders 对象中,最后在执行 Orders 表中的 update 方法。
public class OrdersDaoImpl extends HibernateDaoSupport implements OrdersDao {
public Orders findById(Integer id) {
return (Orders) this.getHibernateTemplate().get(Orders.class, id);
}
public void update(Orders entity) {
Customers customers = (Customers) this.getHibernateTemplate().get(
Customers.class, entity.getCustomers().getId());
Employees employees = (Employees) this.getHibernateTemplate().get(
Employees.class, entity.getEmployees().getId());
Payments payments = (Payments) this.getHibernateTemplate().get(
Payments.class, entity.getPayments().getId());
entity.setCustomers(customers);
entity.setEmployees(employees);
entity.setPayments(payments);
this.getHibernateTemplate().update(entity);
}
public List customerslist() {
return this.getHibernateTemplate().find(" from Customers");
}
public List emplist() {
return this.getHibernateTemplate().find(" from Employees");
}
public List payModelist() {
return this.getHibernateTemplate().find(" from Payments");
}
}
以上是整个修改的全过程 ( 添加操作与修改操作类似,在此不做详解 ) ,若有写的不清楚的地方请留言 ……..