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://prdownload/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 download) http://otn.racle.com/software/htdocs/distlic.html?/software/tech/java/sqlj_jdbc/htdocs/jdbc9201.html
MySQL JDBC DRIVER Download Http://dev.mysql.com/downloads/connector/J/3.0.html
PostgreSQL JDBC Driver Download 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 the C: / dev / 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 / srcc: / workspace / My1stHibernate / classesc: / workspace / My1stHibernate / libc: / workspace / My1stHibernate / lib / hibernatec: / workspace / My1stHibernate / lib / db 4. Under the directory of the C: / Dev / 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 Custom (CID INTEGER NOT NULL Primary Key, Username VARCHAR (12) Not Null, Password Varchar (12)); Write a Java file Public Class Customer {Private INT ID; Private String UserName; Private String Password; 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 = usrname;} } This class is stored as a C: /Workspace/my1sthibernate/src/customer.java file. Write TEST class Import net.sf.hibernate. *; import net.sf.hibernate.cfg. *; Public class test { Public static void main (String [] args) { Try {sessionFactory sf = new configuration (). configure (). BuildSessionFactory (); session session = sf.opensesis (); transaction tx = session.begintransaction (); For (int i = 0; i <200; i ) {Customer Customer = New Customer (); Customer.SetUserName ("Customer" i); Customer.SetPassword ("Customer"); session.save (Customer); TX.comMit (); session.close (); } catch (HibernateException E) {E.PrintStackTrace ();}}} To C: /Workspace/my1sthiba Document. 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. XML Version = "1.0"?> 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. XML Version = "1.0"?> provject> 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. XML Version = "1.0" Encoding = "UTF-8"?> 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 properly [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. Note, in the "Writing Ant Build.xml file" section should be added "put the build.xml file in the C: / Workspace / my1sthibernate directory". [From: http://blog.9cbs.net/doodoofish/archive/2004/07/16/43207.aspx]