hibernate初学者的第一步

新建项目

学习建立user-library-hibernate,并加入相应的jar

  a.项目右键-build path-configure build path-add library

  b.选择user-library,在其中新建library,命命为hibernate

  c. 在该library中加入hibernate所需要的jar

      hibernate3.3.2 

      /hibernate3.jar

      /lib/required目录下的所有包 6

      sl4j-nop jar

引入mysqlJDBC驱动包

MYSQL中建数据库和相应的表student(id,name,age);

建立hibernate配置文件hibernate.cfg.xml

      参考文档中COPY,修改对应的数据库连接,

建立student

建立映射文件Student.hbm.xml 参考相应文档

将映射文件加到hibernate-cfg.xml

     搭建日志环境并配置显示DDL语句

     slf的实现:slf4j nodep ,log4j ,jdk logging api ,apache common-log.

     slf4j.nop.jarslf-api.jar其相应的接口实现

     把slf的接口对到log4j的实现,user libraryhibernate,slf的实现slf4j-nop-1.5.8.jar去掉,添加log4j的实现log4j-1.2.15.jar,再添加一个slf-apilog4j转换器slf4j-log4j12-1.5.8.jar.

     把slf的接口转换成log4j的接口.最后添加一个log4j的配置文件log4j.properties

     利用HIBERNATE导出数据库建表

     //读取配置文件hibernate.cfg.xml

     Configuration cfg=new AnnotationConfiguration().configure();(有注解时使用AnnotationConfiguration),configure()可以手动指定配置文件名称.

     Configuration cfg=new Configuration(),会默认读取hibernate.properties文件

      //创建schemaExport对象

      import org.hibernate.tool.hbm2ddl.SchemaExport;

      SchemaExport export=new SchemaExport(cfg);

      //创建数据库表

      export.create(true, true);

强烈建议初学者尝试着去敲代码。

你可能感兴趣的:(hibernate初学者的第一步)