- 建立Person实体类.
- 建立PersonDao.
- 建立PersonManager 作为PersonDao的业务
- 使用JSF,
Struts 2,
Spring MVC or
Tapestry建立WEB层应用
一.建立Person实体类.
- 建立一个pojo加进JPA的Annotations
package net.meetrice.model;
![](images/None.gif)
import org.appfuse.model.BaseObject;
![](images/None.gif)
import javax.persistence.Entity;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.Column;
@Entity
![](images/ExpandedBlockStart.gif) public class Person extends BaseObject ...{
private Long id;
![](images/InBlock.gif)
private String firstName;
![](images/InBlock.gif)
private String lastName;
![](images/InBlock.gif)
@Override
![](images/ExpandedSubBlockStart.gif) public boolean equals(Object o) ...{
// TODO Auto-generated method stub
return false;
}
![](images/InBlock.gif)
@Override
![](images/ExpandedSubBlockStart.gif) public int hashCode() ...{
// TODO Auto-generated method stub
return 0;
}
![](images/InBlock.gif)
@Override
![](images/ExpandedSubBlockStart.gif) public String toString() ...{
// TODO Auto-generated method stub
return null;
}
![](images/InBlock.gif)
@Column(name="first_name", length=50)
![](images/ExpandedSubBlockStart.gif) public String getFirstName() ...{
return firstName;
}
![](images/InBlock.gif)
![](images/ExpandedSubBlockStart.gif) public void setFirstName(String firstName) ...{
this.firstName = firstName;
}
@Id @GeneratedValue(strategy = GenerationType.AUTO)
![](images/ExpandedSubBlockStart.gif) public Long getId() ...{
return id;
}
![](images/InBlock.gif)
![](images/ExpandedSubBlockStart.gif) public void setId(Long id) ...{
this.id = id;
}
@Column(name="last_name", length=50)
![](images/ExpandedSubBlockStart.gif) public String getLastName() ...{
return lastName;
}
![](images/InBlock.gif)
![](images/ExpandedSubBlockStart.gif) public void setLastName(String lastName) ...{
this.lastName = lastName;
}
![](images/InBlock.gif)
}
![](images/None.gif)
2.用Maven从一个对象建立一个数据库表
打开src/main/resources/hibernate.cfg.xml 在里面加上
<mapping class="org.appfuse.tutorial.model.Person"/>
然后运行mvn compile hibernate3:hbm2ddl
就会生成数据库表
create table person (id bigint not null auto_increment, first_name varchar(50), primary key (id)) type=InnoDB;
|