新建一个model类并带有注解
import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; @Entity//表示为实体类 @Table(name="test_user")//表名注解 public class User { private Integer id; private String userName; private String passWorld; private String userNick; private Integer enable; @Id//表示主键 @GenericGenerator(name = "generator", strategy = "increment") @GeneratedValue(generator = "generator")// 自增长 @Column(name = "id")// 类属性对应着表字段 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(name = "user_name") public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } @Column(name = "pass_world") public String getPassWorld() { return passWorld; } public void setPassWorld(String passWorld) { this.passWorld = passWorld; } @Column(name = "user_nick") public String getUserNick() { return userNick; } public void setUserNick(String userNick) { this.userNick = userNick; } @Column(name = "enable") public Integer getEnable() { return enable; } public void setEnable(Integer enable) { this.enable = enable; } }
在hibernate的session工厂中添加一个属性:
<property name="packagesToScan"> <list> <value>com.haier.hibernate.model</value> </list> </property>
即自动扫描路径。
开发时使用这个model即可。