zoukankan      html  css  js  c++  java
  • SSH框架下的多表增删改查

    下载地址: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<Employee>, 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<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);
        }
    
    }
  • 相关阅读:
    Oracle 数值函数
    oracle常用函数
    LeetCode Second Highest Salary 第二高薪水
    placeholder在不同浏览器下的表现及兼容方法
    鼠标放在图片上指针变成放大镜形状
    PS如何查找自己想要的字体
    网页常用字体
    JS倒计时代码
    JavaScript 导出Excel 代码
    event.keycode大全(javascript)
  • 原文地址:https://www.cnblogs.com/ceet/p/7602227.html
Copyright © 2011-2022 走看看