The simplest introduction in Hibernate in history

xiaoxiao2021-03-06  120

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 the file you require Java SDK, Hibernate Package, ANT Pack, and JDBC Driver. 1, Hibernate package download address: http://prdownload/hibernate/? Sort_by = DATE & SORT = desc2, ANT package download address: http://apache.130th.net/ant/binaries/apache-ant-1.6 .1-bin.zip3, 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.oracle.com/software/htdocs/distlic.html?/software/tech/java/sqlj_jdbc/HTDOCS/ jdbc9201.htmlMySQL JDBC Driver Download http://dev.mysql.com/downloads/connector/j/3.0.htmlPostgreSQL JDBC Driver Download http://jdbc.postgresql.org/download.htmlMS-SQL Server JDBC Driver Download http://www.microsoft.com/downloads/details.aspx?familyid=9f1874b6-f8e1-4bd6-947c-0fc5bf05bf71&displayLANG=EN4, extracting the Hibernate package and the ANT package to C: / dev / under (this directory is not important You can change any other directory). Configuring the environment 1, you need to add a new environment variable: Ant_home, let it point to the 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.

So 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 / db4, the directory of the C: / DEV / /Hibernate2.jar file COPY to C: / Workspace / my1sthibernate / lib / hibernate. The same copy to C: / Workspace / my1sthibernate / lib / hibernate will be on the directory> / lib / 你. Copy your JDBC Driver file (typically a JAR file) Copy to C: / Workspace / My1Sthibernate / lib / dB. Create a database 1, create a hibernate_test's database with your favorite Database software. 2, under this database, new Table called CustomerCreate Table Customer (CID INTEGER NOT NULL Primary Key, Username VARCHAR (12) NOT NULL, Password Varchar (12)); Writing 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 = username;}} the deposit such as c: /workspace/My1stHibernate/src/Customer.java file.

Write TEST IMPORT NET.SF.HIBERNATE. *; Import Net.sf.hibernate.cfg. *; Public Class Test {PUBLIC VOID MAIN (String [] args) {Try {sessionFactory sf = new configuration (). 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"); session.save (Customer);} tx.commit (); session.close ();} catch (hibernateException E) {E.PrintStackTrace ();} }} Use this class 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. save this file as c: /workspace/my1sthibernate/src/customer.hbm. XML, and Customer.java are placed in the same directory.

Write an Ant Build.xml file You don't have to know the details of this build.xml, in fact, Ant is not necessary for Hibernate. Here are Ant to simplify some tasks, such as compilation, COPY, running, etc.

< Target name = "init">

Configuring Hibernate Description File Hibernate Description files can be a Properties or XML file, where the most important is to define the connection of the database. I am listed here is an XML format hibernate.cfg.xml description file. true Oracle.jdbc.driver.racledriver JDBC: Oracle: OCI8: @Hibernate_test Your Database Username Your database password Net.sf.hibernate.diaalect.racle9diaAlact < ! - Specify the mapping file of Customer -> If you are not Oracle 9i, you can go to C: / dev / / src / hibernate The .properties file finds your database and replaces the value of the above. Start running to C: / Workspace / My1Sthibernate, running Ant Run.

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

New Post(0)