all gone

all gone

SSH(Spring + Struts + Hibernate)小试 之一

之前对Spring Struts Hibernate都稍有了解,Spring的IOC给我留下了很深的印象,容器管理的对象生成
方法极大地简化了程序逻辑,Spring也提供了用于Web的Spring MVC 框架,由于我感觉其易用性还是不及
Struts,所以我还是选择Struts作为前端WEB框架,Hibernate我一直没有在Web应用中试过,这次当然不会
错过机会了。


开发环境的塔建我就不多说了,具体可以参考我以前的几篇日志。

在Eclipse中新建一个动态Web工程,将Struts 、Spring、Hibernate需要的jar都添加到WEB-INF/lib目录下,
将Struts标签所需的tld文件、struts-config.xml文件(可以由StrutsIDE生成)添加到WEB-INF下,另外在
此目录下添加一个Spring的配置文件applicationContext.xml。


我参考的例子是夏昕的《深入浅出Hibernate》上的第6章,很好的一个论坛示例。

首先按照书上的ER模型,在数据库(MySQL)中建立相应的数据库。


create database forum;

use forum;

create table user(
 id int not null auto_increment primary key,
 name varchar(50),
 pwd varchar(50),
 email varchar(50)

)type=innodb;


create table board(
 id int not null auto_increment primary key, 
 parent_id int,
 create_by int not null,
 name varchar(50) not null,
 remark varchar(255),
 create_time datetime,

 index(parent_id),
 index(create_by),

 foreign key(parent_id) references board(id) on delete cascade,
 foreign key(create_by) references user(id) on delete no action
 
)type=innodb;


create table article(
 id int not null auto_increment primary key, 
 parent_id int ,
 board_id int not null,
 article_type int not null,
 title varchar(255),
 body text,
 create_by int not null,

 create_time datetime,
 hits int unsigned,
 bytes int unsigned,
 last_update_by int not null,
 last_update_time datetime,

 index(parent_id),
 index(board_id),
 index(create_by),
 index(last_update_by),

 foreign key(parent_id) references article(id) on delete no action,
 foreign key(board_id) references board(id) on delete cascade,
 foreign key(create_by) references user(id) on delete no action,
 foreign key(last_update_by) references user(id) on delete no action
 
)type=innodb;

之后,我选择了用Hibernate Sychonizer来生成映射文件和实体类,然后对生成的类进行修改(删掉了生成的Base类),
实际上Hibernate Sychonizer生成的映射文件也有错误,需要修改,我也是后来在使用中发现的。

//User.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd " >

<hibernate-mapping package="com.lzy.forum.domain">
 <class
  name="User"
  table="user"
 >

  <id
   name="Id"
   type="integer"
   column="id"
  >
   <generator class="native"/>
  </id>

  <property
   name="Name"
   column="name"
   type="string"
   not-null="false"
   length="50"
  />
  <property
   name="Pwd"
   column="pwd"
   type="string"
   not-null="false"
   length="50"
  />
  <property
   name="Email"
   column="email"
   type="string"
   not-null="false"
   length="50"
  />
 

  <set name="ArticlesByLastUpdateBy" inverse="true" lazy="true">
   <key column="last_update_by"/>
   <one-to-many class="Article"/>
  </set>

  <set name="ArticlesByCreateBy" inverse="true" lazy="true">
   <key column="create_by"/>这里的create_by是后来修改的,Hibernate Sychonizer生成的有错,后面有几处同样的错误】
   <one-to-many class="Article"/>
  </set>

  <set name="Boards" inverse="true" lazy="true">
   <key column="create_by"/>【这里的create_by是后来修改的】

   <one-to-many class="Board"/>
  </set>


 </class> 
</hibernate-mapping>

//User.java
package com.lzy.forum.domain;


public class User{
 private static final long serialVersionUID = 1L;

//  primary key
 private java.lang.Integer id;

 // fields
 private java.lang.String name;
 private java.lang.String pwd;
 private java.lang.String email;

 // collections
 private java.util.Set<Article> articlesByLastUpdateBy;
 private java.util.Set<Article> articlesByCreateBy;
 private java.util.Set<Board> boards;
 
 
 public java.util.Set<Article> getArticlesByCreateBy() {
  return articlesByCreateBy;
 }
 public void setArticlesByCreateBy(java.util.Set<Article> articlesByCreateBy) {
  this.articlesByCreateBy = articlesByCreateBy;
 }
 public java.util.Set<Article> getArticlesByLastUpdateBy() {
  return articlesByLastUpdateBy;
 }
 public void setArticlesByLastUpdateBy(
   java.util.Set<Article> articlesByLastUpdateBy) {
  this.articlesByLastUpdateBy = articlesByLastUpdateBy;
 }
 public java.util.Set<Board> getBoards() {
  return boards;
 }
 public void setBoards(java.util.Set<Board> boards) {
  this.boards = boards;
 }
 public java.lang.String getEmail() {
  return email;
 }
 public void setEmail(java.lang.String email) {
  this.email = email;
 }
 public java.lang.Integer getId() {
  return id;
 }
 public void setId(java.lang.Integer id) {
  this.id = id;
 }
 public java.lang.String getName() {
  return name;
 }
 public void setName(java.lang.String name) {
  this.name = name;
 }
 public java.lang.String getPwd() {
  return pwd;
 }
 public void setPwd(java.lang.String pwd) {
  this.pwd = pwd;
 }


}

