S2SH CRUD 整合
发布日期:2022-03-29 14:04:54 浏览次数:50 分类:博客文章

本文共 16108 字,大约阅读时间需要 53 分钟。

采用的框架 Struts2+Spring4+Hbiernate4.

 

目录结构

:   

EmployeeAction:

1 package com.xx.ssh.actions;  2   3 import java.io.ByteArrayInputStream;  4 import java.io.InputStream;  5 import java.io.UnsupportedEncodingException;  6 import java.util.Date;  7 import java.util.Map;  8 import org.apache.struts2.interceptor.RequestAware;  9 import com.opensymphony.xwork2.ActionSupport; 10 import com.opensymphony.xwork2.ModelDriven; 11 import com.opensymphony.xwork2.Preparable; 12 import com.xx.ssh.entities.Employee; 13 import com.xx.ssh.service.DepartmentService; 14 import com.xx.ssh.service.EmployeeService; 15  16 public class EmployeeAction extends ActionSupport implements RequestAware, 17         ModelDriven
, Preparable { 18 19 private static final long serialVersionUID = 1L; 20 21 private EmployeeService employssService; 22 23 public void setEmployssService(EmployeeService employssService) { 24 this.employssService = employssService; 25 } 26 27 private DepartmentService departmentService; 28 29 public void setDepartmentService(DepartmentService departmentService) { 30 this.departmentService = departmentService; 31 } 32 33 public String list() { 34 request.put("employees", employssService.getAll()); 35 System.out.println("request: " + request.size()); 36 return "list"; 37 } 38 39 private Integer id; 40 41 public void setId(Integer id) { 42 this.id = id; 43 } 44 45 private InputStream inputStream; 46 47 public InputStream getInputStream() { 48 return inputStream; 49 } 50 //回调函数。判断是否删除 51 public String delete() { 52 try { 53 employssService.delete(id); 54 inputStream = new ByteArrayInputStream("1".getBytes("UTF-8")); 55 } catch (Exception e) { 56 e.printStackTrace(); 57 try { 58 inputStream = new ByteArrayInputStream("0".getBytes("UTF-8")); 59 } catch (UnsupportedEncodingException e1) { 60 e1.printStackTrace(); 61 } 62 } 63 return "ajax-success"; 64 } 65 66 private String lastName; 67 68 public void setLastName(String lastName) { 69 this.lastName = lastName; 70 } 71 //回调函数。判断用户名是否存在。 72 public String validateLastName() { 73 try { 74 if (employssService.lastNameIsValid(lastName)) { 75 76 inputStream = new ByteArrayInputStream("1".getBytes("utf-8")); 77 } else { 78 79 inputStream = new ByteArrayInputStream("0".getBytes("utf-8")); 80 } 81 } catch (Exception e) { 82 83 } 84 return "ajax-success"; 85 } 86 87 private Employee model; 88 89 /* 90 * 可以根椐ID来判断为save方法准备的model是new的还是数据库获取的。 91 */ 92 public void prepareSave() { 93 if (id == null) { 94 model = new Employee(); 95 } else { 96 model = employssService.get(id); 97 } 98 } 99 100 public String save() {101 102 if (id == null) {103 model.setCreateTime(new Date());104 105 }106 employssService.saveOrUpdate(model);107 return SUCCESS;108 }109 110 public String input() {111 request.put("departments", departmentService.getAll());112 return INPUT;113 }114 115 public void prepareInput() {116 if (id != null) {117 model = employssService.get(id);118 }119 120 }121 122 private Map
request;123 124 @Override125 public void setRequest(Map
arg0) {126 this.request = arg0;127 128 }129 130 @Override131 public Employee getModel() {132 133 return model;134 }135 136 @Override137 public void prepare() throws Exception {138 139 }140 141 }
View Code

SSHDateConverter:自定义转换器

