Hibernate之Hibernate環境配置

來源:互聯網
上載者:User

標籤:upd   utf-8   poj   mat   center   簡化   system   final   auth   

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 測試環境

[email protected] @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環境配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.