//Board.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd " >

<hibernate-mapping package="com.lzy.forum.domain">
 <class
  name="Board"
  table="board"
 >
   <meta attribute="sync-DAO">false</meta>
  <id
   name="Id"
   type="integer"
   column="id"
  >
   <generator class="native"/>
  </id>

  <property
   name="Name"
   column="name"
   type="string"
   not-null="true"
   length="50"
  />
  <property
   name="Remark"
   column="remark"
   type="string"
   not-null="false"
   length="255"
  />
  <property
   name="CreateTime"
   column="create_time"
   type="timestamp"
   not-null="false"
   length="19"
  />
  <many-to-one
   name="CreateBy"
   column="create_by"
   class="User"
   not-null="true"
  >
  </many-to-one>
  <many-to-one
   name="Parent"
   column="parent_id"
   class="Board"   
   not-null="false"
   lazy="false">
  </many-to-one>
 

  <set name="Articles" inverse="true" lazy="true" order-by="id">
   <key column="board_id"/>【这里的board_id是后来修改的】
   <one-to-many class="Article"/>
  </set>

  <set name="ChildBoards" inverse="true"  lazy="true" order-by="id">
   <key column="parent_id"/>这里的parent_id是后来修改的】
   <one-to-many class="Board"/>
  </set>


 </class> 
</hibernate-mapping>

//Board.java
package com.lzy.forum.domain;

 


public class Board  {
 private static final long serialVersionUID = 1L;

//  primary key
 private java.lang.Integer id;

 // fields
 private java.lang.String name;
 private java.lang.String remark;
 private java.util.Date createTime;

 // many to one
 private com.lzy.forum.domain.User createBy;
 private com.lzy.forum.domain.Board parent;

 // collections
 private java.util.Set<com.lzy.forum.domain.Article> articles;
 private java.util.Set<com.lzy.forum.domain.Board> childBoards;
 
 
 public java.util.Set<com.lzy.forum.domain.Article> getArticles() {
  return articles;
 }
 public void setArticles(java.util.Set<com.lzy.forum.domain.Article> articles) {
  this.articles = articles;
 }
 public java.util.Set<com.lzy.forum.domain.Board> getChildBoards() {
  return childBoards;
 }
 public void setChildBoards(java.util.Set<com.lzy.forum.domain.Board> childBoards) {
  this.childBoards = childBoards;
 }
 public com.lzy.forum.domain.User getCreateBy() {
  return createBy;
 }
 public void setCreateBy(com.lzy.forum.domain.User createBy) {
  this.createBy = createBy;
 }
 public java.util.Date getCreateTime() {
  return createTime;
 }
 public void setCreateTime(java.util.Date createTime) {
  this.createTime = createTime;
 }
 public java.lang.Integer getId() {
  return id;
 }
 public void setId(java.lang.Integer id) {
  this.id = id;
 }
 public java.lang.String getName() {
  return name;
 }
 public void setName(java.lang.String name) {
  this.name = name;
 }
 public com.lzy.forum.domain.Board getParent() {
  return parent;
 }
 public void setParent(com.lzy.forum.domain.Board parent) {
  this.parent = parent;
 }
 public java.lang.String getRemark() {
  return remark;
 }
 public void setRemark(java.lang.String remark) {
  this.remark = remark;
 }


}

//Article.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd " >

