---
title: "Struts2+Spring+Hibernate In Action（一）"
author: "kyo100900"
date: "2007-11-01T00:26:00Z"
tags: []
categories: ["企业架构"]
slug: "struts2-spring-hibernate-in-action"
draft: false
lastmod: "2007-11-01T00:26:00Z"
id: 125465
url: "https://superleo.iteye.com/blog/125465"
---

**前言：**

关于Struts2系列的文章网上基本上都有了，但鲜有比较系统的Demo。这几天朋友要我做了一个面试题，于是我用Struts2做了这个比较完整的Demo。

本程序用到的主要技术为：

Struts2，Spring，Hibernate，DWR，JavaMail，以及少部分正则表达式。其实都是非常基础简单的应用，目的就是让大家看看这些框架是如何协同工作的。开发平台为：Winxp2+JDK1.5+Tomcat5.5+MySQL5.0。

 

以下是本例子的需求：

 

每5分钟遍历一下页面(http://post.baidu.com/f?kw=%D2%EC%C8%CB%B0%C1%CA%C0%C2%BC),

请统计页面中"《"和"》"之间有多少个"异人"这两个字,

将这数值弹出一个alert并且发信给指定的邮件地址 获取页面内所有地方的以"&lt;&lt; &gt;&gt;"为标示的这个符号代表书名号,获取之中的内容 &lt;&lt;使用正则表达式&gt;&gt; 功能点:

1)URL抓取页面。

2)正则表达式匹配。

3)邮件系统调用 请注意设计思路和代码规范。

 

 

**第一部分：**

1.       设计实体

本程序共有四个实体：

User：用户基本信息，可用来登录本系统。

Article：存储每次通过正则从URL抓取的目标数据总数。

Log：邮件发送时的日志类。

Mail：邮件发送列表。

 

两个抽象实体，目的是为了承继。

PrimaryEntiry：承继主键（id）

CommonEntity：承继候选键（name）

实体之间的关系：

只有Log与Mai之间是多对一的关系。在看下面的Pojo类的时候需要注意一下，我这里用的是JPA，关系都写在Annotation里了。

 

 

以下是这几个实体详细信息：

 

**PrimaryEntity.java**

java 代码

01. /\**
02. * @author SuperLeo
03. * @date 2007-6-15
04. * @version 1.0
05. * �实体类的父类，抽象出ID部分
06. \*/
07. package com.leo.po;
08. import javax.persistence.GeneratedValue;
09. import javax.persistence.GenerationType;
10. import javax.persistence.Id;
11. import javax.persistence.MappedSuperclass;
12. /\**
13. * @author SuperLeo
14. \*/
15. @MappedSuperclass
16. // 用来声明这个是父类，子类可以继承属
17. public abstract class PrimaryEntity {
18. @Id
19. @GeneratedValue(strategy = GenerationType.AUTO)
20. private long id;
21. /\**
22. * @return 返回id
23. \*/
24. public long getId() {
25. return id;
26. }
27. /\**
28. * @param id
29. *            设置id
30. \*/
31. public void setId(long id) {
32. this.id = id;
33. }
34. }

 **CommonEntity.java**

java 代码

01. /\**
02. * @author SuperLeo
03. * @date 2006-6-15
04. * @version 1.0
05. * 名称抽象实体类
06. \*/
07. package com.leo.po;
08. import javax.persistence.Column;
09. import javax.persistence.MappedSuperclass;
10. /\**
11. * @author SuperLeo
12. \*
13. \*/
14. @MappedSuperclass
15. // 用来声明这个是父类，子类可以继承属性
16. public abstract class CommonEntity extends PrimaryEntity {
17. @Column(name = "name")
18. private String name;
19. /\**
20. * @return 返回名称
21. \*/
22. public String getName() {
23. return name;
24. }
25. /\**
26. * @param name
27. *            设置名称
28. \*/
29. public void setName(String name) {
30. this.name = name;
31. }
32. }

 

**Article.java**

java 代码

01. /\**
02. * 存储每次抓取的数量
03. \*/
04. package com.leo.po;
05. import java.util.Date;
06. import javax.persistence.Column;
07. import javax.persistence.Entity;
08. import javax.persistence.Table;
09. /\**
10. * @author superleo
11. \*
12. \*/
13. @Entity
14. @Table(name = "tbl\_article")
15. public class Article extends PrimaryEntity {
16. /\**
17. * 文章点击量
18. \*/
19. @Column(name = "count\_size")
20. private int count;
21. /\**
22. * 文章建立时间
23. \*/
24. @Column(name = "create\_date")
25. private Date createDate;
26. public int getCount() {
27. return count;
28. }
29. public void setCount(int count) {
30. this.count = count;
31. }
32. public Date getCreateDate() {
33. return createDate;
34. }
35. public void setCreateDate(Date createDate) {
36. this.createDate = createDate;
37. }
38. }

**Log.java**

java 代码

01. /\**
02. * 邮件列表
03. \*/
04. package com.leo.po;
05. import java.util.Date;
06. import javax.persistence.Column;
07. import javax.persistence.Entity;
08. import javax.persistence.JoinColumn;
09. import javax.persistence.ManyToOne;
10. import javax.persistence.Table;
11. /\**
12. * @author superleo
13. \*
14. \*/
15. @Entity
16. @Table(name = "tbl\_log")
17. public class Log extends PrimaryEntity {
18. @Column(name = "log")
19. private String log;
20. @Column(name = "create\_date")
21. private Date createDate;
22. @ManyToOne
23. @JoinColumn(name = "fk\_mail\_id")
24. private Mail mail;
25. public Mail getMail() {
26. return mail;
27. }
28. public void setMail(Mail mail) {
29. this.mail = mail;
30. }
31. public String getLog() {
32. return log;
33. }
34. public void setLog(String log) {
35. this.log = log;
36. }
37. public Date getCreateDate() {
38. return createDate;
39. }
40. public void setCreateDate(Date createDate) {
41. this.createDate = createDate;
42. }
43. }

 

 **Mail.java**

