Hibernate基础复习

我就是我 2022-06-16 04:49 313阅读 0赞
  1. Hibernate3.6
  2. 持久层的框架
  3. 添加环境:
  4. 1jar
  5. 2,配置文件
  6. hibernate.cfg.xml
  7. xxx.hbm.xml
  8. 使用Hibernate实现CRUD操作
  9. // --- 准备
  10. Configuration cfg = new Configuration().configure(); // hibernate.cfg.xml
  11. SessionFactory sessionFactory = cfg.buildSessionFactory(); // 只需要一个
  12. // --- 模板代码
  13. Session session = sessionFactory.openSession();
  14. Transaction tx = null;
  15. try{
  16. tx = session.beginTransaction();
  17. // 操作
  18. tx.commit();
  19. }catch(Exception e){
  20. tx.rollback();
  21. throw e;
  22. }finally{
  23. session.close();
  24. }
  25. // --- 操作
  26. Session中的方法:
  27. save(Object) --> insert into ..
  28. update(Object) --> update ..
  29. saveOrUpdate(Object)
  30. delete(Object) --> delete ..
  31. get(Class, id) --> select ...
  32. createQuery(hql) --> select ..
  33. 主配置文件
  34. 1,数据库信息
  35. 方言、URL、驱动、用户名、密码
  36. 2,导入映射文件
  37. 3,其他配置
  38. show_sql = true
  39. hbm2ddl.auto = update
  40. 映射配置:
  41. 映射基础
  42. --
  43. 属性 --
  44. 映射普通属性
  45. name, type, column, length, not-null, ...
  46. 映射主键
  47. 主键生成略:native, uuid
  48. ====================================================================

发表评论

表情:
评论列表 (有 0 条评论,313人围观)

还没有评论,来说两句吧...

相关阅读