Hibernate Getting Started with your hand to choose from CHAINSHUN's Blog

xiaoxiao2021-03-06  40

All of the Hibernate I found is divided into three categories: 1, the analysis is very thorough, but only the main code is only the main code, and cannot be compiled. 2. The example of lifting can only be performed in some environment, such as Tomcat. 3, long story, light a hbm.xml wrote a paper, halo. Here, I provide a simplest introductory example. (Example) Based on the <2003-based servant's book> Transparent article "Hibernate Database") Steps: Preparation: Install the J2SE SDK 1.4.2 or above, and MySQL database (other database, please modify the configuration). 1, download Hibernate 2.1 Expand. 2. Create a working directory such as E: / my. 3. Establish the following files in the working directory (see the article last): a. SetEnv.bat // to set the compilation of the Hibernate application to run the desired class path B. Hibernate.properties // Hibernate configuration file, mainly used to configure The database c. User.hbm.xml // data class and data table mapping setting file d. Hbtest.java // main program E. User.java // Data class 4, modify setenv.bat, where hibernate_home should be set to you Your own hibernate directory. 5. Modify hibernate.properties comment with HyperSonics QL's setup, remove the Mysql settings Hibernate.Connection.url jdbc: mysql: /// Test Hibernate.Connection.parsword ×××××××× Set in the MySQL path, user name, password to fit it. 6, the compiler enters the working directory in the DOS window and executes setenv.bat. Then perform the following: Javac User.java Javac-SourcePath ./ Hbtest.java 7, execute Java Hbtest to view database TEST in MySQL, and a new table User should have been established and new records. 8. Your first hibernate application is complete.

Attached (all files other than hibernate.property) setenv.bat set hibernate_Home = E: /JAVA/HIBERNATE-2.1 # // To change to its own directory set classpath =% hibernate_home% / hibernate2.jar;% hibernate_home% / lib /DOM4J-1.4.jar;%Hibernate_Home%/LIB/commons-logging-1.0.4.jar;%ClassPath% set classpath =% hibernate_Home% / lib / commons-collections-2.1.1.jar;% hibernate_home% / lib /odmg-3.0.jar;%Hibernate_Home%/LIB/cglib-full-2.0.2.jar;%ClassPath% set classpath =% hibernate_home% / lib / mysql-connection-java-3.0.9-stable-bin.jar ;% HIBERNATE_HOME% / lib / ehcache-0.9.jar;% CLASSPATH% User.java

 public class User {private String id; private String password; private String mail; private String nick_name; User () {} User (String AID) {id = AID;} void setId (String AID) {ID = AID;} String getId () {return id;} void setpassword (string pass) {password = pass;} string getpassword () {Return password;} void setMail (String aMail) {mail = aMail;} String getMail () {return mail;} void setNick_name (String name) {nick_name = name;} String getNick_name () {return nick_name;}}  Hbtest.java 
 import net.sf.hibernate. *; Import.sf.hibernate.cfg.configuration; import net.sf.hibernate.tool.hbm2ddl. *; Public class hbtest {public static void main (String " ] args) {Try {configuration cfg = new configuration (); cfg.addclass (user.class); sessionFactory sessions = cfg.buildsessionFactory (); New SchemaExport (CFG) .create (true, true);

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

New Post(0)