Hibernate connection Database

Source: Internet
Author: User
Tags generator tomcat server

1. New Project
2. New Hibernate.xml Frame Right-click the project name->myeclipse->add hibernate
3. Create a new name to manipulate the database table. Hbm.xml module Right-click Src->new->xml (Basic Templates)--(change name) next-> (Create XMLFile from a DTD file ) next-> (Select XML Catalog entry) key found in Hibernate Mapping DTD 3.0 next->finish
4. Configure the name of the operation on the database table. Hbm.xml Module View Operations
(1). Right-click the module name->new->class Add name and table (database table name) properties
(2). Right-click Class, respectively new ID and property Note: The ID of the new column and the generator ID will be changed to manual input: <generator class= "Assigned"/>

1<?xml version= "1.0" encoding= "UTF-8"?>2<! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en"3"Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >45<className= "Com.abc.Person" table= "T_person" >6<id name= "id" type= "Java.lang.Integer" >7<column name= "person_id"/>8<generatorclass= "Assigned"/>9</id>Ten<property generated= "Never" lazy= "false" Name= "name" type= "java.lang.String"/> One<property generated= "Never" lazy= "false" name= "age" type= "Java.lang.Integer"/> A<list name= "Schools" table= "School" > -<key column= "person_id" not-NULL= "true"/> -<list-index base= "0" column= "List_order"/> the<element column= "Schoos_name" type= "java.lang.String"/> -</list> -</class> -

5. Configuring the Hibernate.xml Framework

Create a DB connection database

Select View Actions in Hibernate.xml

Select the DB database connection you created in DB driver and finally add in mappings Person.hbm.xml

6. Entity class operations that correspond to database tables

Mainly with the database table properties, fields consistent, right-click the space->source->generate Getters and Setters ... Add get and set

1  Packagecom.abc;2 3 Importjava.util.ArrayList;4 Importjava.util.List;5 6  Public classPerson {7     PrivateInteger ID;8     PrivateString Name;9     Private intAge ;Ten     PrivateList<string> schools=NewArraylist<string>(); One      PublicInteger GetID () { A         returnID; -     } -      Public voidSetID (Integer iD) { theID =ID; -     } -      PublicString GetName () { -         returnName; +     } -      Public voidsetName (String name) { +Name =name; A     } at      Public intGetage () { -         returnAge ; -     } -      Public voidSetage (intAge ) { -          This. Age =Age ; -     } in      PublicList<string>Getschools () { -         returnschools; to     } +      Public voidSetschools (list<string>schools) { -          This. Schools =schools; the     } *  $}

7. Call to use

1  Packagecom.abc;2 3 Importjava.util.ArrayList;4 Importjava.util.List;5 6 Importorg.hibernate.Session;7 Importorg.hibernate.SessionFactory;8 Importorg.hibernate.Transaction;9 Importorg.hibernate.cfg.Configuration;Ten  One  Public classTest { A  -    Public Static voidA () { -        theConfiguration configuration=NewConfiguration (). Configure (); -       //configuration.configure ("/hibernate.cfg.xml"); -        -Sessionfactory sessionfactory=configuration.buildsessionfactory (); +Session session=sessionfactory.opensession (); -Transaction trans=session.begintransaction (); +Person b=NewPerson (); AB.setid (123); atB.setage (0); -B.setname ("Zhang San"); -List<string> schools=NewArraylist<string>(); -Schools.add ("High School"); -Schools.add ("school")); - B.setschools (schools); in Session.save (b); - trans.commit (); to session.close (); +   } -  the}

8.index.jsp Calling Method

9. Click Add Tomcat server to run

Hibernate connection Database

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.