溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

hibernate5.2的基本配置方法(詳解)

發(fā)布時(shí)間:2020-08-19 18:37:13 來(lái)源:腳本之家 閱讀:126 作者:jingxian 欄目:編程語(yǔ)言

目標(biāo):將Student實(shí)體對(duì)象加入數(shù)據(jù)庫(kù)

1、首先需要下載三個(gè)東西:hibernate,slf4j,mysql

2、分別取他們的包導(dǎo)入新建的項(xiàng)目中,我這里的版本是:hibernate-release-5.2.10里面lib目錄下的required中的全部文件 slf4j-1.7.25下的受slf4j-nop-1.7.25.jar mysql的mysql-connector-java-5.1.42-bin.jar

3、在src下配置hibernate.cfg.xml(建議直接去文檔復(fù)制然后改)

<?xml version='1.0' encoding='utf-8'?>
<!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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/text02</property>
<property name="connection.username">root</property>
<property name="connection.password">6530033197</property>
<!-- JDBC connection pool (use the built-in) -->
<!--
<property name="connection.pool_size">1</property>
-->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<!-- <property name="current_session_context_class">thread</property> -->
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!--
<property name="hbm2ddl.auto">update</property>
-->
<mapping resource="student/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>

4、在mysql中創(chuàng)建student表,字段:id age name

5、創(chuàng)建自己的實(shí)體類在src下建包student,然后建Class:Student.java

package student;

public class Student {
private int id;
private int age;
private String name;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student(int id, int age, String name) {
super();
this.id = id;
this.age = age;
this.name = name;
}

public Student() {
// TODO Auto-generated constructor stub
}

}

6、在對(duì)應(yīng)package即student下配置文件:Student.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="student">
<class name="Student" table="student">
<id name="id" column="id">
</id>
<property name="name" type="string" column="name"/>
<property name="age" type="int" column="age"/>
</class>
</hibernate-mapping>

7、創(chuàng)建測(cè)試類:StudentText.java

package student;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class StudentText {

public static void main(String[] args) {
Student stu = new Student();
stu.setId(4);
stu.setName("小明");
stu.setAge(12);

Configuration con = new Configuration();
SessionFactory sf = con.configure().buildSessionFactory();
Session s = sf.openSession();
s.beginTransaction();
s.save(stu);
s.getTransaction().commit();
s.close();
sf.close();

}

}

輸出結(jié)果,完成:

hibernate5.2的基本配置方法(詳解)

以上這篇hibernate5.2的基本配置方法(詳解)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI