The simplest introduction in Hibernate in history

xiaoxiao2021-03-06  57

The simplest introduction in Hibernate in history

Copyright Notice: 9CBS is this BLOG managed service provider. If this paper involves copyright issues, 9CBS does not assume relevant responsibilities, please contact the copyright owner directly with the article Author.

In fact, Hibernate itself is a separate framework that does not require any support for Web Server or Application Server. However, most of the Hibernate introductions have added a lot of non-Hibernate, such as Tomcat, Eclipse, Log4j, Struts, XDoclet, and even JBoss. This is easy to generate hibernate complex misunderstandings, especially the enthusiasm of the initiator.

In this article will not involve Eclipse, Log4j, Struts, Tomcat, XDoclet, and JBoss. The purpose of this article is to demonstrate the Hibernate installation process and the most basic function, thus giving the beginners a low gate threshold.

download file

You need Java SDK, Hibernate Package, ANT Pack, and JDBC Driver.

1, Hibernate package download address:

Http://prdownloads.sourceforge.net/hibernate/?sort_by=date&sort=desc

2, ANT package download address:

http://apache.130th.net/ant/binaries/apache-ant-1.6.1-bin.zip

3, JDBC Driver To set according to the Database you used, there will be on the Database official website. Hibernate supports common Database, such as MySQL, Oracle, PostgreSQL, and MS-SQL Server. These databases have JDBC Driver:

Oracle JDBC Driver download address (must agree to Oracle Agreement before downloading)

Http://otn.racle.com/software/htdocs/distlic.html?/software/tech/java/sqlj_jdbc/htdocs/jdbc9201.html

MySQL JDBC DRIVER download address

http://dev.mysql.com/downloads/connector/j/3.0.html

PostgreSQL JDBC DRIVER download address

http://jdbc.postgreSQL.org/download.html

MS-SQL Server JDBC DRIVER download address

http://www.microsoft.com/downloads/details.aspx?familyid=9f1874b6-f8e1-4bd6-947c-0fc5bf05bf71&displaylang=en

4. Differentiate the Hibernate package and the ANT package to C: / DEV / (this directory is not important, you can change any other directory).

Configuration Environment

1, you need to add a new environment variable: Ant_home, let it point to C: / dev /

. And add% Ant_Home% / bin in the PATH environment variable.

2, you need to add a new environment variable: java_home, let it point to your J2SDK root directory. And add% java_home% / bin in the PATH environment variable.

3. Create a project directory, such as C: / Workspace / My1Sthibernate.

In the project directory, create three directories: SRC, Classes, LIB.

In the lib directory, create two directories: Hibernate and DB.

This way you have the following file structure:

C: / Workspace / My1Sthibernate /

C: / Workspace / My1Sthibernate / SRC

C: / Workspace / My1Sthibernate / Classesc: / Workspace / My1Sthibernate / LIB

C: / Workspace / My1Sthibernate / lib / hibernate

C: / Workspace / My1Sthibernate / LIB / DB

4, will C: / dev /

/Hibernate2.jar files Copy to C: / Workspace / My1Sthibernate / lib / hibernate under.

Will C: / DEV /

All files under / LIB / under the same copy to c: / Workspace / my1sthibernate / lib / hibernate.

Copy your JDBC Driver file (typically a JAR file) Copy to C: / Workspace / My1Sthibernate / lib / dB.

Create a database

1. Use your favorite Database software to create a hibernate_test's database.

2, under this database, create a new Table called Customer

Create Table Customer

(

??? CID INTEGER NOT NULL PRIMARY Key,

??? Username varchar (12) Not null,

??? Password varchar (12)

);

Write a Java file

Public class customer {

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

??? private int id;

?????pring username;

??????????????????????????????????????????????????????????????????????????????

??? public int getId () {

????? RETURN ID;

???}

??? public string getpassword () {

???? Return Password;

???}

??? public string getusername () {

????? Return UserName;

???}

??? public void setid (int id) {

??? ??? this.id = ID;

???}

??? public void setpassword (String password) {

????? this.password = password;

???}

??? public void setusername (String Username) {

????? this.username = username

???}

}

This class is stored as a C: /Workspace/my1sthibernate/src/customer.java file.

Write TEST class

Import net.sf.hibernate. *;

Import net.sf.hibs. *;

Public class test {

??? public static void main (String [] args) {

????? Try {

??????? SessionFactory sf =

??? ??? ??? ??? New configure (). configure (). BuildSessionFactory ();

????????, Session session = sf.opensession ();

??? ??? ??? transaction tx = session.begintransaction ();

???????? for (int i = 0; i <200; i ) {

??? ???????? Customer customer = new customer (); ??? ??? ??? ??? Customer.SetuserName ("Customer" i);

????????? customer.setpassword ("Customer");

??? ???????? sympathion.save (Customer);

?????????

???????? tx.commit ();

???????? session.Close ();

??? ???} catch (hibernateException e) {

???????? e.printStackTrace ();

??????}

???}

}

This class is stored as a C: /Workspace/my1sthibernate/src/test.java file.

Create a Hibernate mapping file

Because there is only one class - Customer and a table --- Customer, you only need to create a mapping file - Customer.hbm.xml, to correspond to the relationship between the Customer class and the Customer table.

??? "- // hibernate / hibernate mapping dtd // en"

??? "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

????????

?????????

????????

????????

????????

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

This file is stored as C: /Workspace/my1sthibernate/src/customer.hbm.xml, and Customer.java is placed in the same directory.

Write an Ant Build.xml file

You don't have to know the details of this build.xml, actually Ant is not Hibernate. Here are Ant to simplify some tasks, such as compilation, COPY, running, etc.

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

???????

???????????

???????

???????

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

????????

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

???????

????????

?????????

???????????

?????????

????????

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

???????

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

???????

???????????

???????

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

Configure hibernate description files

The Hibernate description file can be a Properties or XML file, which is most important to define the connection of the database. I am listed here is an XML format hibernate.cfg.xml description file.

??? public "- // Hibernate / Hibernate Configuration DTD // En"

??? "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

????????

????????

True

????????

??????? oracle.jdbc.driver.Orcledriver

????????

????????

???????? jdbc: Oracle: OCI8: @Hibernate_test

????????

????????

????????, Your database username

????????

????????

????????, Your database password

????????

????????

??????????????????????????????????????????????????????????????????????????????????????????????????????

????????

????????

????????

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

If you are not Oracle 9i, you can go to C: / dev /

Find your database in /src/hipernate.properties file, and then replace the above corresponding values.

start operation

Under C: / Workspace / My1Sthibernate, run Ant Run. If you strictly follow the steps, you should see

Run:

??????? [java] log4j: warn no appenders could be found for logger (net.sf.hibernate.cfg.environment).

??????? [java] log4j: Warn please initialize the log4j system proteth.

??????? [java] Hibernate: INSERT INTO CUSTOMER (UserName, Password, CID) VALUES (?,?,?)

Build Successful

Take a look at your hibernate_test database, add 200 records in the CustMor table, but you have not written any JDBC CODE.

In the future, if you want to change the database, you only need to change the corresponding value in the hibernate.cfg.xml description file.

in conclusion

This article is a very low gateway introduction. I gave a friend who didn't understand Hibernate. He used his first Hibernate program for less than 30 minutes, which has caused him to interested Hibernate. However, the reader must recognize that this is just a beginning, and this article is a small ice crystal on a hibernate iceberg. A journey of a thousand miles begins with a single step, you can regard this article as a starting point towards Hibernate Avenue.

转载请注明原文地址:https://www.9cbs.com/read-117285.html

New Post(0)