"Hibernate" hibernate in the Eclipse+mysql configuration, installation, pure Java, the use of annotation and HQL to complete the database additions and deletions to search

Source: Internet
Author: User

This article has a lot of slots, in the hibernate4.x above with hibernate3.x writing to write. And there will be a lot of warnings in the program to eclipse, but that's a certain level of getting started with hibernate. After all, many of the books that introduce hibernate now are written in the hibernate3.x notation. The last time that "hibernate" is the simplest Hibernate project-the Account registration system (click to open the link), the technology of the complex struts is wrong. Because hibernate is done from Java to the database, from the database to the Java task. After that, the interaction between Java and the JSP foreground page is given to struts to complete. As a result, the use of a Java project is sufficient to illustrate Hibernate's problem. You're not a big deal. You can even think of hibernate as a Java plugin for "Java", which uses Java to encode and decode QR codes for URLs (click the Open link), like the Qrcode.lib QR code. Put the jar file in place and Hibernate is enough to start working.


I. BASIC OBJECTIVES

There is a usertable table in the MySQL database:


The data from the beginning is as follows:


After the end of the aaa,bbb inserted into the TestTable table, the TestTable table id=1 username to the CCC, delete username=ccc,number=bbb those items after three operations, the data is as follows:



II. Basic Preparation

1, this thing is a simple, to search on the internet on some mysql-connector-java-5.1.32.jar, with SQL can be completed, see the "MySQL" Java MySQL database additions and deletions, Java system class " (Click to open the link), but now everyone with hibernate you have to learn, first to Hibernate's official website (click to open the link) such as, download the latest version of Hibernate, foreign sites, speed on this, you can also download the following URL (click to open the link)


2. Create a new Java engineering hibernatetest in Eclipse, note Java Engineering, not Javaweb project! Open this Java project under your computer's directory, under SRC Create a new hibernate.cfg.xml in it to write the following things, pay attention to modify the things you should change.

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http://www.hibernate.org/ DTD/HIBERNATE-CONFIGURATION-3.0.DTD ">

3. Open the Java project with the directory under your computer, create a new Lib folder inside. Unzip the downloaded hibernate-release-4.3.8.final, hibernate-release-4.3.8.final\lib\required all lib files below, and copy to the Lib folder below the Java project. This time is really required below all lib, unlike the last "Struts2" Struts2 Pure manual installation, configuration and HelloWorld, with the latest version of Struts 2.3.20 GA Example (click to open the link) that way, more copy will not run up. On the Internet to search for some Mysql-connector-java-5.1.32.jar also put in Lib inside. In this case, the Lib folder under Java Engineering is as follows:


4, first refresh the eclipse of the hibernatetest, specifically, right-click this project, select Refresh

5. Then import the Lib package into eclipse, for example, right-click Java Project Hibernatetest Select Properties->java Build path->add Jars, select all the jar files under Hibernatetest to import.


This completes the basic configuration of hibernate.


Third, the production process

1, Hibernate is the database testtable the entire table into Java, and then let you slowly operation. First, take testtable into Java, create a testtable below SRC below,

Import javax.persistence.*; @Entity @table (name= "TestTable") public class TestTable {private int id;private String Username;private String number;//represents the primary key with the auto-generated item @id@generatedvaluepublic int getId () {return Id;} public void setId (int id) {this.id = ID;} @Column (name = "username") public String GetUserName () {return username;} public void Setusername (String username) {this.username = username;} @Column (name = "number") public String GetNumber () {return number;} public void Setnumber (String number) {this.number = number;}}

This testtable.java is mapped to the structure of the table:


The number of columns in the table, it should have a number of static member variables, and must have a corresponding getter and setter, this annotation way, different from the previous way to build an XML file, simple and fast.

In order for this mapping file to take effect, it is also necessary to add more words <mapping class= "TestTable"/> in Hibernate.cfg.xml, such as:


2, finally, is our real implementation of the file Test.java

Import java.util.*;import org.hibernate.*;import org.hibernate.cfg.*;p ublic class Test {public static void main (string[ ] args) {//Here is the so-called establishment transaction session Session=new Configuration (). Configure (). Buildsessionfactory (). Opensession ();        Transaction transaction=session.begintransaction ();        TestTable testtable=new testtable (); list<testtable> list = new arraylist<testtable> ();        System.out.println ("The TestTable table at this time:");        List=session.createquery ("from TestTable"). List (); for (int i=0;i<list.size (); i++) {System.out.println (List.get (i). GetId () + "," +list.get (i). GetUserName () + "," +        List.get (i). GetNumber ());        } System.out.println ("======== Take out testtable table 2nd =========="); Testtable= (testtable) Session.get (Testtable.class, 2);        System.out.println (Testtable.getid () + "," +testtable.getusername () + "," +testtable.getnumber ());  SYSTEM.OUT.PRINTLN ("========= inserts the AAA,BBB into the TestTable table ========="); Testtable=new testtable (); Testtable.setusername ("AAA"); Testtable.setnUmber ("BBB"); Session.save (testtable);        System.out.println ("The TestTable table at this time:");        List=session.createquery ("from TestTable"). List (); for (int i=0;i<list.size (); i++) {System.out.println (List.get (i). GetId () + "," +list.get (i). GetUserName () + "," +        List.get (i). GetNumber ());        } System.out.println ("============= the username of the id=1 in the TestTable table");        List=session.createquery ("from TestTable where id=1"). List ();        for (int i=0;i<list.size (); i++) {List.get (i). Setusername ("CCC");        Session.save (List.get (i));        } System.out.println ("The TestTable table at this time:");        List=session.createquery ("from TestTable"). List (); for (int i=0;i<list.size (); i++) {System.out.println (List.get (i). GetId () + "," +list.get (i). GetUserName () + "," +        List.get (i). GetNumber ());        } System.out.println ("======= Delete username=ccc,number=bbb those items ============="); List = Session.createquery ("From testtable where number like ' BBB ' and usernameLike ' CCC '). List ();        for (int i=0;i<list.size (); i++) {Session.delete (List.get (i));        } System.out.println ("The TestTable table at this time:");        List=session.createquery ("from TestTable"). List (); for (int i=0;i<list.size (); i++) {System.out.println (List.get (i). GetId () + "," +list.get (i). GetUserName () + "," +        List.get (i). GetNumber ()); }//Here is the so-called execution and Commit transaction transaction.commit (); Session.close ();}}
The central idea of the entire file is to use the HQL statement to convert the query results into a list using a dynamic array to hold the results of each query.

List must indicate a list of testtable.

HQL statement is actually not difficult, read a bit can understand, in the CreateQuery method inside the parameter is HQL. It is from testtable here class to query, must capitalize.

Adding a record is to construct a class first, and then save it with the Save method.

Delete the time also to construct a class and then delete.

When modifying, it is first to modify the item query out, construct this class, and then delete.

This is the whole idea of hibernate.

The Java program will not stop after it is run, and it should be stopped manually, because the database and Java mappings are in progress and are suitable for server operation. So it works with struts. The result of the operation is not affixed, too long, different table results are different. Anyway, this is an introduction to hibernate!


"Hibernate" hibernate in the Eclipse+mysql configuration, installation, pure Java, the use of annotation and HQL to complete the database additions and deletions to search

Related Article

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.