Hibernate学习笔记—Hibernate4.3环境搭建

来源:互联网 发布:登录淘宝要脸部拍摄 编辑:程序博客网 时间:2024/05/23 10:10

第一步:从Hibernate官网上下载4.3.11.Final版本的Hibernate,并解压缩

解压后lib文件夹下各个文件夹介绍

  • required文件夹:必须包
  • osgi文件夹:动态model包
  • optional文件夹:可选包
  • jpa-metamodel-generator文件夹:jpa反向工程包
  • jpa文件夹:jpa规范支持包
  • envers文件夹:4.3引入的新模块包

第二步:新建java项目,并导入jar包

所需的jar包如下:
这里写图片描述

这里写图片描述

这里写图片描述

还有数据库连接的jar包也要一并导入
如mssql的数据库的jar包 :sqljdbc42.jar

第三步:创建核心配置文件hibernate.cfg.xml

新建hibernate.cfg.xml文件并写入以下模板内容,修改对应的参数

<?xml version='1.0' encoding='utf-8'?><!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>        <!-- Database connection settings -->        <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>        <property name="connection.url">jdbc:hsqldb:hsql://localhost</property>        <property name="connection.username">sa</property>        <property name="connection.password"></property>        <!-- JDBC connection pool (use the built-in) -->        <property name="connection.pool_size">1</property>        <!-- SQL dialect -->        <property name="dialect">org.hibernate.dialect.HSQLDialect</property>        <!-- Enable Hibernate's automatic session context management -->        <property name="current_session_context_class">thread</property>        <!-- Disable the second-level cache  -->        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>        <!-- Echo all executed SQL to stdout -->        <property name="show_sql">true</property>        <!-- Drop and re-create the database schema on startup -->        <property name="hbm2ddl.auto">update</property>        <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/>    </session-factory></hibernate-configuration>

修改参数后的hibernate.cfg.xm文件内容

<?xml version='1.0' encoding='utf-8'?><!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.microsoft.sqlserver.jdbc.SQLServerDriver</property>        <property name="connection.url">jdbc:sqlserver://localhost:1433;database=XLLGenius</property>        <property name="connection.username">root</property>        <property name="connection.password">root</property>        <property name="connection.pool_size">1</property>        <property name="dialect">org.hibernate.dialect.SQLServer2008Dialect</property>        <property name="show_sql">true</property>        <mapping resource="com/pl/hiber/model/User.hbm.xml"/>    </session-factory></hibernate-configuration>

第四步:创建实体类(model)

public class User {    private int userId;    private String uname;    private int gender;    private Date birthday;    public int getUserId() {        return userId;    }    public void setUserId(int userId) {        this.userId = userId;    }    public String getUname() {        return uname;    }    public void setUname(String uname) {        this.uname = uname;    }    public int getGender() {        return gender;    }    public void setGender(int gender) {        this.gender = gender;    }    public Date getBirthday() {        return birthday;    }    public void setBirthday(Date birthday) {        this.birthday = birthday;    }}

第五步:创建实体类对应的xml文件(命名规范:实体类名+.hbm;如: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"><hibernate-mapping>    <class name="com.pl.hiber.model.User" table="t_user">        <id name="userId" column="user_id">            <generator class="assigned"></generator>        </id>        <property name="uname" column="uname"></property>        <property name="gender" column="gender"></property>        <property name="birthday" column="birthday"></property>    </class></hibernate-mapping>

第六步:创建数据表

public class DBExport {    public static void main(String[] args) {        //创建hibernate配置对象        Configuration cfg=new Configuration();        //指定hibernate.cfg.xml的位置        cfg.configure("hibernate.cfg.xml");        //创建表对象        SchemaExport se=new SchemaExport(cfg);        se.create(true,true);    }}

第七步:往数据表中插入一条记录

package com.pl.hiber.test;import static org.junit.Assert.*;import java.util.Date;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.junit.Test;import com.pl.hiber.model.User;public class TestHibernate {    @Test    public void test() {        // 创建配置对象        Configuration cfg = new Configuration();        // 配置hibernate核心文件的配置        cfg.configure("hibernate.cfg.xml");        // 注册配置属性信息        ServiceRegistry sr = new StandardServiceRegistryBuilder().                applySettings(cfg.getProperties()).build();        //创建SessionFactory        SessionFactory factory=cfg.buildSessionFactory(sr);        //创建Session        Session session=factory.openSession();        //开启事务        Transaction tx=session.beginTransaction();        //创建对象        User user=new User();        user.setUserId(1);        user.setUname("张三");        user.setGender(1);        user.setBirthday(new Date());        //通过session保存对象        session.save(user);        //提交事务        tx.commit();        session.close();    }}