1 package com.xx.ssh.converters; 2  3 import java.text.DateFormat; 4 import java.text.ParseException; 5 import java.text.SimpleDateFormat; 6 import java.util.Date; 7 import java.util.Map; 8  9 import org.apache.struts2.util.StrutsTypeConverter;10 11 public class SSHDateConverter extends StrutsTypeConverter {12 13     private DateFormat dateFormat;14     {15         dateFormat = new SimpleDateFormat("yyyy-MM-dd");16     }17     18     @Override19     public Object convertFromString(Map context, String[] values, Class toClass) {20         if(toClass == Date.class){21             try {22                 return dateFormat.parse(values[0]);23             } catch (ParseException e) {24                 e.printStackTrace();25             }26         }27         28         return null;29     }30 31     @Override32     public String convertToString(Map context, Object o) {33         if(o instanceof Date){34             return dateFormat.format((Date)o);35         }36         return null;37     }38 39 }
View Code

BaseDao:SessionFactory

1 package com.xx.ssh.dao; 2  3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5  6 public class BaseDao { 7     private SessionFactory  sessionFactory; 8  9     public void setSessionFactory(SessionFactory sessionFactory) {10         this.sessionFactory = sessionFactory;11     }12     13     public Session getSession() {14         return  this.sessionFactory.getCurrentSession();15     }16 }
View Code

DepartmentDao:Dao层

1 package com.xx.ssh.dao; 2  3 import java.util.List; 4  5 import com.xx.ssh.entities.Department; 6  7 public class DepartmentDao extends BaseDao{ 8     public List
getAll(){ 9 String hql="FROM Department";10 return getSession().createQuery(hql).list();11 }12 }
View Code

EmployeeDao

1 package com.xx.ssh.dao; 2  3 import java.util.List; 4  5 import org.hibernate.Query; 6 import org.hibernate.Session; 7 import org.hibernate.SessionFactory; 8  9 import com.xx.ssh.entities.Employee;10 11 public class EmployeeDao extends BaseDao {12     13     14     public void delete(Integer id){15         String hql="delete from Employee e where e.id=? ";16         getSession().createQuery(hql).setInteger(0,id).executeUpdate();17     }18 19     public List
getAll(){20 21 String hql="from Employee e LEFT OUTER JOIN FETCH e.department";22 return getSession().createQuery(hql).list();23 }24 public void saveOrUpdate(Employee employee){25 getSession().saveOrUpdate(employee);26 }27 public Employee getEmployeeByLastName(String lastName){28 29 String hql="from Employee e where e.lastName=? ";30 Query query = getSession().createQuery(hql).setString(0,lastName);31 return (Employee)query.uniqueResult();32 }33 public Employee get(Integer id){34 return (Employee) getSession().get(Employee.class,id);35 36 }37 }
View Code

实体:Department

1 package com.xx.ssh.entities; 2  3 public class Department { 4     private Integer id; 5     private String departmentName; 6  7     public Integer getId() { 8         return id; 9     }10 11     public void setId(Integer id) {12         this.id = id;13     }14 15     public String getDepartmentName() {16         return departmentName;17     }18 19     public void setDepartmentName(String departmentName) {20         this.departmentName = departmentName;21     }22 }
View Code

实体:Employee

