hibernate-release-5.0.6.Final简单环境搭建

第一步:下载hibernate

去hibernate官网下载hibernate的最新版

http://hibernate.org/orm/downloads/

将下载的hibernate-release-5.0.6.Final.zip解压

hibernate的目录结构

    documentation目录是hibernate的文档

    lib目录是hibernate所需jar包

    project是hibernate提供的示例

    changelog.txt是hibernate的更新说明

    hibernate_logo.gif是hibernate的图标

    lgpl.txt是一个LICENSE

第二步:添加jar包

将lib目录下required文件夹中的所有jar包添加到项目的classpath下

将lib/optional/c3p0目录下的所有jar包添加到项目的classpath下

hibernate还需要连接数据库,将mysql-connector-java-xxx.jar添加到项目的classpath下

第三步:编写hibernate配置文件hibernate.cfg.xml

hibernate.cfg.xml如下




    
        
        com.mysql.jdbc.Driver
        
        jdbc:mysql://localhost/hibernate_test
        
        root
        
        cheng
        
        20
        
        1
        
        5000
        
        100
        3000
        2
        true
        
        org.hibernate.dialect.MySQL5InnoDBDialect
        
        update
        
        true
        
        true
        
        false
        
        
    

将hibernate.cfg.xml放在src目录下

第四步:编写pojo并添加注解

package com.test.dao;

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

@Entity
@Table(name="news_inf")
public class News {
    @Id//主键
    @GeneratedValue(strategy=GenerationType.IDENTITY)//自动增长
    private Integer id;
    // 消息标题
    private String title;
    // 消息内容
    private String content;

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
}

第五步:测试

package com.test.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

public class Test {

    public static void main(String[] args) {
        final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
                .configure() // configures settings from hibernate.cfg.xml
                .build();
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
            session = sessionFactory.openSession();
            //开始事务
            session.beginTransaction();
            //创建对象
            News news = new News();
            news.setContent("123");
            news.setTitle("678");
            //保存对象
            session.save(news);
            //提交事务
            session.getTransaction().commit();
            //关闭session

        }
        catch (Exception e) {
            session.getTransaction().rollback();
            StandardServiceRegistryBuilder.destroy( registry );
        }finally{
            session.close();
            sessionFactory.close();
        }
    }
}

如图

hibernate-release-5.0.6.Final简单环境搭建_第1张图片

参考文献

http://docs.jboss.org/hibernate/orm/5.0/quickstart/html/

你可能感兴趣的:(hibernate)