hibernate5.2.1版本使用

依赖的jar包:

依赖的jar

实例类 Student.java

package zh.hibernate.domain;

import java.io.Serializable;

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

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name = "student", catalog = "demo")
public class Student implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -2303585682086502906L;
    @GenericGenerator(name = "generator", strategy = "increment")
    @Id
    @GeneratedValue(generator = "generator")
    @Column(name = "id", unique = true, nullable = false)
    private int id;
    @Column(name = "no")
    private String no;
    @Column(name = "name")
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Student() {

    }

}
测试类 
package zh.hibernate.test;

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

import zh.hibernate.domain.Student;

public class Test {
    public static void main(String[] args) {

        // 1、新建Configuration对象
        // Configuration cfg =new Configuration().configure();
        // 2、通过Confinguration创建SessionFactory对象
        // 在hibernate3.x是这种写法
        // SessionFactory sf =cfg.buildSessionFactory();
        // hibernate4.3
        // ServiceRegistry registry = new StandardServiceRegistryBuilder()
        // .applySettings(cfg.getProperties())
        // .build();
        // SessionFactory sf = cfg.buildSessionFactory(registry);
        // 3、通过SessionFactory对象得到Session

        /**
         * 5.2.1配置的方法
         */
        StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
        SessionFactory sessionFactory = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
        Session session = sessionFactory.openSession();
        // 4、通过Session对象得到Transaction
        Transaction tx = session.beginTransaction();
        // 5、保存输出
        Student user = new Student();
        user.setName("小米");
        user.setNo("060814");
        session.save(user);
        // 6、提交事务
        tx.commit();
        // 7、关闭session
        session.close();
    }
}

hibernate.cfg.xml 配置文件

<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="show_sql">true</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
        <property name="connection.url">jdbc:mysql://localhost:3306/demo</property>  
        <property name="connection.username">root</property>  
        <property name="connection.password">root</property>  
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>  
        <mapping class="zh.hibernate.domain.Student" />
    </session-factory>
</hibernate-configuration>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
3.6.10.Final 2012-02-09 3.6.9.Final 2011-12-15 3.6.8.Final 2011-10-27 3.6.7.Final 2011-08-17 3.6.6.Final 2011-07-21 3.6.5.Final 2011-06-09 3.6.4.Final 2011-05-05 3.6.3.Final 2011-04-06 3.6.2.Final 2011-03-10 3.6.1.Final 2011-02-03 3.6.0.Final 2010-10-14 3.6.0.CR2 2010-09-29 3.6.0.CR1 2010-09-16 3.5.6-Final 2010-09-15 3.6.0.Beta4 2010-09-02 3.5.5-Final 2010-08-18 3.6.0.Beta3 2010-08-18 3.6.0.Beta2 2010-08-04 3.5.4-Final 2010-07-22 3.6.0.Beta1 2010-07-21 3.5.3-Final 2010-06-17 3.5.2-Final 2010-05-14 3.5.1-Final 2010-04-15 3.5.0-Final 2010-03-31 3.5.0-CR-2 2010-02-25 3.5.0-CR-1 2010-02-10 3.5.0-Beta-4 2010-01-29 3.5.0-Beta-3 2010-01-14 3.5.0-Beta-2 2009-11-03 3.5.0.Beta-1 2009-08-21 3.3.2.GA 2009-06-24 3.2.7.ga 2009-06-03 3.3.1.GA 2008-09-11 3.3.0.SP1 2008-08-20 3.3.0.GA 2008-08-15 3.3.0.cr2 2008-08-01 3.3.0.cr1 2008-04-29 3.2.6.ga 2008-02-07 3.2.5.ga 2007-07-31 3.2.4.sp1 2007-05-18 3.2.4.ga 2007-05-09 3.2.3.ga 2007-04-02 3.2.2.ga 2007-01-24 3.2.1.ga 2006-11-17 3.2.0.ga 2006-10-16 3.2.0.cr5 2006-10-05 3.2.0.cr4 2006-08-25 3.2.0.cr3 2006-07-06 3.2 cr2 2006-05-06 3.2 cr1 2006-03-27 3.1.3 2006-03-20 3.2 alpha2 2006-03-16 3.2 alpha1 2006-03-01 3.1.2 2006-01-28 3.1.1 2006-01-18 3.1 2005-12-12 3.1 rc1 2005-12-12 3.1 rc3 2005-11-18 3.1 rc2 2005-10-17 3.1 beta 3 2005-09-13 3.1 beta2 2005-08-16 3.1 beta1 2005-07-21 3.1 alpha1 2005-06-24 3.0.5 2005-05-25 3.0.4 2005-05-23 3.0.3 2005-05-08 3.0.2 2005-04-27 3.0.1 2005-04-18 3.0 final 2005-03-31 3.0 rc 1 2005-02-28 3.0 beta 4 2005-02-11 3.0 beta 3 2005-01-30 3.0 beta 2 2005-01-24 3.0 beta 1 2004-12-20 3.0 alpha 2004-08-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值