Hibernate's HelloWorld implementation

Source: Internet
Author: User

Start by creating a new Web project and then creating a new Lib folder and importing the appropriate jar packages

Hibernate Development Steps:

1. Create Hibernate profile

2. Create a Persistence class

3. Creating an Object-relational mapping file

As follows:

The corresponding jar packages are as follows:

(Jar package goes to hibernate file to find)

The Hibernate.cfg.xml code is as follows:

1<?xml version= "1.0" encoding= "UTF-8"?>2<! DOCTYPE hibernate-Configuration Public3"-//hibernate/hibernate Configuration DTD 3.0//en"4"Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >56<session-factory>7<!--Configure Hibernate basic information-8<property name= "Connection.username" >root</property>9<property name= "Connection.password" >root</property>Ten<property name= "Connection.driver_class" >com.mysql.jdbc.Driver</property> One<property name= "Connection.url" >jdbc:mysql:///hibernate1</property> A       -<!--Hibernate basic configuration-- -<!--Database dialect--- the<property name= "dialect" >org.hibernate.dialect.MySQLDialect</property> -  -<!--whether to print SQL-- -<property name= "Show_sql" >true</property> +<!--whether to format SQL-- -<property name= "Format_sql" >true</property> +<!--policies for generating tables-- A<property name= "Hbm2ddl.auto" >update</property> at       -       -       -<!--need to associate the mapping file-- -<mapping resource= "Com/nzx/hibernate/news.hbm.xml"/> -       in</session-factory> -

The Persistence class news code is as follows:

1  Packagecom.nzx.hibernate;2 3 Importjava.util.Date;4 5 /**6  * @authorZxN7  * @versioncreated: January 3, 2016 PM 2:13:578 * Class Description9  */Ten classNews { One     PrivateInteger ID; A     PrivateString title; -     PrivateString author; -     Privatedate date; the      PublicNews () { -         Super(); -         //TODO auto-generated Constructor stub -     } +      PublicNews (string title, string author, date date) { -         Super(); +      A          This. title =title; at          This. Author =author; -          This. Date =date; -     } -      PublicInteger getId () { -         returnID; -     } in      Public voidsetId (Integer id) { -          This. ID =ID; to     } +      PublicString GetTitle () { -         returntitle; the     } *      Public voidSettitle (String title) { $          This. title =title;Panax Notoginseng     } -      PublicString Getauthor () { the         returnauthor; +     } A      Public voidSetauthor (String author) { the          This. Author =author; +     } -      PublicDate getDate () { $         returndate; $     } -      Public voidsetDate (date date) { -          This. Date =date; the     } - @OverrideWuyi      PublicString toString () { the         return"News [id=] + ID +", title= "+ title +", author= "+author -+ ", date=" + Date + "]"; Wu     } -      About      $}

The Object relational mapping file News.hbm.xml code is as follows:

1<?xml version= "1.0"?>2<! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en"3"Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >4<!--Generated 2016-2-29 14:33:38 by Hibernate Tools 3.4.0.CR1--56<className= "Com.nzx.hibernate.News" table= "News" >7<id name= "id" type= "Java.lang.Integer" >8<column name= "ID"/>9<generatorclass= "Native"/>Ten</id> One<property name= "title" Type= "Java.lang.String" > A<column name= "TITLE"/> -</property> -<property name= "Author" type= "java.lang.String" > the<column name= "AUTHOR"/> -</property> -<property name= "Date" type= "Java.util.Date" > -<column name= "DATE"/> +</property> -</class> +

The JUnit test class Hibernatetest code is as follows:

1  Packagecom.nzx.hibernate;2 3 Importjava.util.Date;4 5 Importorg.hibernate.Session;6 Importorg.hibernate.SessionFactory;7 Importorg.hibernate.Transaction;8 Importorg.hibernate.cfg.Configuration;9 ImportOrg.hibernate.service.ServiceRegistry;Ten ImportOrg.hibernate.service.ServiceRegistryBuilder; One Importorg.junit.Test; A  -  -  Public classHibernatetest { the  - @Test -      Public voidTest () { -         //1. Create a Sessionfactory object +Sessionfactory sessionfactory =NULL; -          +         //1). Create a Configuration object: Basic configuration information and object-relational mapping information for hibernate AConfiguration Configuration =NewConfiguration (). Configure (); at         //2). Create a Servicerigistry object hibernate any configuration and services need to be registered in this object to take effect -          -Serviceregistry serviceregistry= -                  NewServiceregistrybuilder (). Applysettings (Configuration.getproperties ()). Buildserviceregistry (); -                          -Sessionfactory =configuration.buildsessionfactory (serviceregistry);  in         //2. Create a Session object -Session session =sessionfactory.opensession (); to         //3. Turn on the transaction +Transaction Transaction =session.begintransaction (); -         //4. Perform a save Operation theNews news =NewNews ("Java", "EE",NewDate (Newjava.util.Date (). GetTime ( )); * Session.save (news); $         //5. Commit a transactionPanax Notoginseng transaction.commit (); -          the         //6. Close Session + session.close (); A         //7. Close the Sessionfactory object the sessionfactory.close (); +          -          $     } $  -}

Executes the test method, creates a new table and appears with the inserted statement

Hibernate:
Insert
Into
NEWS
(TITLE, AUTHOR, DATE)
Values
(?, ?, ?)

Hibernate's HelloWorld implementation

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.