- 建立Person实体类.
- 建立PersonDao.
- 建立PersonManager 作为PersonDao的业务
- 使用JSF,
Struts 2,
Spring MVC or
Tapestry建立WEB层应用
一.建立Person实体类.
- 建立一个pojo加进JPA的Annotations
package net.meetrice.model;
import org.appfuse.model.BaseObject;
import javax.persistence.Entity;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.Column;
@Entity
public class Person extends BaseObject ...{
private Long id;
private String firstName;
private String lastName;
@Override
public boolean equals(Object o) ...{
// TODO Auto-generated method stub
return false;
}
@Override
public int hashCode() ...{
// TODO Auto-generated method stub
return 0;
}
@Override
public String toString() ...{
// TODO Auto-generated method stub
return null;
}
@Column(name="first_name", length=50)
public String getFirstName() ...{
return firstName;
}
public void setFirstName(String firstName) ...{
this.firstName = firstName;
}
@Id @GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() ...{
return id;
}
public void setId(Long id) ...{
this.id = id;
}
@Column(name="last_name", length=50)
public String getLastName() ...{
return lastName;
}
public void setLastName(String lastName) ...{
this.lastName = lastName;
}
}
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;
|