Hibernate配置文件及详解

时间:2022-08-03 19:35:25

XML方式:

1.导入hibernate包,以及数据库驱动文件。

2.src目录下生成hibernate配置文件:hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--为true表示将Hibernate发送给数据库的sql显示出来 -->
<property name="show_sql">true</property>
<!-- SQL方言,这边设定的是MySQL -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- 一次读的数据库记录数 -->
<property name="jdbc.fetch_size">50</property>
<!-- 设定对数据库进行批量删除 -->
<property name="jdbc.batch_size">30</property>
<!--驱动程序-->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<!-- JDBC URL -->
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<!-- 数据库用户名-->
<property name="connection.username">root</property>
<!-- 数据库密码-->
<property name="connection.password">root</property>
<!--映射文件 -->
<mapping resource="hibernate-mapping.hbm.xml"/>
</session-factory>
</hibernate-configuration>

3.生成实体类映射文件:

<?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>

<class name="com.xy.business.User.model.User" table="USE_USER">
<!-- Ad mapping info-->
<id name="id" column="id">//name大小写必须和类对应
<generator class="sequence">
<param name="sequence">emp_sequence</param>
</generator>
</id>

<property name="realName" column="realname"/>

<property name="age" column="AGE"/>

<property name="sex" column="SEX"/>

<property name="userName" column="USERNAME"/>

<property name="passWord" column="password"/>

</class>
</hibernate-mapping>

4.创建实体类:

package com.xy.business.User.model;

public class User {
private long id;
private String userName;
private String realName;
private long age;
private String sex;
private String passWord;

....//set、get方法
}

此时hibernate搭建好了。写个类测试一下:

package com.xy.test;

import com.xy.business.User.model.User;
import com.xy.util.HibernateUtil;
import org.hibernate.*;

import java.util.List;


public class TestHibernateOralce {
public static void main(String [] args){
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction tr = session.beginTransaction();
        User user = new User();
        user.setPassword("321");
        user.setAge(15);
        user.setName("lisi");
        session.save(user);
        tr.commit();
        System.out.println("ok");

}
}

Annotation方式:

1.导入annotation方式使用的包。

2.src目录下生成hibernate配置文件:hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--为true表示将Hibernate发送给数据库的sql显示出来 -->
<property name="show_sql">true</property>
<!-- SQL方言,这边设定的是MySQL -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- 一次读的数据库记录数 -->
<property name="jdbc.fetch_size">50</property>
<!-- 设定对数据库进行批量删除 -->
<property name="jdbc.batch_size">30</property>
<!--驱动程序-->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<!-- JDBC URL -->
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<!-- 数据库用户名-->
<property name="connection.username">root</property>
<!-- 数据库密码-->
<property name="connection.password">root</property>
<!--映射实体类 -->
<mapping class="com.xy.busniess.model.Student" />//与xml方式相同。
</session-factory>
</hibernate-configuration>

3.创建实体类,在实体类的get方法上面用@标注:

package com.xy.busniess.model;

import javax.persistence.*;
import javax.persistence.Table;

@Entity
@Table(name = "Student")
@SequenceGenerator(name = "emp_sequence", sequenceName = "emp_sequence")
public class Student {
    
    private long id ;
    private String name;
    private String password;
    
    public Student() {
        super();
    }
    public Student(long id, String name, String password) {
        super();
        this.id = id;
        this.name = name;
        this.password = password;
    }
    
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "emp_sequence") //strategy 主键生成方式
    @Column(name = "ID", unique = true, nullable = false, precision = 22, scale = 0)// unique 是否唯一  nullable是否可为空     
public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    
    @Column(name = "name", length = 500)
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    @Column(name = "password", length = 500)
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", password="
                + password + "]";
    }
}



此时hibernate搭建好了。写个类测试一下:
package com.xy.test;

import com.xy.business.User.model.User;
import com.xy.util.HibernateUtil;
import org.hibernate.*;

import java.util.List;


public class TestHibernateOralce {
public static void main(String [] args){
SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();//用AnnotationConfiguration的方式。
        Session session = sessionFactory.openSession();
        Transaction tr = session.beginTransaction();
        Student student = new Student();
        student.setName("xuyang");
        student.setPassword("123123");
        tr.commit();
        System.out.println("save ok");

}
}

eg:创建sequence:

在数据库中创建sequence:

CREATE SEQUENCE emp_sequence
INCREMENT BY 1 -- 每次加几个
START WITH 1 -- 从1开始计数
NOMAXvalue -- 不设置最大值
NOCYCLE -- 一直累加,不循环

hibernate读取配置文件:

Configuration().configure()