hibernate笔记:一、快速上手
hibernate是一个开源的对象关系映射框架,它对jdbc进行了轻量级的对象封装,使用它我们可以使用对象编程思想来操作数据库,事实上,在java世界,它已成了ORM框架的代表。下面就一起来学习下hibernate。
一、获取hibernate。从https://www.hibernate.org/网站上可以获取到最新版本的hibernate和相关文档,笔记的例子使用了3.2.5。
二、快速上手。
1、打开myEclipse,新建一个java project,导入hibernate及JDBC相关jar包,就可以配置开发hibernate应用了。注:JDBC包是必需的,且需与数据对应,笔者使用的是mysql5.0数据库。
2、配置hibernte。在src目录下新建一个xml文件,名称为hibernate.cfg.xml(当然,你也可以不叫这个名称,不过在代码中要作相应的修改),拷贝如下内容:
代码
<!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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="com/eja/hibernate/domain/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory >
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="com/eja/hibernate/domain/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
3、新建一个实体类,User.java.为了方便,只有一个属性,就是name,如:
代码
package com.eja.hibernate.domain;
import java.util.Date;
public class User {
private int id;
private String name;
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;
}
}
import java.util.Date;
public class User {
private int id;
private String name;
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;
}
}
4、新建好实体类后,配置对应的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">
<hibernate-mapping package="com.eja.hibernate.domain">
<class name="User" >
<id name="id">
<generator class="native" />
</id>
<property name="name"/>
</class>
</hibernate-mapping>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.eja.hibernate.domain">
<class name="User" >
<id name="id">
<generator class="native" />
</id>
<property name="name"/>
</class>
</hibernate-mapping>
好。配置已经好了。测试一下效果。增加junit,编写如下测试类。
代码
package com.eja.hibernate.Test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import com.eja.hibernate.domain.User;
public class TestUser {
@Test
public void addUser() {
try {
Configuration cfg = new Configuration();
cfg.configure(); //如果配置的不是hibernate.cfg.xml。则需在此方法中引入
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
User user = new User();
user.setName("name");
session.save(user);
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import com.eja.hibernate.domain.User;
public class TestUser {
@Test
public void addUser() {
try {
Configuration cfg = new Configuration();
cfg.configure(); //如果配置的不是hibernate.cfg.xml。则需在此方法中引入
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
User user = new User();
user.setName("name");
session.save(user);
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
点击jnuit就可以测试了。