Hibernate 开发步骤

 一、Hibernate 开发步骤(以Hibernate3.2.0为例):

1、Hibernate是开源的,下载Hibernate的源代码包,里面包括所有的jar包和文档。

2、添加Hibernate包:hibernate3.jar和lib下的Hibernate依赖包(大约一共39个)。

3、编写domain类:一些属性和get、set方法。

4、编写映射文件.hbm.xml:把java对象和关系模型对应起来。

5、编写配置文件 hibernate.cfg.xml:用于配置Hibernate,初始化时首先读取此配置文件。

6、编写测试类进行测试。

 

二、Hibernate 详细开发步骤:

1、编写domain类:com.cos.User

package com.cos;
import java.util.Date;

public class User {
    private int id;
    private String name;
    private Date birthday;

    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    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;
    }
}
 

domain类必须有个缺省的不带参数的构造方法。因为Hibernate使用了反射,有个instens方法。 

domain类必须是finnal的,因为懒加载的时候此类不能是finnal的。

 

2、编写映射文件:com.cos.User.hbm.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.cos">

	<class name="User" table="testuser">
		<id name="id" column="id">
			<generator class="native"/>
		</id>
		<property name="name" column="userName" unique="true" not-null="true"/>
        <property name="birthday"/>
	</class>

</hibernate-mapping>

 table代表表名。如果值testsuer是数据库的关键字,那么需要做以下修改:table="`testuser`"

 id表示主键。 

 <generator class=""/>:generator标签指定主键的产生方式。native表示自增长。

 column指定列名,不指定column的时候,表示属性名和字段名相同。

 unique="true" 表示此字段值在数据库中唯一。

 not-null="true" 表示不允许为空。

 

 3、编写配置文件:hibernate.cfg.xml 

<?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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql:///test</property>
        <property name="hibernate.connection.username"></property>
        <property name="hibernate.connection.password"></property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.hbm2ddl.auto">validate</property>
        <property name="show_sql">true</property>
        <mapping resource="com/cos/User.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

"hibernate.dialect":方言的意思。作用是告诉Hibernate使用的是哪一种数据库。

比如说在分页的时候各个数据库都是不一样的,Hibernate根据这个方言知道用户所使用的数据库后就会自动为其选择。

 

"hibernate.show_sql":输出HQL语句

"hibernate.format_sql" :格式化输出HQl语句

<mapping resource=""> :把映射文件包含进来

 "hibernate.hbm2ddl.auto":表示让Hibernate帮助建表,不手动建表。

 

4、编写测试类

package com.com.test;

import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.cos.entity.User;

public class Main {

	public static void main(String[] args) {
		//解析配置文件、解析映射文件。因此很耗时,一般只初始化一次
		Configuration conf = new Configuration();
		conf.configure();
		SessionFactory sf = conf.buildSessionFactory();
		Session session = sf.openSession();
		try{
			Transaction tx = session.beginTransaction();
			User user = new User();
			user.setName("wuyuan");
			user.setBirthday(new Date());
			session.save(user);
			tx.commit();
		}catch(Exception e){
			e.printStackTrace();
		} finally{
			session.close();
			session = null;
		}
		
		System.out.println("end");
	}
}

SessionFactory 相当于JDBC中的 DriverManager。用 SessionFactory 获得 Session。

SessionFactory 是工厂模式,是用来生产Session的。

 

JDBC缺省情况下自动提交为true。

Hibernate缺省情况下,自动提交为false。所以必须先手动开启事务,处理完毕之后再指定提交。

 

注意:

mysql的引擎分为:MyISAM和InnoDB。 

mysql4.0及以前缺省的引擎为MyISAM。

MyISAM不支持事务(开启事物和不开启事物都不起作用),因此效率高。但不支持外键。

InnoDB引擎支持事务。因此效率会低一点。但数据量大的时候InnoDB会高一些。

 

 

代码优化:

通常Hibernate对映射文件和配置文件读取只需要一次,因此我们可以给独立出来,让只初始化一次:

package com.cos.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
 * 工具类
 * @author Administrator
 */
public final class HibernateUtil {

	private static SessionFactory sessionFactory = null;
	
	private HibernateUtil(){
	}
	static{
		//解析配置文件、解析映射文件。
		Configuration conf = new Configuration();
		conf.configure();
		sessionFactory = conf.buildSessionFactory();
	}
	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}
}

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值