Hibernate framework use case

Source: Internet
Author: User

Hibernate is a lightweight ORM framework for JDBC that acts as a persistence layer for the project

Hibernate-Dependent libraries:

Create a project to add a jar package:

Hibernate.cfg.xml:

<! DOCTYPE hibernate-configuration Public
"-//hibernate/hibernate Configuration DTD 3.0//en"
"Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" >

<session-factory>
<!--database connection information--
<property name= "Connection.driver_class" >
Com.mysql.jdbc.Driver
</property>
<property name= "Connection.url" >jdbc:mysql://localhost/mysql</property>
<property name= "Connection.username" >root</property>
<property name= "Connection.password" >sxh</property>
<!--True indicates that SQL sent hibernate to the database is displayed--
<property name= "Show_sql" >true</property>
<!--Database dialect---
<property name= "dialect" >
Org.hibernate.dialect.MySQLDialect
</property>
<mapping resource= "Cn/haidashu/pojo/users.hbm.xml"/>
</session-factory>

Users.hbm.xml:

<?xml version= "1.0"?>
<! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en"
"Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<class name= "Cn.haidashu.pojo.Users" table= "Users" >
<!--primary KEY policies--
<id name= "id" >
<generator class= "native"/>
</id>
<property name= "Name"/>
<property name= "pwd"/>
</class>

Users Category:

Package Cn.haidashu.pojo;

public class Users {
private int id;
private String name;
Private String pwd;
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 String getpwd () {
return pwd;
}
public void SetPwd (String pwd) {
This.pwd = pwd;
}
}

Test class:

Package Cn.haidashu.pojo;

Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.Transaction;
Import org.hibernate.cfg.Configuration;

public class Test {
public static void Main (string[] args) {
Configuration cfg = new configuration ();
Sessionfactory SF = Cfg.configure (). Buildsessionfactory ();
Session session = Sf.opensession ();
Session.begintransaction ();
Save data
Users user = new users ();
User.setname ("Zoe");
User.setpwd ("335666");
Session.save (user);
System.out.println ("1");
Commit a transaction
Tx.commit ();
Session.close ();
Session.gettransaction (). commit ();
Session.close ();
Sf.close ();

}
}

Code Address: Http://pan.baidu.com/s/1nve1E8T

Hibernate framework use case

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.