Java Learning notes--the first Hibernate framework Program

Source: Internet
Author: User

Program Structure

1. Build a table

This example selects SQL Server2008 as a database to add a ztest database in MySQL to create a CUSTOMER table

CREATE TABLE CUSTOMER (    INTEGERnotNULLPRIMARYKEY,      VARCHAR (not NULL,     VARCHAR());

2. Create a PO Object

Supplement: Pojo is an abbreviation for plain Ordinaryjava object, which refers to a generic Java object that does not use entity beans, and pojo can be used as an aid class to support business logic. Pojo can be understood as a simple entity class, as the name implies, the role of the Pojo class is to facilitate programmers to use the data tables in the database, Customer.java
Package Com.session;public class Customer {        private int id;    Private String username;    private String password;    public int getId () {        return ID;    }    Public String GetPassword () {        return password;    }    Public String GetUserName () {        return username;    }    public void setId (int id) {        this.id = ID;    }    public void SetPassword (String password) {        this.password = password;    }    public void Setusername (String username) {        this.username = username;    }}

3.XML image File

To tell the Hibernate object how to image to a database table, you need to write an XML mapping file named Customer.hbm.xml

Consistent with the corresponding table name, the XML mapping file is as follows

<?XML version= "1.0" encoding= "Utf-8"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://hibernate.source Forge.net/hibernate-mapping-3.0.dtd "><hibernate-mapping>    <classname= "Com.session.Customer"Table= "CUSTOMER">        <IDname= "id"column= "CID"type= "Java.lang.Integer">            <Generatorclass= "Increment" />        </ID>        < Propertyname= "username"column= "USERNAME"type= "Java.lang.String"/>        < Propertyname= "Password"column= "PASSWORD"type= "Java.lang.String"/>    </class></hibernate-mapping>

4. Define Hibernate configuration file

Primarily for sessionfactory configuration, hibernate can be configured using XML or a property file, which is configured using XML

The configuration file name is Hibernate.cfg.xml and can also be named other

<?XML version= "1.0" encoding= "Utf-8"?><!DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "Http://hi Bernate.sourceforge.net/hibernate-configuration-3.0.dtd "><hibernate-configuration>    <session-factory>         <!--SQL Server database driver -          < Propertyname= "Hibernate.connection.driver_class">Com.microsoft.sqlserver.jdbc.SQLServerDriver</ Property>          <!--MySQL database name -          < Propertyname= "Hibernate.connection.url">jdbc:sqlserver://localhost:1433; Databasename=ztest</ Property>          <!--login user name for database -          < Propertyname= "Hibernate.connection.username">Sa</ Property>          <!--login password for the database -          < Propertyname= "Hibernate.connection.password">123</ Property>          <MappingResource= "Com/session/customer.hbm.xml"/>        <!--Note: Must be "/" and not ".".  -    </session-factory>        </hibernate-configuration>

Note: The file header of this file is not the same as the file header in 3, note the third line of the version number do not write wrong

5. Writing the application

Here is a demo program

Package Com.session;import Org.hibernate.hibernateexception;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) {        try {sessionfactory SF =                new Configuration (). Configure (). Buildsessionfactory ();            Session session = Sf.opensession ();            Transaction tx = Session.begintransaction ();            for (int i = 0; i < i++) {                Customer customer = new Customer ();                Customer.setusername ("customer" + i);                Customer.setpassword ("Customer");                Session.save (customer);            }            Tx.commit ();            Session.close ();        } catch (Hibernateexception e) {            e.printstacktrace ();}}    }

Compile execution Test.java

Execute query statement in SQL Server2008: Select * from CUSTOMER

Results

An article with another 2004-year link: http://blog.csdn.net/doodoofish/article/details/43207/

Good technology will be constantly developed and perfected.

Java Learning notes--the first Hibernate framework Program

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.