HIBERNATE Getting Started small case

Source: Internet
Author: User

I'm sure a lot of people will ask what is hibernate at the Beginning.

So let's find out what's going on Here. hibernate!

first, Hibernate is an Open-source object-relational mapping framework that provides JDBC with a very lightweight object encapsulation that allows Java programmers to manipulate the database at will using object programming Thinking. Hibernate can be applied to any JDBC application, both in Java client applications and in servlet/jsp Web applications, and most revolutionary of all, hibernate can replace CMP in the EE architecture of the EJB Application. The task of achieving data persistence.

We contacted Intelij idea, It is understood that hibernate this is a particularly powerful software, below with you probably know about the idea to see the approximate operation, and then interested students can go to Study.

First of all, I want to say that in IntelliJ idea "new project" is the equivalent of our Eclipse "workspace", and "new Module" is to create a project, this is a point to Note.

Architecture diagram in Intelij idea:

Creating a project is divided into the following steps:

(1) New Project

(1.1) the first step

(1.2) Step Two

(1.3) Step Three

Such a project will have, and then we will add the resources we need

(2) Create Lib Folder

These are the jar packages we need at this stage

(3) Create the resource folder, which contains the large configuration information of hibernate, in which you should pay attention to the naming problem: Hibernate.ccfg.xml must not be less ccfg.xml behind.

<?xml version= ' 1.0 ' encoding= ' utf-8 '? ><! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http://www.hibern ATE.ORG/DTD/HIBERNATE-CONFIGURATION-3.0.DTD "><!--associated Small configuration--
<mapping-resource> cn/lex/entity/student.hbm.xml</mapping-resource>

(4) configuration small configuration, exist in the physical layer, named: Student.hbm.xml

<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping public        "-//hibernate/hibernate mapping DTD 3.0//en"        "HTTP://WWW.HIBERNATE.ORG/DTD /HIBERNATE-MAPPING-3.0.DTD ">

(5) Writing Test

Package Cn.lex.test;import Cn.lex.entity.student;import Org.hibernate.query;import org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.transaction;import Org.hibernate.cfg.configuration;import Org.junit.after;import org.junit.before;import org.junit.test;import java.util.list;/** * Created by ACCP on 2017/1/9.  */public class Firsttest {Configuration cfg;   Configuration Object Session session;    Session Object Transaction tx;  The transaction object @Before public void Mybefore () {cfg=new Configuration (). Configure (); Gets the Configuration object sessionfactory Factory = Cfg.buildsessionfactory ();  Gets the Sessionfactory object session= factory.opensession ();  Gets the Session object tx= session.begintransaction ();        Open Transaction}//add student @Test public void add () {Student stu=new Student ();        Stu.setname ("big and Big Fat man");  Session.save (stu);    Submit to Database System.out.println ("save ok!"); }//query student name According to criteria @Test public void Select () {String hql= ' from Student where id=? ";        Query query = Session.createquery (hql);        Query.setparameter (0,26);        Student stu = (Student) Query.uniqueresult ();    System.out.println (stu.getname ()); }

Change Student's Name
@Test
public void update () {
Student Stu=session.load (student.class,serializable:1);
Stu.setname ("little Little Fat man");
System.out.println ("update ok!");

}

Delete Student
@Test
public void Delete () {
Student Stu=session.load (student.class,serializable:1);
Session.delete (stu);
System.out.println ("delete ok!");

}

Query students
@Test
public void Select () {
Student Stu=session.load (student.class,serializable:1);
System.out.println ("name:" +stu.getname ());
} @After public void after () {tx.commit (); Commit TRANSACTION Session.close (); Close session}}

If you've done all this, congratulations! You can run it and let's see what's going on in this exciting Moment.

HIBERNATE Getting Started small case

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.