() Hibernate usage of Java Web

Source: Internet
Author: User

Hibernate is an open source object-relational mapping framework that provides JDBC with a very lightweight object encapsulation that maps Pojo to database tables, is a fully automated ORM framework, and Hibernate automatically generates SQL statements, automatically executes, So that Java programmers can use object programming thinking to manipulate the database at will.

Orm:object realtion Mapping

One, import jar package

  

Second, create a database, build a table

  

Third, create the class, and the corresponding relationship mapping file for the table

Account.class

public class Account {/* must generate setter getter method, otherwise package exception */private int accountid;private String name;private int age;private String nickname;public int Getaccountid () {return accountId;} public void Setaccountid (int accountId) {this.accountid = accountId;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public String Getnickname () {return nickname;} public void Setnickname (String nickname) {this.nickname = nickname;}}

Account.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 ">

Four, configure Hibernate configuration file

Hibernate.cfg.xml

<?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 ">

Five, code calls

Public String Register () {System.out.println ("Start registration-------------------------");    Account Account = new account ();    Account.setaccountid (1);    Account.setname ("Xiao Gang");    Account.setage (15);    Account.setnickname ("xg");    Gets the management class object that loads the configuration file config = new configuration (); Config.configure ();    Default load Src/hibenrate.cfg.xml File//Create session factory object Sessionfactory SF = Config.buildsessionfactory ();    Create session (represents a session, a conversation with a database connection) session session = Sf.opensession ();    Open transaction Transaction tx = Session.begintransaction ();    Save-database Session.save (account);    Commit Transaction Tx.commit ();    Close Session.close ();    Sf.close ();  return SUCCESS; }

() Hibernate usage of Java Web

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.