<hibernate-mapping package="com.lzy.forum.domain">
 <class
  name="Article"
  table="article"
 >
  <meta attribute="sync-DAO">false</meta>
  <id
   name="Id"
   type="integer"
   column="id"
  >
   <generator class="native"/>
  </id>

  <property
   name="ArticleType"
   column="article_type"
   type="integer"
   not-null="true"
   length="11"
  />
  <property
   name="Title"
   column="title"
   type="string"
   not-null="false"
   length="255"
  />
  <property
   name="Body"
   column="body"
   type="string"
   not-null="false"
  />
  <property
   name="Hits"
   column="hits"
   type="integer"
   not-null="false"
   length="10"
  />
  <property
   name="Bytes"
   column="bytes"
   type="integer"
   not-null="false"
   length="10"
  />
  <property
   name="CreateTime"
   column="create_time"
   type="timestamp"
   not-null="false"
   length="19"
  />
  <property
   name="LastUpdateTime"
   column="last_update_time"
   type="timestamp"
   not-null="false"
   length="19"
  />
  <many-to-one
   name="LastUpdateBy"
   column="last_update_by"
   class="User"
   not-null="true"
  >
  </many-to-one>
  <many-to-one
   name="CreateBy"
   column="create_by"
   class="User"
   not-null="true"
  >
  </many-to-one>
  <many-to-one
   name="Parent"
   column="parent_id"
   class="Article"
   not-null="false"
  >
  </many-to-one>
  <many-to-one
   name="Board"
   column="board_id"
   class="Board"
   not-null="true"
  >
  </many-to-one>
 

  <set name="Articles" inverse="true" lazy="false" order-by="id">
   <key column="parent_id"/>【这里的parent_id是后来修改的】
   <one-to-many class="Article"/>
  </set>


 </class> 
</hibernate-mapping>



//Article.java
package com.lzy.forum.domain;

import java.util.Date;

 


public class Article  {
 private static final long serialVersionUID = 1L;
 // primary key
 private java.lang.Integer id;

 // fields
 private java.lang.Integer articleType;
 private java.lang.String title;
 private java.lang.String body;
 private java.lang.Integer hits;
 private java.lang.Integer bytes;
 private java.util.Date lastUpdateTime;
 private java.util.Date createTime;
 

 // many to one
 private com.lzy.forum.domain.User lastUpdateBy;
 private com.lzy.forum.domain.User createBy;
 private com.lzy.forum.domain.Article parent;
 private com.lzy.forum.domain.Board board;

 // collections
 private java.util.Set<com.lzy.forum.domain.Article> articles;

 public java.util.Set<com.lzy.forum.domain.Article> getArticles() {
  return articles;
 }

 public void setArticles(java.util.Set<com.lzy.forum.domain.Article> articles) {
  this.articles = articles;
 }

 public java.lang.Integer getArticleType() {
  return articleType;
 }

 public void setArticleType(java.lang.Integer articleType) {
  this.articleType = articleType;
 }

 public com.lzy.forum.domain.Board getBoard() {
  return board;
 }

 public void setBoard(com.lzy.forum.domain.Board board) {
  this.board = board;
 }

 public java.lang.String getBody() {
  return body;
 }

 public void setBody(java.lang.String body) {
  this.body = body;
 }

 public java.lang.Integer getBytes() {
  return bytes;
 }

 public void setBytes(java.lang.Integer bytes) {
  this.bytes = bytes;
 }

 public com.lzy.forum.domain.User getCreateBy() {
  return createBy;
 }

 public void setCreateBy(com.lzy.forum.domain.User createBy) {
  this.createBy = createBy;
 }

 public java.lang.Integer getHits() {
  return hits;
 }

 public void setHits(java.lang.Integer hits) {
  this.hits = hits;
 }

 public java.lang.Integer getId() {
  return id;
 }

 public void setId(java.lang.Integer id) {
  this.id = id;
 }

 public com.lzy.forum.domain.User getLastUpdateBy() {
  return lastUpdateBy;
 }

 public void setLastUpdateBy(com.lzy.forum.domain.User lastUpdateBy) {
  this.lastUpdateBy = lastUpdateBy;
 }

 public java.util.Date getLastUpdateTime() {
  return lastUpdateTime;
 }

 public void setLastUpdateTime(java.util.Date lastUpdateTime) {
  this.lastUpdateTime = lastUpdateTime;
 }

 public com.lzy.forum.domain.Article getParent() {
  return parent;
 }

 public void setParent(com.lzy.forum.domain.Article parent) {
  this.parent = parent;
 }

 public java.lang.String getTitle() {
  return title;
 }

 public void setTitle(java.lang.String title) {
  this.title = title;
 }
 public java.util.Date getCreateTime(){
  return createTime;
 }

 public void setCreateTime(java.util.Date createTime) {
  // TODO Auto-generated method stub
  this.createTime = createTime;
 }
 

}

接下来将会在web中加入Spring支持和为实体类提供DAO支持。(未完待续)

posted on 2006-07-18 09:54 all gone 阅读(1781) 评论(2)  编辑  收藏 所属分类: Java

评论

# re: SSH(Spring + Struts + Hibernate)小试 之一 2006-07-19 17:45 rox

其实可以看看《Spring Live》,有中文翻译版,
不同的框架官网都有完整代码下载。
讲解还是比较详细的。  回复  更多评论   

# re: SSH(Spring + Struts + Hibernate)小试 之一 2006-07-27 22:14 all gone

谢谢  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: