hibernate是我们做项目时经常采用的框架之一, 我们通常会有一个需求: 就是把实体类映射到数据库中, 当然采用.xml方式编写的实体是很容易被映射的. 因为它是纯粹的Hibernate实体.SchemaExport可以为我们轻松搞定. JDK5.0后, 出现了Annotation. 他的出现使我们不用写那么多配置文件了, 真是爽啊. 但是你会把采用Annotation标注的实体映射到数据库? 呵呵, 如果你还在找就看看.. Very Easy..
来看看代码.. 注意红色部分.
怎么样? 明白了吧. AnnotationConfiguration 就是用来读Annotation的.
最后还要在配置文件中声明才行.
Good Luck
来看看代码.. 注意红色部分.
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class DBCreater {
public static void main(String[] args){
Configuration conf = new AnnotationConfiguration().configure();
SchemaExport dbExport = new SchemaExport(conf);
dbExport.create(true, true);
}
}
怎么样? 明白了吧. AnnotationConfiguration 就是用来读Annotation的.
最后还要在配置文件中声明才行.
Good Luck