【Hibernate】(1)初识Hibernate

1. 概念

ORM(Object/Relationship Mapping):对象/关系映射

利用面相对象思想编写的数据库应用程序最终都是把对象信息保存在关系型数据库中,于是要编写很多和底层数据库相关的SQL语句。

Hibernate:

是Java领域的一款开源的ORM框架技术,是对JDBC进行了非常轻量级的对象封装。

其它主流的ORM框架技术:

(1). MyBatis:前身就是著名的iBatis

(2). Toplink:后备Oracle收购,并重新包装为Oracle AS Toplink

(3). EJB:本身是JavaEE的规范,重量级框架,使用起来比较复杂


2. 首个Hibernate程序

(1). 创建工程,导入所需Jar包

在下载的hibernate-release目录的lib下的required目录下的所有Jar包

(2). 创建Hibernate配置文件

右键src目录创建other,然后选择Hibernate Configuration File,点击finish即可。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
		"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<property name="connection.username">root</property>
		<property name="connection.password">123456</property>
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF-8</property>
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="show_sql">true</property>
		<property name="format_sql">true</property>
		<property name="hbm2ddl.auto">create</property>
	</session-factory>
</hibernate-configuration>
(3). 创建持久化类

创建类遵循JavaBean原则:

public class Student {

	private int id;
	private String name;
	private String gender;
	private Date birthday;
	private String address;

	public Student() {
	}

	public Student(int id, String name, String gender, Date birthday,
			String address) {
		this.id = id;
		this.name = name;
		this.gender = gender;
		this.birthday = birthday;
		this.address = address;
	}

	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;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", gender=" + gender
				+ ", birthday=" + birthday + ", address=" + address + "]";
	}
}

(4). 创建对象-关系映射文件

右键src创建other,选择Hibernate XML Mapping file,选择我们刚才创建的Student,finishi。

然后在hibernate.cfg.xml中加入这个<mapping>配置:

		<mapping resource="Student.hbm.xml" />
 我们在MySQL中创建数据库hibernate,选择字符集:utf8-utf8 general_ci,完成。

(5). 创建测试类

创建Source Folder目录,创建测试类StudentTest:

public class StudentTest {

	@Before
	public void init() {

	}

	@Test
	public void testSaveStudent() {

	}

	@After
	public void destory() {

	}
}
(6). 通过Hibernate API编写访问数据库的代码

补充我们的测试文件:

	@Before
	public void init() {
		// 创建配置对象
		Configuration config = new Configuration().configure();
		// 创建服务注册对象
		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
				.applySettings(config.getProperties()).buildServiceRegistry();
		// 创建会话工厂对象
		sessionFactory = config.buildSessionFactory(serviceRegistry);
		// 会话对象f
		session = sessionFactory.openSession();
		// 开启事务
		transaction = session.beginTransaction();
	}

	@Test
	public void testSaveStudent() {
		// 生成学生对象
		Student s1 = new Student(1, "张三", "男", new Date(), "西安");
		Student s2 = new Student(2, "张三", "男", new Date(), "西安");
		session.save(s1); // 保存对象进入数据库
		session.save(s2); // 保存对象进入数据库
	}

	@After
	public void destory() {
		transaction.commit(); // 提交事务
		session.close(); // 关闭会话
		sessionFactory.close(); // 关闭会话工厂
	}
右键testSaveStudent方法, 运行junit,进入MySQL查看数据,成功。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章