Hibernate application configuration

xiaoxiao2021-03-06  62

********************************** Hibernate application configuration instructions ********** *********************************************************** *********** ANT configuration ******************************************* Download Ant, decompress Ant to C: add Ant_Home = C: / ANTPATH ​​= C: / Ant / bin in the directory environment variable; enter the command line mode, input: Ant, build.xml does NOT EXIST! Build Failed This display shows that ANT has been configured success ******************************************* Middlegen Hibernate configuration ********************************* is Hibernate's O / R mapping tool http: / /Hibernate.BlueMars.Net/98.html can generate a * .hbm.xml file, is the map file required for Hibernate to download MiddleGen Hibernate, decompressed, such as D: / Hiber / MiddleGen-Hibernate-R1 / Enter the directory, [Configuring Database Taking MS SQL Server as an example, JDBC is driven to jsql connect (www.j-netdirect.com)] 1, enter / config / database / directory 2, driver JsqlConnect.jar to / lib / directory 3, edit MSSQL.XML File, as follows [Edit MiddleGen Hibernate configuration] 1, edit build.xml2, modify the database default file, find this line, probably About 22 lines

File: ./ Config / Database / HSQLDB.XML "-> // Original content, annotate // Plus]> 3, editing The root of the output is found, find the line, probably 30 rows // original content, annotate / / Plus, value is the package you want, such as COM4, ​​edit output, find this line, probably at around 210 row // Original content, annotation / / Plus, in the generated XML file, will use com.test as your package output directory to /build/gen-src/com/test/*.hbm.xml (iii corresponding table, named you The name defined by yourself) 5, define the output directory, find the line, last Change to the directory structure you want to output, it is best to use your package as a directory structure < / target> 6, when connecting SQL Server, prompting the database connection to success, but say no table, find the line

