Hibernate之Hibernate环境配置

Posted 勇闯天涯zfc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 1 package cn.com.zfc.hibernate.entities;
 2 
 3 /**
 4 * 
 5 * @title User
 6 * @describe Hibernate 的 POJO
 7 * @author 张富昌
 8 * @date 2017年4月7日下午10:47:27
 9 */
10 public class User {
11   private Integer id;
12   private String userName;
13   private String userDesc;
14 
15   public User() {
16     super();
17   }
18 
19   public User(String userName, String userDesc) {
20     super();
21     this.userName = userName;
22     this.userDesc = userDesc;
23   }
24 
25   public Integer getId() {
26     return id;
27   }
28 
29   public void setId(Integer id) {
30     this.id = id;
31   }
32 
33   public String getUserName() {
34     return userName;
35   }
36 
37   public void setUserName(String userName) {
38     this.userName = userName;
39   }
40 
41   public String getUserDesc() {
42     return userDesc;
43   }
44 
45   public void setUserDesc(String userDesc) {
46     this.userDesc = userDesc;
47   }
48 
49   @Override
50   public String toString() {
51     return "User [id=" + id + ", userName=" + userName + ", userDesc=" + userDesc + "]";
52   }
53 }

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

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4 <!-- Generated 2017-3-19 13:53:52 by Hibernate Tools 3.5.0.Final -->
 5 <!-- 一般写 package,简化全类名 -->
 6 <hibernate-mapping package="cn.com.zfc.hibernate.entities">
 7   <class name="User" table="USERS">
 8     <!-- 映射主键,id:属性,column:字段 -->
 9     <id name="id" type="java.lang.Integer">
10       <column name="ID" />
11       <!-- 主键生成策略 -->
12       <generator class="native" />
13     </id>
14     <!-- 映射普通字段,property:属性,column:字段 -->
15     <property name="userName" type="java.lang.String">
16       <column name="USER_NAME" />
17     </property>
18 
19     <property name="userDesc" type="java.lang.String">
20       <column name="USER_DESC" />
21     </property>
22   </class>
23 </hibernate-mapping>

4.在hibernate.cfg.xml Hibernate配置文件中配置Hibernate 数据源及相关属性:在hibernate.cfg.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 5 
 6 <hibernate-configuration>
 7   <session-factory>
 8     <!-- Hibernate 的基本配置 -->
 9 
10     <!-- 数据源信息配置 -->
11     <!-- 数据库用户名 -->
12     <property name="hibernate.connection.username">sa</property>
13     <!-- 数据库密码 -->
14     <property name="hibernate.connection.password">123456</property>
15     <!-- 数据库驱动宝 -->
16     <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
17     <!-- 不指定数据库名称,可以在映射文件中指定数据库名称 -->
18     <property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=hibernate-01</property>
19 
20     <!-- 指定在控制台输出 SQL 语句,便于调试 -->
21     <property name="show_sql">true</property>
22     <!-- 格式化 SQL 语句 -->
23     <property name="format_sql">true</property>
24     <!-- 指定数据库生成策略 -->
25     <property name="hbm2ddl.auto">update</property>
26     <!-- 配置数据库方言 -->
27     <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
28 
29     <!-- 注册 Hibernate 映射文件 -->
30     <mapping resource="cn/com/zfc/hibernate/entities/User.hbm.xml" />
31   </session-factory>
32 </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

 1 package cn.com.zfc.hibernate.test;
 2 
 3 import org.hibernate.Session;
 4 import org.hibernate.SessionFactory;
 5 import org.hibernate.Transaction;
 6 import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
 7 import org.hibernate.cfg.Configuration;
 8 import org.hibernate.service.ServiceRegistry;
 9 import org.junit.After;
10 import org.junit.Before;
11 import org.junit.Test;
12 
13 import cn.com.zfc.hibernate.entities.User;
14 
15 /**
16 * 
17 * @title TestHibernate
18 * @describe 测试 Hibernate 的环境
19 * @author 张富昌
20 * @date 2017年4月7日下午10:47:05
21 */
22 public class TestHibernate {
23 
24   private Configuration configuration = null;
25   private SessionFactory sessionFactory = null;
26   private Session session = null;
27   private Transaction transaction;
28 
29   @Before
30   public void init() {
31     configuration = new Configuration().configure();
32     ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
33     sessionFactory = configuration.buildSessionFactory(serviceRegistry);
34     session = sessionFactory.openSession();
35     transaction = session.beginTransaction();
36   }
37 
38   @After
39   public void destory() {
40     transaction.commit();
41     session.close();
42     sessionFactory.close();
43   }
44 
45   // 添加
46   @Test
47   public void testSave() {
48     User user = new User("UU-1", "爱生活,爱编程");
49     session.save(user);
50   }
51 
52   // 立即加载
53   @Test
54   public void testGet() {
55     User user = (User) session.get(User.class, 1);
56     System.out.println("user:" + user);
57   }
58 
59   // 延迟加载:当数据真正使用时在开始加载
60   @Test
61   public void testLoad() {
62     User user = (User) session.load(User.class, 1);
63     System.out.println("user:" + user);
64   }
65 
66   // 删除
67   @Test
68   public void testDelete() {
69     User user = (User) session.load(User.class, 4);
70     session.delete(user);
71   }
72 
73   // 修改
74   @Test
75   public void testUpdate() {
76     User user = (User) session.load(User.class, 1);
77     user.setUserDesc("我是修改过的东西");
78     session.update(user);
79   }
80 
81   @Test
82   public void dataSource() {
83   }
84 
85 }

以上是关于Hibernate之Hibernate环境配置的主要内容,如果未能解决你的问题,请参考以下文章

JAVAWEB开发之Hibernate详解——Hibernate的框架概述开发流程CURD操作和核心配置与API以及Hibernate日志的使用

hibernate之初学一对多和多对一配置及使用

大家一起撸代码之——Hibernate各种主键生成策略与配置详解

Hibernate单表映射学习笔记之一——hibernalnate开发环境配置

Hibernate之HelloWorld

hibernate之helloword(环境搭建)