hibernate5.2的基本配置方法(詳解)
目標(biāo):將Student實(shí)體對(duì)象加入數(shù)據(jù)庫
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)建測試類: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的基本配置方法(詳解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)SHA-1算法實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)SHA-1算法,實(shí)例分析了java實(shí)現(xiàn)SHA-1算法的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
Java模擬撲克牌洗牌實(shí)現(xiàn)生成52張撲克的方法示例
這篇文章主要介紹了Java模擬撲克牌洗牌實(shí)現(xiàn)生成52張撲克的方法,涉及Java數(shù)組遍歷、重排及輸出等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
獲取JPEGImageEncoder和JPEGCode這兩個(gè)類的方法
下面小編就為大家?guī)硪黄@取JPEGImageEncoder和JPEGCode這兩個(gè)類的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
IDEA 將 SpringBoot 項(xiàng)目打包成jar的方法
這篇文章主要介紹了IDEA 將 SpringBoot 項(xiàng)目打包成jar的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Springboot之如何統(tǒng)計(jì)代碼執(zhí)行耗時(shí)時(shí)間
這篇文章主要介紹了Springboot之如何統(tǒng)計(jì)代碼執(zhí)行耗時(shí)時(shí)間問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
SpringBoot整合Redis實(shí)現(xiàn)附近位置查找(LBS)功能
Redis 提供了 GEO 數(shù)據(jù)結(jié)構(gòu),可以高效地存儲(chǔ)和查詢地理位置數(shù)據(jù),本文將介紹如何使用 Spring Boot + Redis 來實(shí)現(xiàn)附近位置查找,需要的可以了解下2025-03-03

