Hibernate before playing

Source: Internet
Author: User

After so long, finally have time to learn to learn hibernate, say the configuration is really complicated.

In the development process, often see the stratification phenomenon, the main purpose is to understand the decoupling.

b/S min. Three layers:

View Presentation Layer
ACTION/SEVLET/XX data
JSP templates
Service Business Layer
DAO data Access Layer

Here is a true introduction to hibernate.

The first is to import some of the necessary jar packages; The database uses MySQL, and of course the database drives the database driver package.

Project Catalog:


The commissioning project is the Java project

Entity class User.java

Package Test.hibernate.domain;public class User {private int id;private String name;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;} @Overridepublic String toString () {//TODO auto-generated method Stubreturn "[user:id=" +id+ ", Name:" +name+ "]";}}
Configuration file Hibernate.cfg.xml, where the file name is Hibernate.cfg

<! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http://www.hibernate.org/ Dtd/hibernate-configuration-3.0.dtd ">
Object-to-table mapping file User.hbm.xml

<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping public        "-//hibernate/hibernate mapping DTD 3.0//en"        "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
The class attribute value of the ID is taken as native to represent the configuration attribute in the database

Main test Class App.java

Package Test.hibernate.domain;import Org.hibernate.sessionfactory;import Org.hibernate.transaction;import Org.hibernate.cfg.configuration;import Org.hibernate.classic.session;import Org.junit.test;public class App { private static Sessionfactory sessionfactory;static{configuration cfg=new Configuration (); Cfg.configure (" Hibernate.cfg.xml "); Sessionfactory=cfg.buildsessionfactory ();} @Testpublic void Testsave () throws Exception {User user=new User (); User.setname ("Zhang San"); Session session=sessionfactory.opensession (); Transaction Transaction = Session.begintransaction (); session.save (user); Transaction.commit (); Session.close ();// Close session, release resources} @Testpublic void Testget () throws Exception {session session=sessionfactory.opensession (); Transaction transaction=session.begintransaction (); User user= (user) Session.get (user.class, 1); SYSTEM.OUT.PRINTLN (user); Transaction.commit (); Session.close ();}}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. Reprint please specify the source Http://blog.csdn.net/lindonglian

Hibernate before playing

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.