java 代码

01. /\**
02. * 邮件列表
03. \*/
04. package com.leo.po;
05. import java.util.List;
06. import javax.persistence.CascadeType;
07. import javax.persistence.Column;
08. import javax.persistence.Entity;
09. import javax.persistence.OneToMany;
10. import javax.persistence.Table;
11. /\**
12. * @author superleo
13. \*
14. \*/
15. @Entity
16. @Table(name = "tbl\_mail")
17. public class Mail extends CommonEntity {
18. @Column(name = "email")
19. private String email;
20. @OneToMany(mappedBy = "mail", cascade = { CascadeType.ALL })
21. private List&lt;log&gt;&lt;/log&gt; log;
22. public String getEmail() {
23. return email;
24. }
25. public void setEmail(String email) {
26. this.email = email;
27. }
28. public List&lt;log&gt;&lt;/log&gt; getLog() {
29. return log;
30. }
31. public void setLog(List&lt;log&gt;&lt;/log&gt; log) {
32. this.log = log;
33. }
34. }

 

 **User.java**

java 代码

01. /\**
02. * @author SuperLeo
03. * @date 2006-6-15
04. * @version 1.0
05. * 用户实体类
06. \*/
07. package com.leo.po;
08. import javax.persistence.Column;
09. import javax.persistence.Entity;
10. import javax.persistence.Table;
11. /\**
12. * @author SuperLeo
13. \*
14. \*/
15. @Entity
16. @Table(name = "tbl\_user")
17. // 映射的表叫 person
18. public class User extends CommonEntity {
19. @Column(name = "password")
20. private String password;
21. @Column(name = "email")
22. private String email;
23. /\**
24. * @return 返回电子邮件
25. \*/
26. public String getEmail() {
27. return email;
28. }
29. /\**
30. * @param email
31. *            设置电子邮件
32. \*/
33. public void setEmail(String email) {
34. this.email = email;
35. }
36. /\**
37. * @return 返回用户密码
38. \*/
39. public String getPassword() {
40. return password;
41. }
42. /\**
43. * @param passowd
44. *            设置用户密码
45. \*/
46. public void setPassword(String password) {
47. this.password = password;
48. }
49. }

 

 

2.     DAO与Service实现

正如大家所想，在Java世界里，DAO与Servcie的实现是大同小异的。这里我的例子足够小了，而且其实有了Hibernate就已经具备了DAO的能力，因些这里只有Service层的设计。

先定义一个父接口，把尽可能相同的事都在父类里面搞定：

 

**IGenericService.java**

java 代码

01. **/\*\***
02. *** @author SuperLeo**
03. *** @date 2006-6-15**
04. *** @version 1.0**
05. *** Service层最顶层接口**
06. **\*/**
07. package **com.leo.service;**
08. import **java.io.Serializable;**
09. import **java.util.List;**
10. import **org.hibernate.Session;**
11. import **com.leo.util.PageInfo;**
12. **/\*\***
13. *** @author superleo**
14. **\***
15. **\*/**
16. public **interface IGenericServiceextends Serializable&gt; {**
17. **/\*\***
18. *** 保存实体**
19. **\***
20. *** @param t**
21. **\*/**
22. **public PK store(T t);**
23. **/\*\***
24. *** 加载单个实体**
25. **\***
26. *** @param className**
27. *** @param id**
28. *** @return**
29. **\*/**
30. **public T load(PK id);**
31. **/\*\***
32. *** 更新实体**
33. **\***
34. *** @param t**
35. **\*/**
36. **public void update(T t);**
37. **/\*\***
38. *** 删除实体**
39. **\***
40. *** @param t**
41. **\*/**
42. **public void delete(T t);**
43. **/\*\***
44. *** 分页计算总数**
45. **\***
46. *** @param session**
47. *** @param hql**
48. *** @return**
49. *** @throws Exception**
50. **\*/**
51. **public int getTotalCount(Session session, String hql) throws Exception;**
52. **/\*\***
53. *** 根据名称精确查找**
54. **\***
55. *** @param className**
56. *** @param name**
57. *** @return**
58. **\*/**
59. **public T findByNameExact(String name);**
60. **/\*\***
61. *** 根据名称模糊查找**
62. **\***
63. *** @param className**
64. *** @param name**
65. *** @return**
66. **\*/**
67. **List&lt;t&gt;&lt;/t&gt; findByName(String name, PageInfo pageInfo) throws Exception;**
68. **/\*\***
69. *** 查找所有记录**
70. **\***
71. *** @param hql**
72. *** @param pageInfo**
73. *** @return**
74. *** @throws Exception**
75. **\*/**
76. **List&lt;t&gt;&lt;/t&gt; findByAll(final String hql, PageInfo pageInfo) throws Exception;**
77. **/\*\***
78. *** 查找所有记录**
79. **\***
80. *** @param hql**
81. *** @param pageInfo**
82. *** @return**
83. *** @throws Exception**
84. **\*/**
85. **List&lt;t&gt;&lt;/t&gt; findByAll() throws Exception;**
86. **/\*\***
87. *** 删除全部**
88. **\***
89. *** @param hql**
90. *** @return**
91. *** @throws Exception**
92. **\*/**
93. **public List&lt;t&gt;&lt;/t&gt; deleteByAll(final String hql) throws Exception;**
94. **}**

以及它的实现