Hibernate entry Level

Source: Internet
Author: User

Hibernate is the data layer of the architecture, presumably everyone is very familiar with it, say not much, directly paste code.

The first is the configuration of the Hibernate.cfg.xml, which is actually the configuration of the database:

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

Here is the User.hbm.xml, which is actually the field that matches the database, for the persistence layer to operate.

<?xml version= "1.0" encoding= "Utf-8"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">

Here's the Java code.

The first is Logincontroller:

/* * This class is tested with *  *  */public class Logincontroller {public static void main (string[] args) {User user=new user (); user. SetName ("123"); User.setpassword ("123");    Logindao logindao=new Logindaoimpl ();      User User2=logindao.login (user);//Call the Login method        //print out the ID    if (user2!=null) {    System.out.println ("ID:" + User2.getid ());}}}   

Next is: Logindao; I omitted the business layer because the operations of the transaction were already handled at the DAO layer, and there was no need to throw it into the business layer.

This is not a good way to do this, this test is relatively simple.

Public interface Logindao {public    user login (user user);}

Here is the Daoimpl, this is not necessary, if you use JPA, this is not necessary.

public class Logindaoimpl implements logindao{    Private session session;    private static Hibernateutil Hibernateutil;        Static loading session factory production session class    static{    hibernateutil=new hibernateutil ();    }    Public user login (user user) {User user2=new User (); session=hibernateutil.getsession ();//Get Sessionquery produced by session factory Query=session.createquery ("Select u from User u"); List<user> list=query.list (); User2.setid (List.get (0). GetId ()); return user2;}}

Finally, the entity class.

public class User {    private int id;    private String name;    private String password;        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 GetPassword () {return password;} public void SetPassword (String password) {this.password = password;}   }

These things for reference only, feel the data layer of processing is quite easy to use, but alone with the configuration is too cumbersome, combined with spring will be very useful.


Hibernate entry Level

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.