[Java教程]SSH框架下的多表增删改查
0 2017-09-27 17:00:46
下载地址:SSH框架下的多表增删改查
点击进入码云Git下载
点击进入CSDN下载
项目结构:
项目代码就不全部贴出来了,只贴下核心代码。需要项目的自己可以去下载。package com.atguigu.ssh.actions;import java.io.ByteArrayInputStream;import java.io.InputStream;import java.io.UnsupportedEncodingException;import java.util.Date;import java.util.Map;import org.apache.struts2.interceptor.RequestAware;import com.atguigu.ssh.entities.Employee;import com.atguigu.ssh.service.DepartmentService;import com.atguigu.ssh.service.EmployeeService;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;import com.opensymphony.xwork2.Preparable;public class EmployeeAction extends ActionSupport implements RequestAware, ModelDriven, Preparable { private static final long serialVersionUID = 1L; private EmployeeService employeeService; public void setEmployeeService(EmployeeService employeeService) { this.employeeService = employeeService; } private DepartmentService departmentService; public void setDepartmentService(DepartmentService departmentService) { this.departmentService = departmentService; } public String list() { request.put("employees", employeeService.getAll()); return "list"; } private Integer id; public void setId(Integer id) { this.id = id; } private InputStream inputStream; public InputStream getInputStream() { return inputStream; } public String delete() { try { employeeService.delete(id); inputStream = new ByteArrayInputStream("1".getBytes("UTF-8")); } catch (Exception e) { e.printStackTrace(); try { inputStream = new ByteArrayInputStream("0".getBytes("UTF-8")); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } } return "ajax-success"; } public String input() { request.put("departments", departmentService.getAll()); return INPUT; } public void prepareInput() { if (id != null) { model = employeeService.get(id); } } public String save() { if (id == null) { model.setCreateTime(new Date()); } employeeService.saveOrUpdate(model); return SUCCESS; } /** * 可以根据 id 来判断为 save 方法准备的 model 是 new 的还是从数据库获取的! */ public void prepareSave() { if (id == null) { model = new Employee(); } else { model = employeeService.get(id); } } private String lastName; public void setLastName(String lastName) { this.lastName = lastName; } public String validateLastName() throws UnsupportedEncodingException { if (employeeService.lastNameIsValid(lastName)) { inputStream = new ByteArrayInputStream("1".getBytes("UTF-8")); } else { inputStream = new ByteArrayInputStream("0".getBytes("UTF-8")); } return "ajax-success"; } private Map request; @Override public void setRequest(Map arg0) { this.request = arg0; } @Override public void prepare() throws Exception { } private Employee model; @Override public Employee getModel() { return model; }}package com.atguigu.ssh.dao;import java.util.List;import org.hibernate.Query;import com.atguigu.ssh.entities.Employee;public class EmployeeDao extends BaseDao { public void delete(Integer id) { String hql = "DELETE FROM Employee e WHERE e.id = ?"; getSession().createQuery(hql).setInteger(0, id).executeUpdate(); } @SuppressWarnings("unchecked") public List getAll() { String hql = "FROM Employee e LEFT OUTER JOIN FETCH e.department"; return getSession().createQuery(hql).list(); } public void saveOrUpdate(Employee employee) { getSession().saveOrUpdate(employee); } public Employee getEmployeeByLastName(String lastName) { String hql = "FROM Employee e WHERE e.lastName = ?"; Query query = getSession().createQuery(hql).setString(0, lastName); Employee employee = (Employee) query.uniqueResult(); System.out.println(employee.getDepartment().getClass().getName()); return employee; } public Employee get(Integer id) { return (Employee) getSession().get(Employee.class, id); }}package com.atguigu.ssh.service;import java.util.List;import com.atguigu.ssh.dao.EmployeeDao;import com.atguigu.ssh.entities.Employee;public class EmployeeService { private EmployeeDao employeeDao; public void setEmployeeDao(EmployeeDao employeeDao) { this.employeeDao = employeeDao; } public boolean lastNameIsValid(String lastName) { return employeeDao.getEmployeeByLastName(lastName) == null; } public void saveOrUpdate(Employee employee) { employeeDao.saveOrUpdate(employee); } public void delete(Integer id) { employeeDao.delete(id); } public List getAll() { List employees = employeeDao.getAll(); // employees.clear(); return employees; } public Employee get(Integer id) { return employeeDao.get(id); }}
本文网址:http://www.shaoqun.com/a/326864.html
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:admin@shaoqun.com。
SSH
0