1 package com.xx.ssh.entities; 2  3 import java.util.Date; 4  5 public class Employee { 6      7      8     // 9     private Integer id;10     //不能被修改11     private String lastName;12     private String email;13     //从前端传入的是string类型,所以需要注意转换。14     private Date birth;15     //不能被修改16     private Date createTime;17     //单向n-1的关联关系18     private Department department;19     20     public Integer getId() {21         return id;22     }23     public void setId(Integer id) {24         this.id = id;25     }26     public String getLastName() {27         return lastName;28     }29     public void setLastName(String lastName) {30         this.lastName = lastName;31     }32     public String getEmail() {33         return email;34     }35     public void setEmail(String email) {36         this.email = email;37     }38     public Date getBirth() {39         return birth;40     }41     public void setBirth(Date birth) {42         this.birth = birth;43     }44     public Date getCreateTime() {45         return createTime;46     }47     public void setCreateTime(Date createTime) {48         this.createTime = createTime;49     }50     public Department getDepartment() {51         return department;52     }53     public void setDepartment(Department department) {54         this.department = department;55     }56     57     @Override58     public String toString() {59         return "Employee [birth=" + birth + ", createTime=" + createTime60                 + ", department.id=" + department + ", email=" + email + ", id="61                 + id + ", lastName=" + lastName + "]";62     }63 }
View Code

表与类映射文件配置。

Department.hbm.xml

1 
2 4
5
6
7
8
9
10
11 12
13
14
15 16
17
View Code

Employee.hbm.xml

1 
2 4
5
6
7 8
9
10
11
12 13
14
15
16 17
18
19
20 21
22
23
24 25
26
27
28 29
30
31
32
33 34
35
View Code

Service层:Department

1 package com.xx.ssh.service; 2  3 import java.util.List; 4  5 import com.xx.ssh.dao.DepartmentDao; 6 import com.xx.ssh.entities.Department; 7  8 public class DepartmentService { 9     private DepartmentDao departmentDao;10     11     public void setDepartmentDao(DepartmentDao departmentDao){12         this.departmentDao=departmentDao;13     }14     public List
getAll(){15 return departmentDao.getAll();16 }17 18 }
View Code

Service层:Employee

1 package com.xx.ssh.service; 2  3 import java.util.List; 4  5 import com.xx.ssh.dao.EmployeeDao; 6 import com.xx.ssh.entities.Employee; 7  8 public class EmployeeService { 9     private EmployeeDao employeeDao;10     11     public void setEmployeeDao(EmployeeDao employeeDao)12     {13         this.employeeDao=employeeDao;14     }15     public boolean lastNameIsValid(String lastName){16         return employeeDao.getEmployeeByLastName(lastName)==null;17     }18     public void delete(Integer id){19         employeeDao.delete(id);20     }21     public void saveOrUpdate(Employee employee){22         employeeDao.saveOrUpdate(employee);23     }24     public List
getAll(){25 List
employees=employeeDao.getAll();26 /*employees.clear();*/27 System.out.println(employees.size());28 return employees;29 }30 public Employee get(Integer id) {31 // TODO Auto-generated method stub32 return employeeDao.get(id);33 34 }35 36 }
View Code

配置文件:

applicationContext-beans.xml

1 
2
6 7
8
9
10 11
12
13
14 15
16
17
18 19
20
21
22 23
25
26
27
28
View Code

applicationContext.xml

1 
2
15 16
17
18 19
20
21
22
23
24
25
26 27
28
29
30
31
32
33 34
35
36
37
38
39 40
41
42
43
44
45
46
47
48 49
50
51
52
53
54
View Code

db.properties

1 jdbc.user=root2 jdbc.password=root3 jdbc.driverClass=com.mysql.jdbc.Driver4 jdbc.jdbcUrl=jdbc:mysql:///spring65 6 jdbc.initPoolSize=57 jdbc.maxPoolSize=10
View Code

hibernate.cfg.xml

1 
2 5
6
7
8 9
10
org.hibernate.dialect.MySQL5InnoDBDialect
11 12
13
true
14
true
15 16
17
update
18 19
20
21
View Code

struts.xml

1 
2 5 6
7 8
9
10 11
12 13
14
15
16
17
false18
19
20
21 22
23
24 25
27
/WEB-INF/views/emp-list.jsp
28
29
text/html30
inputStream31
32
/WEB-INF/views/emp-input.jsp
33
/emp-list
34
35 36
37 38
View Code

xwork-conversion.properties :时间转换器配置文件。

java.util.Date=com.xx.ssh.converters.SSHDateConverter

web.xml

1 
2
6 7 8
9
contextConfigLocation
10
classpath:applicationContext*.xml
11
12
13
org.springframework.web.context.ContextLoaderListener
14
15
16
17
struts2
18
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
19
20
21
struts2
22
/*
23
24
View Code

JSP:

emp-input.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %>
Insert title here

Employee Input Page

<%--
--%>

emp-list.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
Insert title here

Employee List Page

没有任何员工信息;
ID LASTNAME EMAIL BIRTH CREATETIME DEPT DELETE Edit
${id } ${lastName } ${email }
${department.departmentName } Delete Edit

 

转载地址:https://www.cnblogs.com/1-Admin/p/6143299.html 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:spring mvc <mvc:default-servlet-handler /> 。
下一篇:Chrome 浏览器提示adobe flash player不是最新版本

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月03日 05时12分31秒