Hibernate环境搭建和配置
2017-07-12 16:02
459 查看
hibernate是一个ORM(对象关系映射)框架,将实体类对象和数据库数据关系做了映射,使我们操作数据库时,方便简单。hibernate对JDBC重复的操作做了封装,而且使用hibernate开发的项目,不管数据库怎么改变,都不用修改代码,是需要将配置文件中的信息修改为对应的数据库信息即可。
今天我们开始学习hibernate,下面介绍一下hibernate的环境搭建和配置:
先看一下hibernate4_hello项目的整体架构
一、新建一个java工程,取名为hibernate4_hello,
右键项目名,新建一个floder文件,取名为lib
二、导入hibernate的jar包
从hibernate官网上(http://hibernate.org/orm/downloads/)下载hibernate.final文件,
我下载的是hibernate的4.3.11.final,如图:
解压后,找到hibernate-release-4.3.11.Final\lib\required这个地址,将里面的jar包拷贝一份到hibernate4_hello中的lib文件夹中
三、新建hibernate.cfg.xml配置文件
从解压的hibernate final文件中,找到如下路径:hibernate-release-4.3.11.Final\project\etc
里面有一个hibernate.cfg.xml文件,如图:
将hibernate.cfg.xml文件拷贝一份到hibernate4_hello项目的src文件夹下,
修改成如下:
其中,配置数据库的连接信息和方言,可以从hibernate-release-4.3.11.Final\project\etc这个路径下的hibernate.properties文件中寻找,
在hibernate.properties文件中有很多配置信息,我的项目是使用的mysql数据库,因此我找到了mysql的连接配置信息,如图:
配置到hibernate.cfg.xml中后如图:
四、新建实体类User
五、配置实体类User对应的配置文件User.hbm.xml
代码如下:
将User.hbm.xml配置文件放到hibernate.cfg.xml中,如图:
六、完成测试类
测试类的代码如下:
补充:
增加一个有事务回滚和异常处理的测试类,代码如下:
今天我们开始学习hibernate,下面介绍一下hibernate的环境搭建和配置:
先看一下hibernate4_hello项目的整体架构
一、新建一个java工程,取名为hibernate4_hello,
右键项目名,新建一个floder文件,取名为lib
二、导入hibernate的jar包
从hibernate官网上(http://hibernate.org/orm/downloads/)下载hibernate.final文件,
我下载的是hibernate的4.3.11.final,如图:
解压后,找到hibernate-release-4.3.11.Final\lib\required这个地址,将里面的jar包拷贝一份到hibernate4_hello中的lib文件夹中
三、新建hibernate.cfg.xml配置文件
从解压的hibernate final文件中,找到如下路径:hibernate-release-4.3.11.Final\project\etc
里面有一个hibernate.cfg.xml文件,如图:
将hibernate.cfg.xml文件拷贝一份到hibernate4_hello项目的src文件夹下,
修改成如下:
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 配置数据库连接信息 --> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="connection.url">jdbc:mysql:///hibernate4</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <!-- 数据库方言 --> <property name="hibernate.dialect"> org.hibernate.dialect.MySQL5Dialect </property> </session-factory> </hibernate-configuration>
其中,配置数据库的连接信息和方言,可以从hibernate-release-4.3.11.Final\project\etc这个路径下的hibernate.properties文件中寻找,
在hibernate.properties文件中有很多配置信息,我的项目是使用的mysql数据库,因此我找到了mysql的连接配置信息,如图:
配置到hibernate.cfg.xml中后如图:
四、新建实体类User
package com.robert.pojo; public class User { private int id ; private String name ; private String pwd ; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }
五、配置实体类User对应的配置文件User.hbm.xml
代码如下:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- This mapping demonstrates content-based discrimination for the table-per-hierarchy mapping strategy, using a formula discriminator. --> <hibernate-mapping> <class name="com.robert.pojo.User" table="user"> <id name="id"> <!-- 主键生成策略 --> <generator class="native" /> </id> <property name="name" /> <property name="pwd" /> </class> </hibernate-mapping>
将User.hbm.xml配置文件放到hibernate.cfg.xml中,如图:
六、完成测试类
测试类的代码如下:
package com.robert.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import com.robert.pojo.User; public class Test { public static void main(String[] args) { // 1.新建Configuration对象 Configuration cfg = new Configuration().configure(); // 2.通过Configuration创建sessionFactory对象 /** --在hibernate3中的写法 * SessionFactory sf = cfg.buildSessionFactory() ; * --在hibernate4.0~hibernate4.2使用 * ServiceRegistry sRegistry = new ServiceRegistryBuilder(). * applySettings(cfg.getProperties()) * .buildServiceRegistry() ; */ // 在hibernate4.3中的用法 ServiceRegistry sr = new StandardServiceRegistryBuilder() .applySettings(cfg.getProperties()).build(); SessionFactory sf = cfg.buildSessionFactory(sr); // 3.通过SessionFactory得到session对象 Session session = sf.openSession() ; // 4.通过session对象,得到Transaction对象,开启事务 Transaction tx = session.beginTransaction() ; // 5.保存数据 User user = new User() ; user.setName("罗伯特") ; user.setPwd("12345") ; session.save(user) ; // 6.提交事务 tx.commit(); // 7.关闭session session.close(); } }
补充:
增加一个有事务回滚和异常处理的测试类,代码如下:
package com.robert.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import com.robert.pojo.User; public class TestException { public static void main(String[] args) { Configuration cfg = null ; SessionFactory sf = null ; Session session = null ; Transaction tx = null ; try { cfg = new Configuration().configure(); ServiceRegistry sr = new StandardServiceRegistryBuilder() .applySettings(cfg.getProperties()).build(); sf = cfg.buildSessionFactory(sr); session = sf.openSession() ; tx = session.beginTransaction() ; User user = (User) session.get(User.class, 1); System.out.println("name="+user.getName()); tx.commit(); } catch (Exception e) { tx.rollback(); }finally{ if(session!=null && session.isOpen()) { session.close(); } } } }
相关文章推荐
- hibernate---搭建日志环境并配置显示DDL语句
- hibernate环境搭建(Hello world 配置文件版)
- Hibernate3.5.4---java application的xml和annotation环境搭建(hibernate.cfg.xml配置文件说明,映射文件Student.hbm.xml说明
- 第四步 hibernate核心配置文件(搭建环境)
- Hibernate环境搭建与配置方法(Hello world配置文件版)
- hibernate的环境搭建和安装插件和创建配置文件
- Hibernate环境搭建、配置及实现简单数据库存取
- Hibernate3.5.4---java application的xml和annotation环境搭建(hibernate.cfg.xml配置文件说明,映射文件Student.hbm.xml说明
- springmvc+spring-data-jpa+hibernate环境搭建与配置
- Hibernate---基础配置之日志信息slf 及搭建日志环境
- Hibernate环境搭建跟配置
- 简单的hibernate环境搭建、自动生成model/配置/hibernate.xml配置文件
- maven+springMVC+hibernate项目搭建(一基础环境配置)
- Hibernate环境搭建与配置
- Hibernate3.5.4---java application的xml和annotation环境搭建(hibernate.cfg.xml配置文件说明,映射文件Student.hbm.xml说明)
- 搭建和配置Spring与Hibernate整合的环境
- IDEA安装与破解/IDEA汉化版安装/IDEA + Maven + SpringMVC + Hibernate集成环境搭建/框架配置以及IDEA快捷使用
- 手把手配置Hibernate环境(JBuilderX版)
- ubuntu下并行环境搭建(1)rsh的配置
- spring+hibernate+struts环境搭建