As the figure, the default will generate the corresponding name according to the table name and field name. Of course, you can also define it yourself. You can click [Generate], you can find / own /build/gen-src/com/test/*.hbm .xml, generate corresponding XML files ******************************************** Hibernate-extensions-2.0 application Configuring *********************************** This is Hibernate tool, 3 tools under Tools Class2HBM.BAT generates the corresponding * .hbm.xml file DDL2HBM.BAT automatically generates * .hbm.xml and * .java file according to your database table, automatically generate * .hbm.xml and * .java files (if you don't need to detail) according to your database table. Configuration, it is recommended to generate * .xml and * .java files) hbm2java.bat Before generating corresponding * .java files according to * .java file, first use ANT to compile [Note] Build.xml < Property name = "hibernate-core.home" value = "../..// hibernate-2.0" /> // Find this line, 40 rows left and right value to you have Hibernate unzipped directory [compiled] Compile-compiled file storage In /Tools/target/Hibernate-tools-2.0/ can now map [Applied] hbm2java.bat1 with compiled execution files, enter the command line mode, enter the hibernate-tools-2.0 directory, enter / bin2, first execute SetENV.BAT, set the environment variable, before run, check the setENv.bat file, open with the text editor, find the following line: set jdbc_driver = c: /progra ~ 1/sqllib/java/db2java.zip; C: /MM.MYSQL-2.0.14/mm.mysql-2.0.14-bin.jar;d:/J2SDK1.4.1/lib/jsqlconnect.jar// Put your JDBC driver into the remote hibernate_home = .. /. ./Hibernate-2.0// The original set hibernate_Home = D: /Hiber/Hibernate-2.0// Change into your current Hibernate-2.0 directory 3, execute hbm2java.bat d: / hiber / COM / TEST The * .hbm.xml file DDL2HBM.XML file DDL2HBM.BAT1, 2 steps are you generated by Middlegen Hibernate, running DDL2HBM, running a graphical interface 4, in the Connection panel, enter JDBC-driven information, such as: driverclass: com.jnetdirect.jsql.jsqldriverConnectionURL: JDBC: JSQLConnect: // server name / database = database name & user = username & password = password (Note that this is the driver support I used to support username and Password is placed in this format) UserName: Username (My ConnectionURL has filled in this information, here can not fill) Password: Password (I have filled this information, here you can fill it) 5, in the Code panel, you can define Package 6, in the Output panel, you can define the file output path 7, set it,

Enter the Table panel, click on the Tables ... button, if the database connection is successful, then a small panel is bounced, select the database you want to map, click OK, this time, you will display all the tables in the database, select you Required table, click "Generate", generate * .hbm.xml and * .java files in the output path [Note] Because SQLServer does not support the Sequences type in Hibernate, you will be wrong when testing the code, you open to build * .Hbm.xml will // here can be changed to Identity ************************* Actual application ************* *********************** Development Environment JBuilder9 [Configuration] 1, click "Tools" - "Configure JDK" -, add hibernate-2.0 / lib following jar file JSQLConnect.jarhibernate2.jarcommons-beanutils.jarcommons-collections.jarcommons-dbcp.jarcommons-lang.jarcommons-logging.jarcommons-pool.jardom4j.jarcglib-asm.jar [application] new construction testhiber create a new file table1.java , You can build an empty * .xml and * .java files mapped above to the SRC directory compilation in the JB engineering document, you can see that there is a * .class file in the classes, if there is no * .xml, Will generate * .xml to the corresponding directory to test hibernate-2.0 / src / hibernate.properties to edit Hibernate.properties1, the default connection database in the property file is: HSQLDIALECT, so you need to first ## hypersonicsql, Doll off the content below, probably at 73 lines 2, modify the property of ## JSQL Driver, Hibernate's JSQLConnect driver for MS SQL default, probably on 103 line ## ms SQL Server # hibernate.diaract net.sf.hibernate. ate.dialect.SybaseDialect # hibernate.connection.username sa # hibernate.connection.passwordhibernate.dialect net.sf.hibernate.dialect.SybaseDialecthibernate.connection.username sahibernate.connection.password 12345 ## JSQL Driver # hibernate.connection.driver_class com .jnetdirect.jsql.JSQLDriver # hibernate.connection.url jdbc: JSQLConnect: //WL99LCX27hibernate.connection.driver_class com.jnetdirect.jsql.JSQLDriverhibernate.connection.url jdbc: JSQLConnect: // gamebus / database =

Test's rest of your online document ********************************************************* *********************** / * ========================== ===================================== * / / * Table: users * / / * == ============================================================================================================================================================================================================= ========== * / Create Table DBo.users (userid int identity, username var) Not null, password varchar (100) NULL, Address Varchar (200) Null, Constraint PK_User Primary Key Clustered (Userid), go user.java, which is based on the table structure, automatically generated package com.test; import java.io.Serializable; import org.apache.commons.lang.builder.equalsbuilder; import org.apache.commons. lang.builder.HashCodeBuilder; import org.apache.commons.lang.builder.ToStringBuilder; / ** @author Hibernate CodeGenerator * / public class User implements Serializable {/ ** identifier field * / private Integer userid; / ** persistent field * / private string username; / ** Persistent Field * / private string password; / * Nullable Persistent Field * / Private string address;

/ ** full constructor * / public User (java.lang.Integer userid, java.lang.String username, java.lang.String password, java.lang.String address) {this.userid = userid; this.username = username "this.password = password; this.address = address;} / ** default constructor * / public user () {} / ** minimal constructor * / public user (java.lang.integer userid, java.lang.string username , java.lang.String password) {this.userid = userid; this.username = username; this.password = password;} public java.lang.Integer getUserid () {return this.userid;} public void setUserid (java. lang.Integer userid) {this.userid = userid;} public java.lang.String getUsername () {return this.username;} public void setUsername (java.lang.String username) {this.username = username;} public java .lang.string getp assword () {return this.password;} public void setPassword (java.lang.String password) {this.password = password;} public java.lang.String getAddress () {return this.address;} public void setAddress (java .lang.string address {this.address = address;} public string toString () {Return New TostringBuilder (this) .append ("userid", getUserid ()) .tostring ();} public boolean equals (Object Other) {IF (! (!) RETURN FALSE; User Castother = (user) ost;

Return New Equalsbuilder () .append (this.getuserid (), Castother.getuserid ()) .isequals ();} public int.com () {Return New hashcodebuilder () .append ()) .tohashcode ()) .tohashcode ();} } Trun.java, this is used to test the database operation package com.test; / ** *

Title: Test JDO *

description: *

Copyright: CopyRight ( c) 2003 *

Company: * @Author not attributable * @version 1.0 * / import.sf.hibernate.SessionFactory; import net.sf.hibernate.Session; import net.sf .hibernate.Transaction; import net.sf.hibernate.cfg.Configuration; import net.sf.hibernate.tool.hbm2ddl.SchemaExport; import net.sf.hibernate.Query; import java.util *;. public class trun {private Static sessionFactory sf; private static configuration conf; private static void main (string args []) {trun tt = new trun (); //tt.insertdb("hsh","dfaf", "23 Place "); //tt.deletedb(In Teger.Decode ("2")); //system.out.println (Tt.getdb(integer.Decode ("4 ")). getUserName ()); //tt.updatedb(integer.Decode(Integer.Decode( "3" ), "Zhou Hua"); / * list mylist = tt.listdb (20, 10); if (mylist.size ()> 0) {user myuser = null; for (int i = 0; i

} * /} public trun () {Try {conf = new configuration () .addclass (com.teest.user.class); ///conf.setproperty(//conf.setproperty( "Hibernate.xml.output_stylesheet""e://hibernate- Default.xslt "); System.out.println (" Prop: " conf.getProperty (" Hibernate.xml.output_stylesheet ")); sf = conf.buildsessionFactory ();} catch (Exception E) {E.PrintStackTrace }}} public list Listdb (int Start, int Pagenum) {list resultlist = null; try {s = sf.openSession (); transaction t = s.begintransaction (); query q = s.createQuery ("from user as User order by user.userid "); Q.SetFirstResult (start); Q.SetMaxResults (PAGENUM); ResultList = q.list (); t.commit ();} catch (Exception E) {E.PrintStackTrace (); } Return Resultlist;} public void deletedb (integer uid) {try {s = sf.openSession (); transaction t = s.begintransaction (); user m y = (user) sload (user.class, uid); s delete (my); t.commit ();} catch (exception e) {E.PrintStackTrace ();} finally {try {s.close ();} catCH (Exception E) {E.PrintStackTrace ();}}} public void updatedb (integer uid, string username) {try {s = sf.openSession (); transaction t = s.begintransaction (); user My = (user) s.Load (user.class, uid); my.setusername (username); t.commit ();} catch (exception e) {E.PrintStackTrace ();} finally {try {s.close ();} catch (exception e) {E.PrintStackTrace ();

}}} public user result = null; public user getdb (integer uid) {try {s = sf.opensesis (); transaction t = s.begintransaction (); query = s.createQuery ("from user as user where user .userid =: uid "); Q.SetParameter (" UID ", UID); Result = (user) q.list (). get (0); t.commit ();} catch (exception e) {e. PRINTSTACKTRACE ();} finally {try {s.close ();} catch (exception e) {}}}}}} public void insertdb (string uname, string pass, string address {try {s = sf.opensession ); Transaction t = s.begintransaction (); user u = new user (); u.setUserid (Integer.Decode ("1")); u.setpassword (pass); u.setusername (uname); u.setaddress (Address); S.Save (U); T.Commit ();} catch (exception e) {E.PrintStackTrace ();} finally {try {s.close ();} catCH (Exception E) {e. PRINTSTACKTRACE ();}}}} runs to the database qu ERY, you can see the database has been added: http://www.cjsdn.com/post/view? BID = 2 & id = 41689 & tpg = 1 & ppg = 1 & sty = 1 & agn = 0 # 41689

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

New Post(0)