Hibernate4Step1

hardwork

本着应用的目的快速学习Hibernate4框架

  • 初识Hibernate

版本

本人使用的是Hibernate4.3.5Final版本,使用之前需要将Hibernate中lib下的required中的所有jar包导入工程目录中。

前面的话

本节主要介绍

  1. 获取SessionFactory和Session
  2. 使用映射文件和注解两种方式进行简单的增删改查操作

hibernate.cfg.xml

位置:该文件应放置于src目录下

内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?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.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///db_hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>


<!-- 方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

<!-- 控制台显示SQL -->
<property name="show_sql">true</property>

<!-- 自动更新表结构 -->
<property name="hbm2ddl.auto">update</property>

<mapping resource="cn/codeforgod/model/Student.hbm.xml"/>
<mapping class="cn.codeforgod.model.Teacher"/>
</session-factory>

</hibernate-configuration>

注意:

<mapping resource="cn/codeforgod/model/Student.hbm.xml"/>

Student类的映射规则,使用hbm映射文件方式

<mapping class="cn.codeforgod.model.Teacher"/>

Teacher类的映射规则,使用类注解方式

编写Student类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package cn.codeforgod.model;


public class Student
{

private long id;
private String name;

public long getId()
{

return id;
}

public void setId(long id)
{

this.id = id;
}

public String getName()
{

return name;
}

public void setName(String name)
{

this.name = name;
}

}

编写Student.hbm.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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 package="cn.codeforgod.model">

<class name="Student" table="t_student">
<id name="id" column="stu_id">
<generator class="native"></generator>
</id>

<property name="name" column="name"></property>
</class>

</hibernate-mapping>

HibernateUtil

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package cn.codeforgod.util;


import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil
{

private static SessionFactory sessionFactory;

static
{
// 实例化配置文件
Configuration configuration = new Configuration().configure();


// 实例化服务登记
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();

// 获取Session工厂
sessionFactory = configuration
.buildSessionFactory(serviceRegistry);
}

public static SessionFactory getSessionFactory()
{

return sessionFactory;
}
}

JUnit测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package cn.codeforgod.test;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import cn.codeforgod.model.Student;
import cn.codeforgod.model.Teacher;
import cn.codeforgod.util.HibernateUtil;

public class HibernateUtilTest
{

public SessionFactory sessionFactory;
public Session session;

@Before
public void before()
{


sessionFactory = HibernateUtil.getSessionFactory();
session = sessionFactory.openSession();
session.beginTransaction();
}

@Test
public void test()
{

add();
// delete();
// update();
// getAll();
// saveT();
}

@After
public void after()
{

session.getTransaction().commit();
session.close();
sessionFactory.close();
}

public void add()
{


Student s = new Student();
s.setName("sfa");
session.save(s);
}

public void delete()
{

Object o = session.get(Student.class, Long.valueOf(1));
if(o == null)
{
System.out.println("id为1的Student不存在");
return;
}
session.delete(o);
}

public void update()
{

Student student = (Student) session.get(Student.class, Long.valueOf(6));
student.setName("xxx");
session.save(student);
}

public void getAll()
{

String hql = "from Student";
Query query = session.createQuery(hql);

@SuppressWarnings("rawtypes")
List list = query.list();
for(Object o : list)
{
System.out.println(o);
}
}

//savaTeacher
public void saveT()
{

Teacher s = new Teacher();
s.setName("sfa");
session.save(s);
}
}

注意:
这里可以不建表,因为Hibernate会自动建表。

使用Annotation建立Teacher类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package cn.codeforgod.model;


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name = "t_teacher")
public class Teacher
{

private int id;
private String name;

@Id
@GeneratedValue(generator="_native")
@GenericGenerator(name="_native", strategy="native")
@Column(name="tea_id")
public int getId()
{

return id;
}

public void setId(int id)
{

this.id = id;
}

public String getName()
{

return name;
}

public void setName(String name)
{

this.name = name;
}

}

对于JUnit测试中的SaveT方法,其它方法一样。

坚持原创技术分享,您的支持将鼓励我继续创作