Struts2+Spring+Hibernate In Action(一)
前言:
关于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并且发信给指定的邮件地址 获取页面内所有地方的以"<< >>"为标示的这个符号代表书名号,获取之中的内容 <<使用正则表达式>> 功能点:
1)URL抓取页面。
2)正则表达式匹配。
3)邮件系统调用 请注意设计思路和代码规范。
第一部分:
1. 设计实体
本程序共有四个实体:
User:用户基本信息,可用来登录本系统。
Article:存储每次通过正则从URL抓取的目标数据总数。
Log:邮件发送时的日志类。
Mail:邮件发送列表。
两个抽象实体,目的是为了承继。
PrimaryEntiry:承继主键(id)
CommonEntity:承继候选键(name)
实体之间的关系:
只有Log与Mai之间是多对一的关系。在看下面的Pojo类的时候需要注意一下,我这里用的是JPA,关系都写在Annotation里了。
以下是这几个实体详细信息:
PrimaryEntity.java
java 代码
- /**
- * @author SuperLeo
- * @date 2007-6-15
- * @version 1.0
- * �实体类的父类,抽象出ID部分
- */
- package com.leo.po;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.MappedSuperclass;
- /**
- * @author SuperLeo
- */
- @MappedSuperclass
- // 用来声明这个是父类,子类可以继承属
- public abstract class PrimaryEntity {
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- private long id;
- /**
- * @return 返回id
- */
- public long getId() {
- return id;
- }
- /**
- * @param id
- * 设置id
- */
- public void setId(long id) {
- this.id = id;
- }
- }
CommonEntity.java
java 代码
- /**
- * @author SuperLeo
- * @date 2006-6-15
- * @version 1.0
- * 名称抽象实体类
- */
- package com.leo.po;
- import javax.persistence.Column;
- import javax.persistence.MappedSuperclass;
- /**
- * @author SuperLeo
- *
- */
- @MappedSuperclass
- // 用来声明这个是父类,子类可以继承属性
- public abstract class CommonEntity extends PrimaryEntity {
- @Column(name = "name")
- private String name;
- /**
- * @return 返回名称
- */
- public String getName() {
- return name;
- }
- /**
- * @param name
- * 设置名称
- */
- public void setName(String name) {
- this.name = name;
- }
- }
Article.java
java 代码
- /**
- * 存储每次抓取的数量
- */
- package com.leo.po;
- import java.util.Date;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.Table;
- /**
- * @author superleo
- *
- */
- @Entity
- @Table(name = "tbl_article")
- public class Article extends PrimaryEntity {
- /**
- * 文章点击量
- */
- @Column(name = "count_size")
- private int count;
- /**
- * 文章建立时间
- */
- @Column(name = "create_date")
- private Date createDate;
- public int getCount() {
- return count;
- }
- public void setCount(int count) {
- this.count = count;
- }
- public Date getCreateDate() {
- return createDate;
- }
- public void setCreateDate(Date createDate) {
- this.createDate = createDate;
- }
- }
Log.java
java 代码
- /**
- * 邮件列表
- */
- package com.leo.po;
- import java.util.Date;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.JoinColumn;
- import javax.persistence.ManyToOne;
- import javax.persistence.Table;
- /**
- * @author superleo
- *
- */
- @Entity
- @Table(name = "tbl_log")
- public class Log extends PrimaryEntity {
- @Column(name = "log")
- private String log;
- @Column(name = "create_date")
- private Date createDate;
- @ManyToOne
- @JoinColumn(name = "fk_mail_id")
- private Mail mail;
- public Mail getMail() {
- return mail;
- }
- public void setMail(Mail mail) {
- this.mail = mail;
- }
- public String getLog() {
- return log;
- }
- public void setLog(String log) {
- this.log = log;
- }
- public Date getCreateDate() {
- return createDate;
- }
- public void setCreateDate(Date createDate) {
- this.createDate = createDate;
- }
- }
Mail.java
java 代码
- /**
- * 邮件列表
- */
- package com.leo.po;
- import java.util.List;
- import javax.persistence.CascadeType;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.OneToMany;
- import javax.persistence.Table;
- /**
- * @author superleo
- *
- */
- @Entity
- @Table(name = "tbl_mail")
- public class Mail extends CommonEntity {
- @Column(name = "email")
- private String email;
- @OneToMany(mappedBy = "mail", cascade = { CascadeType.ALL })
- private List<log></log> log;
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public List<log></log> getLog() {
- return log;
- }
- public void setLog(List<log></log> log) {
- this.log = log;
- }
- }
User.java
java 代码
- /**
- * @author SuperLeo
- * @date 2006-6-15
- * @version 1.0
- * 用户实体类
- */
- package com.leo.po;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.Table;
- /**
- * @author SuperLeo
- *
- */
- @Entity
- @Table(name = "tbl_user")
- // 映射的表叫 person
- public class User extends CommonEntity {
- @Column(name = "password")
- private String password;
- @Column(name = "email")
- private String email;
- /**
- * @return 返回电子邮件
- */
- public String getEmail() {
- return email;
- }
- /**
- * @param email
- * 设置电子邮件
- */
- public void setEmail(String email) {
- this.email = email;
- }
- /**
- * @return 返回用户密码
- */
- public String getPassword() {
- return password;
- }
- /**
- * @param passowd
- * 设置用户密码
- */
- public void setPassword(String password) {
- this.password = password;
- }
- }
2. DAO与Service实现
正如大家所想,在Java世界里,DAO与Servcie的实现是大同小异的。这里我的例子足够小了,而且其实有了Hibernate就已经具备了DAO的能力,因些这里只有Service层的设计。
先定义一个父接口,把尽可能相同的事都在父类里面搞定:
IGenericService.java
java 代码
- /**
- *** @author SuperLeo**
- *** @date 2006-6-15**
- *** @version 1.0**
- *** Service层最顶层接口**
- */
- package com.leo.service;
- import java.io.Serializable;
- import java.util.List;
- import org.hibernate.Session;
- import com.leo.util.PageInfo;
- /**
- *** @author superleo**
- *
- */
- public interface IGenericServiceextends Serializable> {
- /**
- *** 保存实体**
- *
- *** @param t**
- */
- public PK store(T t);
- /**
- *** 加载单个实体**
- *
- *** @param className**
- *** @param id**
- *** @return**
- */
- public T load(PK id);
- /**
- *** 更新实体**
- *
- *** @param t**
- */
- public void update(T t);
- /**
- *** 删除实体**
- *
- *** @param t**
- */
- public void delete(T t);
- /**
- *** 分页计算总数**
- *
- *** @param session**
- *** @param hql**
- *** @return**
- *** @throws Exception**
- */
- public int getTotalCount(Session session, String hql) throws Exception;
- /**
- *** 根据名称精确查找**
- *
- *** @param className**
- *** @param name**
- *** @return**
- */
- public T findByNameExact(String name);
- /**
- *** 根据名称模糊查找**
- *
- *** @param className**
- *** @param name**
- *** @return**
- */
- List<t></t> findByName(String name, PageInfo pageInfo) throws Exception;
- /**
- *** 查找所有记录**
- *
- *** @param hql**
- *** @param pageInfo**
- *** @return**
- *** @throws Exception**
- */
- List<t></t> findByAll(final String hql, PageInfo pageInfo) throws Exception;
- /**
- *** 查找所有记录**
- *
- *** @param hql**
- *** @param pageInfo**
- *** @return**
- *** @throws Exception**
- */
- List<t></t> findByAll() throws Exception;
- /**
- *** 删除全部**
- *
- *** @param hql**
- *** @return**
- *** @throws Exception**
- */
- public List<t></t> deleteByAll(final String hql) throws Exception;
- }
以及它的实现
学习~~~~~~~~~~~~~~~~~
备注一下,以后慢慢看
照片是本人吗~!好漂亮啊~!
谢谢 学习中
灰熊不错的说,继续跟踪学习
正则,说实话,在几年前用PHP的时候曾经使用非常频繁,而且写正则感觉很有成就感,呵呵,实际上对使用的人来说就一些标记而已,如果只是记那些东西,对一个人的技术能力没有太大关系。
非常感谢。向你学习
org.springframework.mail.MailSendException; nested exception details (1) are: Failed message 1: com.sun.mail.smtp.SMTPSendFailedException: 553 You are not authorized to send mail, authentication is required
帮忙看一下这是什么问题?谢谢!
如果下载了原代码需要修改两个地方: 1.src下的leo.conf文件,里面改你数据用户,密码,以及你的邮件服务器,还有账号和密码 2.数据库表tbl_mail里的邮件改成你自己的邮件,这样项目运行后,你才知道你到底有没有收到信。
其它的Service只需要继承它了,就可以很方便的使用了,要编码的地方不是特别的多了。Javaeye Blog总出问题,只能这样发给大家看了,详细的代码会提交上来的。下面在第二篇来讲讲Spring的配置。
GenericServiceImpl.java
/**
import java.io.Serializable; import java.util.List;
import org.hibernate.Query; import org.hibernate.Session; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.transaction.annotation.Transactional;
import com.leo.service.IGenericService; import com.leo.util.PageInfo;
/**
*/ public class GenericServiceImpl<T, PK extends Serializable> implements IGenericService<T, PK> {
}