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)的順時(shí)針/逆時(shí)針打印矩陣操作示例
這篇文章主要介紹了java實(shí)現(xiàn)的順時(shí)針/逆時(shí)針打印矩陣操作,涉及java基于數(shù)組的矩陣存儲(chǔ)、遍歷、打印輸出等相關(guān)操作技巧,需要的朋友可以參考下2019-12-12
Java內(nèi)部類_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
內(nèi)部類是指在一個(gè)外部類的內(nèi)部再定義一個(gè)類。下面通過本文給大家java內(nèi)部類的使用小結(jié),需要的朋友參考下吧2017-04-04
淺析Java中Apache BeanUtils和Spring BeanUtils的用法
這篇文章主要介紹了Java中Apache BeanUtils和Spring BeanUtils的用法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
Java中String字符串常量池和intern方法源碼分析
在之前的文章中,小編給大家介紹了String字符串的不可變性及其實(shí)現(xiàn)原理,其中給大家提到了字符串常量池的概念,那么什么是常量池,String字符串與常量池有什么關(guān)系,本文給大家嘮嘮字符串常量池及String#intern()方法的作用,需要的朋友可以參考下2023-05-05

