首页 > 代码库 > Hibernate之Hibernate环境配置

Hibernate之Hibernate环境配置

Hibernate之Hibernate环境配置

一、Hibernate环境搭建的步骤

1.添加Hibernate && SQLServer 的Jar
  antlr-2.7.7.jar
  dom4j-1.6.1.jar
  hibernate-commons-annotations-4.0.5.Final.jar
  hibernate-core-4.3.11.Final.jar
  hibernate-jpa-2.1-api-1.0.0.Final.jar
  jandex-1.1.0.Final.jar
  javassist-3.18.1-GA.jar
  jboss-logging-3.1.3.GA.jar
  jboss-logging-annotations-1.2.0.Beta1.jar
  jboss-transaction-api_1.2_spec-1.0.0.Final.jar


  sqljdbc4.jar

2.创建JavaBean类:User.java

package cn.com.zfc.hibernate.entities;

/**
*
* @title User
* @describe Hibernate 的 POJO
* @author 张富昌
* @date 2017年4月7日下午10:47:27
*/
public class User {
  private Integer id;
  private String userName;
  private String userDesc;

  public User() {
    super();
  }

  public User(String userName, String userDesc) {
    super();
    this.userName = userName;
    this.userDesc = userDesc;
  }

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public String getUserName() {
    return userName;
  }

  public void setUserName(String userName) {
    this.userName = userName;
  }

  public String getUserDesc() {
    return userDesc;
  }

  public void setUserDesc(String userDesc) {
    this.userDesc = userDesc;
  }

  @Override
  public String toString() {
    return "User [id=" + id + ", userName=" + userName + ", userDesc=" + userDesc + "]";
  }
}

3.使用Xxx.hbm.xml 映射文件映射JavaBean属性到数据库表之间的映射关系:User.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-3-19 13:53:52 by Hibernate Tools 3.5.0.Final -->
<!-- 一般写 package,简化全类名 -->
<hibernate-mapping package="cn.com.zfc.hibernate.entities">
  <class name="User" table="USERS">
    <!-- 映射主键,id:属性,column:字段 -->
    <id name="id" type="java.lang.Integer">
      <column name="ID" />
      <!-- 主键生成策略 -->
      <generator class="native" />
    </id>
    <!-- 映射普通字段,property:属性,column:字段 -->
    <property name="userName" type="java.lang.String">
      <column name="USER_NAME" />
    </property>

    <property name="userDesc" type="java.lang.String">
      <column name="USER_DESC" />
    </property>
  </class>
</hibernate-mapping>

4.在hibernate.cfg.xml Hibernate配置文件中配置Hibernate 数据源及相关属性:在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>
    <!-- Hibernate 的基本配置 -->

    <!-- 数据源信息配置 -->
    <!-- 数据库用户名 -->
    <property name="hibernate.connection.username">sa</property>
    <!-- 数据库密码 -->
    <property name="hibernate.connection.password">123456</property>
    <!-- 数据库驱动宝 -->
    <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
    <!-- 不指定数据库名称,可以在映射文件中指定数据库名称 -->
    <property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=hibernate-01</property>

    <!-- 指定在控制台输出 SQL 语句,便于调试 -->
    <property name="show_sql">true</property>
    <!-- 格式化 SQL 语句 -->
    <property name="format_sql">true</property>
    <!-- 指定数据库生成策略 -->
    <property name="hbm2ddl.auto">update</property>
    <!-- 配置数据库方言 -->
    <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>

    <!-- 注册 Hibernate 映射文件 -->
    <mapping resource="cn/com/zfc/hibernate/entities/User.hbm.xml" />
  </session-factory>
</hibernate-configuration>

5.使用Hibernate ORM框架完成对JavaBean对象的操作

  ①创建Configuration对象
    Configuration configuration = new Configuration().configure();
  ②创建SessionFactory 工厂类
    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
    SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
  ③创建Session对象
    Session session = sessionFactory.openSession();
  ④开启事物 Transaction(查询不需要事务)
    Transaction transaction = session.beginTransaction();
  ⑤执行业务******
    CRUD操作
  ⑥提交事物
    transaction.commit();
  ⑦关闭会话
    session.close();
  ⑧关闭 SessionFactory
    sessionFactory.close();

 6.数据库:hibernate-01

数据表:users

技术分享

 


二、JUNIT 单元测试初识

 

1.单元测试的基本概念

2.使用Junit 搭建Hibernate 测试环境

3.@Test @Before @After

4.Hibernate 环境的测试:TestHibernate.java

package cn.com.zfc.hibernate.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.junit.After;
import org.junit.Before;
import org.junit.Test;

import cn.com.zfc.hibernate.entities.User;

/**
*
* @title TestHibernate
* @describe 测试 Hibernate 的环境
* @author 张富昌
* @date 2017年4月7日下午10:47:05
*/
public class TestHibernate {

  private Configuration configuration = null;
  private SessionFactory sessionFactory = null;
  private Session session = null;
  private Transaction transaction;

  @Before
  public void init() {
    configuration = new Configuration().configure();
    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    session = sessionFactory.openSession();
    transaction = session.beginTransaction();
  }

  @After
  public void destory() {
    transaction.commit();
    session.close();
    sessionFactory.close();
  }

  // 添加
  @Test
  public void testSave() {
    User user = new User("UU-1", "爱生活,爱编程");
    session.save(user);
  }

  // 立即加载
  @Test
  public void testGet() {
    User user = (User) session.get(User.class, 1);
    System.out.println("user:" + user);
  }

  // 延迟加载:当数据真正使用时在开始加载
  @Test
  public void testLoad() {
    User user = (User) session.load(User.class, 1);
    System.out.println("user:" + user);
  }

  // 删除
  @Test
  public void testDelete() {
    User user = (User) session.load(User.class, 4);
    session.delete(user);
  }

  // 修改
  @Test
  public void testUpdate() {
    User user = (User) session.load(User.class, 1);
    user.setUserDesc("我是修改过的东西");
    session.update(user);
  }

  @Test
  public void dataSource() {
  }

}

 

Hibernate之Hibernate环境配置