Hibernate之环境搭建

 开始之前,我想先理清一个概念,即ORM是什么?

 ORM介绍

  全称:Object/Relation Mapping,即对象/关系映射。
  ORM也可以理解为一种规范,具体的ORM框架可作为应用程序和数据库的桥梁。
  ORM并不是一种具体的产品,而是一类框架的总称,它概述了这类框架的基本特征:
    完成面向对象的程序设计语言到关系数据库的映射;
    基于ORM框架完成映射后,既可利用面向对象程序设计语言的简单易用性,又可利用关系数据库的技术优势。
  ORM规范映射思想:
    一个表映射成一个类;
    一行记录(一条数据)映射成一个对象;
    一列(一个字段)映射成对象的属性。

 Hibernate介绍

  那么Hibernate则是目前最流行的ORM框架之一,它是对JDBC的封装。Hibernate采用了低侵入式的设计模式,也即完全采用普通的Java对象(POJO),而不需要继承Hibernate的某个超类或实现Hibernate的某个接口。目前最新版本5.1。

  Hibernate下载:http://hibernate.org/orm/

环境搭建

 1、新建一个Java项目Test,在Test下新建一个lib文件夹,将下载的hibernate/lib/required下的Jar文件拷贝到lib文件夹下,然后全选—右键—Build Path—Add To Path;

  不要忘了把MySQL的驱动Jar包也复制进来。

 2、新建一个实体类Person,这里通过注解的方式将实体类与数据表映射,而不是通过.hbm.xml文件的方式建立映射关系。

 1 package com.diesel.entity;
 2 
 3 import javax.persistence.Column;
 4 import javax.persistence.Entity;
 5 import javax.persistence.GeneratedValue;
 6 import javax.persistence.Id;
 7 import javax.persistence.Table;
 8 
 9 import org.hibernate.annotations.GenericGenerator;
10 
11 @Entity
12 @Table(name="t_person")
13 public class Person {
14     
15     @Id
16     @GeneratedValue(generator="systemUuid")
17     @GenericGenerator(name="systemUuid", strategy="uuid")
18     @Column(name="id")
19     private String id;
20 
21     @Column(name="name")
22     private String name;
23 
24     @Column(name="password")
25     private String password;
26 
27     @Column(name="age")
28     private Integer age;
29     
30     
31 
32     public String getId() {
33         return id;
34     }
35     public void setId(String id) {
36         this.id = id;
37     }
38 
39     public String getName() {
40         return name;
41     }
42     public void setName(String name) {
43         this.name = name;
44     }
45 
46     public String getPassword() {
47         return password;
48     }
49     public void setPassword(String password) {
50         this.password = password;
51     }
52 
53     public Integer getAge() {
54         return age;
55     }
56     public void setAge(Integer age) {
57         this.age = age;
58     }
59 
60     @Override
61     public String toString() {
62         return "Person [id=" + id + ", name=" + name + ", password=" + password + ", age=" + age + "]";
63     }
64     
65 }
View Code

  每一张表都会有一个唯一标识Id,可以通过注解的方式设置Id的生成规则,像上面代码中的方式,就是利用UUID来作为表Id的。或者也可以这样:

1 @Id
2 @GeneratedValue(strategy=GenerationType.IDENTITY)
3 @Column(name="id")
4 private Integer id;
View Code

  这表示自动增长类型的Id。更多关于其他的方式,这里就不再列举了。

 3、配置hibernate.cfg.xml,也就是告诉Hibernate要连接哪个数据库,并且配置一些其他属性。

 1 <?xml version='1.0' encoding='utf-8'?>
 2 <!--
 3   ~ Hibernate, Relational Persistence for Idiomatic Java
 4   ~
 5   ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
 6   ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
 7   -->
 8 <!DOCTYPE hibernate-configuration PUBLIC
 9         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
10         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
11 
12 <hibernate-configuration>
13 
14     <session-factory>
15 
16         <!-- Database connection settings -->
17         <!-- <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
18                     <property name="connection.url">jdbc:hsqldb:hsql://localhost/TestDB</property> -->
19 
20         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
21         <property name="connection.url">jdbc:mysql://localhost/Test</property>
22         <property name="connection.username">root</property>
23         <property name="connection.password">root</property>
24 
25         <!-- JDBC connection pool (use the built-in) -->
26         <property name="connection.pool_size">1</property>
27 
28         <!-- SQL dialect -->
29         <property name="dialect">
30             org.hibernate.dialect.MySQLInnoDBDialect
31         </property>
32 
33         <!-- Enable Hibernate's automatic session context management -->
34 <!--         <property name="current_session_context_class">thread</property> -->
35 
36 <!--         <property name="cache.use_query_cache">true</property> -->
37 <!--         <property name="cache.use_second_level_cache">true</property> -->
38 <!--         <property name="cache.use_structured_entries">true</property> -->
39 <!--         <property name="cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</property> -->
40 <!--         <property name="net.sf.ehcache.configurationResourceName">/hibernate-config/ehcache.xml</property> -->
41         <!-- Echo all executed SQL to stdout -->
42         <property name="show_sql">true</property>
43         
44         <mapping class="com.diesel.entity.Person"/>
45 
46     </session-factory>
47 
48 </hibernate-configuration>
View Code

 4、新建一个测试类。

 1 package com.diesel.test;
 2 
 3 import org.hibernate.Session;
 4 import org.hibernate.SessionFactory;
 5 import org.hibernate.Transaction;
 6 import org.hibernate.cfg.Configuration;
 7 
 8 import com.diesel.entity.Person;
 9 
10 public class Main {
11 
12     public static void main(String[] args) throws Exception {
13         
14         Configuration config = new Configuration().configure();
15         SessionFactory factory = config.buildSessionFactory();
16         Session session = factory.openSession();
17         
18         Person p = new Person();
19         p.setName("admin");
20         p.setPassword("123456");
21         
22         Transaction t = session.beginTransaction();
23         session.save(p);
24         t.commit();
25         
26     }
27 }
View Code

 5、在数据库Test中新建一张表t_person。

1 create table t_person(
2     id varchar(32) not null primary key,
3     name varchar(25) not null,
4     password varchar(25) not null,
5     age int
6 );
View Code

 运行测试类,如果能在t_person表中看到新增的记录,则说明程序执行成功了。

posted @ 2016-05-22 11:11  游玩屋  阅读(148)  评论(0编辑  收藏  举报