Add, delete, modify, query, and delete multiple tables under the SSH framework
: Add, delete, modify, and query multiple tables under the SSH framework
Click to go To the Git download page.
Click to go to CSDN download
Project Structure:
The project code is not all pasted, but the core code. You can download the project.
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 <Employee>, Preparable {private static final long serialVersionUID = 1L; private synchronized complete; public void complete (extends EmployeeService. employeeService = employeeSer Vice;} 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 ("commit me ETS ", 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;}/*** you can determine whether the model prepared for the save method is new or retrieved from the database based on the id! */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 <String, Object> request; @ Override public void setRequest (Map <String, Object> 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<Employee> 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<Employee> getAll() { List<Employee> employees = employeeDao.getAll(); // employees.clear(); return employees; } public Employee get(Integer id) { return employeeDao.get(id); }}