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 databaseMain 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