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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值