2. The sample code

Create a new one java engineering , Suppose it's called HibernateHelloWorld. stay src The next one package, It can be called com.sun.hibernate.model

2.1  Class code

Create a new simple class , Put it in com.sun.hibernate.model It's a bag . The contents are as follows :

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.sun.hibernate.model;
 
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;
     }
 
}

2.2 To configure hibernate The configuration file

hibernate\projec\etc Medium hiberante.cfg.xml It can be used as hibernate Configuration documentation for , Or you can use hibernate\documentation\manual\en-US\html_single\index.html As a template . stay src Create a new file under the folder , And named it hibernate.cfg.xml.( Cannot be named as another file name ) The most basic configuration files are as follows :

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?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" >root</property>
     <property name= "connection.password" >root</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>
         
     <!-- Echo all executed SQL to stdout -->
     <property name= "show_sql" > true </property>
         
     <!-- Enable Hibernate's automatic session context management -->
     <!--<property name= "current_session_context_class" >thread</property>-->
         
     <!-- Drop and re-create the database schema on startup -->
     <!-- <property name= "hbm2ddl.auto" >create</property> -->
         
     <!-- Disable the second-level cache -->
     <property name= "cache.provider_class" >org.hibernate.cache.NoCacheProvider</property>
     
     <mapping resource= "com/sun/hibernate/model/Student.hbm.xml" />
         
     </session-factory>
</hibernate-configuration>

mapping resource The value at can be modified according to the package name and class name . If the class name is Student, Then the class configuration file here must be :Student.hbm.xml.

2.3 class mapping file

Create a new file , Name it Student.hbm.xml, Put it in com.sun.hibernate.model It's a bag . The contents are as follows :

?
1
2
3
4
5
6
7
8
9
10
11
12
<?xml version= "1.0" encoding= "UTF-8" ?>
<!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.sun.hibernate.model" >
     < class name= "Student" >
         <id name= "id" ></id>
         <property name= "name" ></property>
         <property name= "age" ></property>
     </ class >
</hibernate-mapping>

Notice the configuration at the beginning of the file , Here and hibernate.cfg.xml Dissimilarity . If the configuration is similar to hiberante.cfg.xml equally , An error will be prompted at runtime :“ Document root element "hibernate-mapping" Must match DOCTYPE root "hibernate-configuration"  ”

2.4 StudentTest Test class

newly added Student.java Of junit Test class StudentTest.java, Put it in com.sun.hibernate.model The code under the package is as follows :

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.sun.hibernate.model;
 
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
 
public class StudentTest {
 
     public static void main(String[] args){
         Student s = new Student();
         s.setId( 1 );
         s.setName( "s1" );
         s.setAge( 1 );
         
         Configuration cfg = new Configuration();
         SessionFactory sf = cfg.configure().buildSessionFactory();
         
         Session session = sf.openSession();
         session.beginTransaction();
         session.save(s);
         session.getTransaction().commit();
         session.close();
         sf.close();    
     }
}

2.5. Running results

function StudentTest.java This class , Although the prompt input is successful . After searching the database , It can be found that the data has been stored in student In the data table . although myeclipse Will prompt buildSessionFactory() This function is deprecated, But in fact, the program can run successfully .

bibnernate(2) More articles about

Random recommendation

  1. AngularJS Authentication in

    Welcome your guidance and discussion : )   One .   The significance of authentication   First of all , The purpose of authentication for web applications is : Protect important resources in the website . For some reason, these resources are not public , For example, paid resources ( Only those who have paid can attend online courses ), Or one ...

  2. ext2 Disk layout

    summary           This blog focuses on ext2 Disk layout of the file system , namely ext2 What does the disk look like when it's formatted .   ext2 Disk layout   whatever Ext2 The first block in the partition is never Ext2 File system management , ...

  3. declare-styleable: Custom control properties

    http://www.cnblogs.com/jisheng/archive/2013/01/10/2854891.html In use , 1 TypedArray a = getContext() ...

  4. mvc User control ascx obtain View The value of the page

    <%Html.RenderAction("AscxSideNav", "UI", new {itemName=ViewData["ItemNam ...

  5. 【 turn 】(DT Series two )device tree The writing standard of

    Original website :http://www.cnblogs.com/biglucky/p/4057478.html devicetree The writing standard of Next, from the node , attribute ,reg,ranges, Interrupt controller and so on ...

  6. UnixShell Programming ( The third edition ) Two chapters

    This book is really well written , I can't stop reading it . 1, It's easy to think of commands as functions , And what follows is the parameter passed . echo Hello Word > Hi   The string hello Wrod write file ...

  7. Use spark-streaming Real time reading Kafka The statistical results are stored in MySQL

    In this article , We simulated a scene , Real time analysis of order data , Real time revenue Statistics . Scene simulation I'm trying to cover one of the most common scenarios in Engineering : 1) First , towards Kafka Write order data in real time ,JSON Format , Include orders ID- Order type - book ...

  8. FFmpeg Simple analysis of source code :avio_open2()

    ===================================================== FFmpeg Library function source code analysis article list : [ Architecture diagram ] FFmpeg Source code structure - decode F ...

  9. CF-339D- Line segment tree

    http://codeforces.com/problemset/problem/339/D Give a sequence . Change one of these values at a time and ask for the sequence's f(), Sequential f() Defined as : Every two adjacent elements by bit or get the length minus ...

  10. modularization &amp;os&amp;sys

    syspath python Use import The priority of module calls is based on sys.path It's the path , This variable is called in the order in which the positions in the list are in , If you find the corresponding module first , Call this module first . import sys pri ...