<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
Hibernate就可以根据定义的映射关系自动更新数据库表结构啦~
用hibernate框架pojo里用hibernate注解 例如:package com.ayumi.pojoimport java.io.Serializable
import javax.persistence.Columnimport javax.persistence.Entityimport javax.persistence.Idimport javax.persistence.Table
import org.hibernate.validator.NotNull
@Entity //实体对象@Table(name="users") //表名public class Users implements Serializable { private Integer id private String username private String password@Id //主键@Column(name="id") //列名 public Integer getId() { return id } public void setId(Integer id) { this.id = id }@Column(name="username",length=20) //列名 及 长度设定@NotNull //非空设定 public String getUsername() { return username } public void setUsername(String username) { this.username = username }@Column(name="password",length=20) //列名 及 长度设定 public String getPassword() { return password } public void setPassword(String password) { this.password = password }}
这样就对表的字段定义好了然后编写main方法测试类public static void main(String[] args) { Configuration cfg=new AnnotationConfiguration().configure() SchemaExport eport=new SchemaExport(cfg) eport.create(true, true) }
执行,这样就会在hibernate.cfg.xml里配置好的相应连接中对应的数据库下创建成功数据库表
既然是自动生成的都是配置好的。拿一个例子说吧,这个也是自动生成的<hibernate-mapping>
<class name="com.portallive.user.domain.User" table="t_user" schema="public">
<id name="UId" type="java.lang.Long">
<column name="u_id" />
<generator class="assigned"></generator>
</id>
<property name="ULoginid" type="java.lang.String">
<column name="u_loginid" length="128" />
</property>
<property name="UName" type="java.lang.String">
<column name="u_name" length="128" />
</property>
<property name="UPwd" type="java.lang.String">
<column name="u_pwd" length="128" />
</property>
<property name="UOrder" type="java.lang.Long">
<column name="u_order" />
</property>
<property name="UEmail" type="java.lang.String">
<column name="u_email" length="128" />
</property>
</class>
</hibernate-mapping>
类里的UId就对应表里的u_id,ULoginid对应表里的u_loginid。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)