Hibernate environment setup and configuration method of Hello world configuration file version
- 2020-05-07 19:35:06
- OfStack
This article illustrates the Hibernate environment setup and configuration. Share with you for your reference, as follows:
1. Download hibernate jar package: hibernate-release-4.3.5.Final, import the necessary jar package, hibernate-release-4.3.5.Final \lib\required
There are 10 jar packages included.
2. Create a new java project.
3. Learn to build User Library yourself:
(a) project right-click -- build path -- configure build path -- add library.
(b) select User-library and create a new library named hibernate.
Add the jar package required by hibernate to library (path: hibernate-release-4.3.5.Final \lib\required), hello world, world, c, c, c, hibernate, hibernate
4. Introduce jdbc driver for database. mysql: mysql-connector-java-5.1.7-bin.jar
(a) create database:
create database hibernate;
(b) switch database:
use hibernate;
(c) create Student table:
create table Student(id int primary key,name varchar(20),age int);
5. hibernate configuration file hibernate.cfg.xml, copy is strongly recommended in the help document under the hibernate-release-4.3.5.Final \documentation\manual\ en-US \html_single path.
Location: 1.1.4. Hibernate configuration. After content modification:
<?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/hibernate</property>
<property name="connection.username">XXX</property>
<property name="connection.password">XXXX</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="com/huxing/hibernate/model/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Create Student class:
public class Student {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Create a mapping file for 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="com.huxing.hibernate.model">
<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>
Final test:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.huxing.hibernate.model.Student;
public class StudentTest {
public static void main(String[] args) {
Student a = new Student();
a.setId(123);
a.setAge(32);
a.setName("hello hibernate!");
Configuration cfg = new Configuration();
SessionFactory cf = cfg.configure().buildSessionFactory();
Session session = cf.openSession();
session.beginTransaction();
session.save(a);
session.getTransaction().commit();
session.close();
cf.close();
}
}
I hope this article is helpful for you to design Hibernate framework.