Hibernate环境搭建和配置

末蓝、 2022-06-12 01:47 319阅读 0赞

hibernate是一个ORM(对象关系映射)框架,将实体类对象和数据库数据关系做了映射,使我们操作数据库时,方便简单。hibernate对JDBC重复的操作做了封装,而且使用hibernate开发的项目,不管数据库怎么改变,都不用修改代码,是需要将配置文件中的信息修改为对应的数据库信息即可。

今天我们开始学习hibernate,下面介绍一下hibernate的环境搭建和配置:

先看一下hibernate4_hello项目的整体架构

Center

一、新建一个java工程,取名为hibernate4_hello,

  1. 右键项目名,新建一个floder文件,取名为lib

二、导入hibernate的jar包

  1. hibernate官网上([http://hibernate.org/orm/downloads/][http_hibernate.org_orm_downloads])下载hibernate.final文件,

我下载的是hibernate的4.3.11.final,如图:

Center 1

解压后,找到hibernate-release-4.3.11.Final\lib\required这个地址,将里面的jar包拷贝一份到hibernate4_hello中的lib文件夹中

Center 2

三、新建hibernate.cfg.xml配置文件

  1. 从解压的hibernate final文件中,找到如下路径:hibernate-release-4.3.11.Final\\project\\etc

里面有一个hibernate.cfg.xml文件,如图:

Center 3

将hibernate.cfg.xml文件拷贝一份到hibernate4_hello项目的src文件夹下,

修改成如下:

  1. <!DOCTYPE hibernate-configuration PUBLIC
  2. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  3. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  4. <hibernate-configuration>
  5. <session-factory>
  6. <!-- 配置数据库连接信息 -->
  7. <property name="connection.driver_class">
  8. com.mysql.jdbc.Driver
  9. </property>
  10. <property name="connection.url">jdbc:mysql:///hibernate4</property>
  11. <property name="connection.username">root</property>
  12. <property name="connection.password">root</property>
  13. <!-- 数据库方言 -->
  14. <property name="hibernate.dialect">
  15. org.hibernate.dialect.MySQL5Dialect
  16. </property>
  17. </session-factory>
  18. </hibernate-configuration>

其中,配置数据库的连接信息和方言,可以从 hibernate-release-4.3.11.Final\project\etc这个路径下的hibernate.properties文件中寻找,

Center 4

在hibernate.properties文件中有很多配置信息,我的项目是使用的mysql数据库,因此我找到了mysql的连接配置信息,如图:

Center 5

配置到hibernate.cfg.xml中后如图:

Center 6

四、新建实体类User

  1. package com.robert.pojo;
  2. public class User {
  3. private int id ;
  4. private String name ;
  5. private String pwd ;
  6. public int getId() {
  7. return id;
  8. }
  9. public void setId(int id) {
  10. this.id = id;
  11. }
  12. public String getName() {
  13. return name;
  14. }
  15. public void setName(String name) {
  16. this.name = name;
  17. }
  18. public String getPwd() {
  19. return pwd;
  20. }
  21. public void setPwd(String pwd) {
  22. this.pwd = pwd;
  23. }
  24. }

五、配置实体类User对应的配置文件User.hbm.xml

代码如下:

  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  5. <!-- This mapping demonstrates content-based discrimination for the table-per-hierarchy
  6. mapping strategy, using a formula discriminator. -->
  7. <hibernate-mapping>
  8. <class name="com.robert.pojo.User" table="user">
  9. <id name="id">
  10. <!-- 主键生成策略 -->
  11. <generator class="native" />
  12. </id>
  13. <property name="name" />
  14. <property name="pwd" />
  15. </class>
  16. </hibernate-mapping>

将User.hbm.xml配置文件放到hibernate.cfg.xml中,如图:

Center 7

六、完成测试类

测试类的代码如下:

  1. package com.robert.test;
  2. import org.hibernate.Session;
  3. import org.hibernate.SessionFactory;
  4. import org.hibernate.Transaction;
  5. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
  6. import org.hibernate.cfg.Configuration;
  7. import org.hibernate.service.ServiceRegistry;
  8. import org.hibernate.service.ServiceRegistryBuilder;
  9. import com.robert.pojo.User;
  10. public class Test {
  11. public static void main(String[] args) {
  12. // 1.新建Configuration对象
  13. Configuration cfg = new Configuration().configure();
  14. // 2.通过Configuration创建sessionFactory对象
  15. /** --在hibernate3中的写法
  16. * SessionFactory sf = cfg.buildSessionFactory() ;
  17. * --在hibernate4.0~hibernate4.2使用
  18. * ServiceRegistry sRegistry = new ServiceRegistryBuilder().
  19. * applySettings(cfg.getProperties())
  20. * .buildServiceRegistry() ;
  21. */
  22. // 在hibernate4.3中的用法
  23. ServiceRegistry sr = new StandardServiceRegistryBuilder()
  24. .applySettings(cfg.getProperties()).build();
  25. SessionFactory sf = cfg.buildSessionFactory(sr);
  26. // 3.通过SessionFactory得到session对象
  27. Session session = sf.openSession() ;
  28. // 4.通过session对象,得到Transaction对象,开启事务
  29. Transaction tx = session.beginTransaction() ;
  30. // 5.保存数据
  31. User user = new User() ;
  32. user.setName("罗伯特") ;
  33. user.setPwd("12345") ;
  34. session.save(user) ;
  35. // 6.提交事务
  36. tx.commit();
  37. // 7.关闭session
  38. session.close();
  39. }
  40. }

补充:

增加一个有事务回滚和异常处理的测试类,代码如下:

  1. package com.robert.test;
  2. import org.hibernate.Session;
  3. import org.hibernate.SessionFactory;
  4. import org.hibernate.Transaction;
  5. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
  6. import org.hibernate.cfg.Configuration;
  7. import org.hibernate.service.ServiceRegistry;
  8. import com.robert.pojo.User;
  9. public class TestException {
  10. public static void main(String[] args) {
  11. Configuration cfg = null ;
  12. SessionFactory sf = null ;
  13. Session session = null ;
  14. Transaction tx = null ;
  15. try {
  16. cfg = new Configuration().configure();
  17. ServiceRegistry sr = new StandardServiceRegistryBuilder()
  18. .applySettings(cfg.getProperties()).build();
  19. sf = cfg.buildSessionFactory(sr);
  20. session = sf.openSession() ;
  21. tx = session.beginTransaction() ;
  22. User user = (User) session.get(User.class, 1);
  23. System.out.println("name="+user.getName());
  24. tx.commit();
  25. } catch (Exception e) {
  26. tx.rollback();
  27. }finally{
  28. if(session!=null && session.isOpen()) {
  29. session.close();
  30. }
  31. }
  32. }
  33. }

发表评论

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

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

相关阅读

    相关 Hibernate环境配置

    hibernate是一个ORM(对象关系映射)框架,将实体类对象和数据库数据关系做了映射,使我们操作数据库时,方便简单。hibernate对JDBC重复的操作做了封装,而且使用

    相关 配置环境

    一、搭建环境 1)新建虚拟机:硬盘大小20G,内存512M。2个网卡,光驱选择从ISO文件启动 ![1360675534_6608.PNG][] 2)启动虚拟机后,根