快速上手Hibernate

来源:互联网 发布:淘宝虚假交易买家处罚 编辑:程序博客网 时间:2024/05/16 04:51
ORM(Object/Relation Mapping)
ORM框架:
    Hibernate、Entity EJB、iBATIS、Oracle的TopLink、OJB
一、Hibernate的数据库操作
1.PO(Persistent Object)持久化对象
POJO(普通Java对象)
    POJO类
==
        
public class News
        
{
            
int id;
            String title;
            String content;
            
public void setId(int id)
            
{
                
this.id = id;
            }

            
public int getId()
            
{
                
return (this.id);
            }

            ……
        }

2.这个普通的JavaBean为使其具备持久化操作的能力,Hibernate应采用XML映射文件。
<?xml version='1.0' encoding='gb2312'?>
<!DOCTYPE hibernate-mapping
    PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--上面四行对所有的hibernate映射文件都相同-->
<!--hiberante-mapping是映射文件的根元素-->
<hibernate-mapping>
    
<!--每个class元素对应一个持久化对象,news为类名,news_table为表名-->
    
<class name="News" table="news_table">
        
<!--id元素定义持久化类的标识属性-->
        
<id name = "id" unsaved-value = "null">
            
<generator class="increment"/>
        
</id>
        
<!--property元素定义常规属性-->
        
<property name="title"/>
        
<property name="content"/>
    
</class>
</hibernate-mapping>
**==>PO = POJO + 映射文件
3.hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-cofiguration PUBLIC
    
"-//Hibernate/Hibernate Cofiguration DTD 3.0//EN"
    
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!--上面四行对所有的hibernate连接配置文件都相同-->
<hibernate-cofiguration>
    
<session-factory>
        
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
        <property name="connection.username">root</property>
        
<property name="connection.password">root</property>
        
<property name="connection.pool_size">5</property>
        
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        
<!--根据需要自动创建数据库-->
        
<property name="hbm2ddl.auto">create</property>
        
<!--罗列所有的映射文件-->
        
<mapping resource="News.hbm.xml"/>
    
</session-factory>
</hibernate-cofiguration>
4.下面是完成插入新闻的代码:
public class NewsDaoHibernate
{
    Cofiguration cofiguration;
    SessionFactory sessionFactory;
    Session session;
    
public void saveNews(News news)
    
{
        
//实例化Cogfiguration
        cofiguration = new Cofiguration().cofigure();
        
//实例化SessionFactory
        sessionFactory = cofiguration.buildSessionFactory();
        
//实例化session
        session = sessionFactory.openSession();
        
//开始事务
        Transaction tx = session.beginTransaction();
        
//增加新闻(处理业务)
        session.save(news);
        
//提交事务
        tx.commit();
        
//关闭session
        session.close();
    }

}

按PO与Session的关系,PO可有3个状态:瞬态、持久化、脱管
对PO的操作必须在Session管理下才能与数据库同步。Session由SessionFactory厂商提供,SessionFactory
是数据库编译后的内存镜像,通常一个应用对应一个SessonFactory对象,该对象由Cofiguration对象生成。
Cofiguration对象用来加载Hibernate配置文件。
5.最后使用如下方法来完成对新闻的增加:
public static void main(String[] args) {
    News n 
= new News();
    n.setTitle(
"新闻标题");
    n.setContent(
"新闻内容");
    NewsDaoHibernate ndh 
= new NewsDaoHibernate();
    ndh.save(n);
}
 
原创粉丝点击