1. Create a database user name demo password demo 2. Create a test table Create a test table demo Field description ID char (100) Primary Key name varchar2 (50) Name: image_id number (10) Image ID system_date date System date user_date date Input date Create an image index table Image Field description ID char (100) Primary Key title varchar2 (50) Image title message varchar2 (2000) Image Information original_image_path varchar (200) Original Image Storage path small _ image_path varchar (200) Image Storage path image_id number (10) Image ID system_date date System date Id index table singleton_id Field description ID char (100) Primary Key name varchar2 (50) Index name (index required for this field) value number (10) Index ID Struts + hibernate Template Development notes --- create hibernate Configuration 1. Copy the Oracle9i database driver ojdbc14.jar to the lib directory of the jbuilder9 installation directory. Use Jbuilder9.0 to create a project. 2. Include the following library files required by hibernate-2.0.3 in the project. hibernate2.jar commons-beanutils.jar commons-collections.jar commons-dbcp.jar commons-lang.jar commons-logging.jar commons-pool.jar dom4j.jar cglib-asm.jar connector.jar 3. Create the hibernate. cfg. xml file and copy it to the src directory. <?xml version='1.0' encoding='utf-8'?> Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> <HIBERNATE-CONFIGURATION> <SESSION-FACTORY> <property name="connection.datasource">java:comp/env/jdbc/demo</property> <property name="show_sql">true</property> <property name="use_outer_join">true</property> <property name="dialect">net.sf.hibernate.dialect.Oracle9Dialect</property>
</SESSION-FACTORY> </HIBERNATE-CONFIGURATION>
Struts + hibernate Template Development notes --- map database tables to Classes Download the hibernate-extensions compressed package. Modify the environment variables in the setenv. BAT file. Modify the hibernate directory and the database driver file name. For example: @echo off rem -------------------------------------------------------------------rem Setup environment for hibernate toolsrem -------------------------------------------------------------------set JDBC_DRIVER=../../../lib/ojdbc14.jarset HIBERNATE_HOME=../../../hibernate-2.0.3set CORELIB=%HIBERNATE_HOME%/libset LIB=../libset PROPS=%HIBERNATE_HOME%/srcset CP=%JDBC_DRIVER%;%PROPS%;%HIBERNATE_HOME%/hibernate2.jar; %CORELIB%/commons-logging.jar;%CORELIB%/commons-collections.jar;%CORELIB%/commons-lang.jar;%CORELIB%/cglib.jar; %CORELIB%/dom4j.jar;%CORELIB%/odmg.jar;%CORELIB%/xml-apis.jar;%CORELIB%/xerces.jar;%CORELIB%/xalan.jar;%LIB%/jdom.jar;%LIB%/../hibernate-tools.jar Run ddl2hbm. BAT and set the parameters as follows: [ CONNECTION ] DRIVER CLASS : oracle.jdbc.driver.OracleDriver (org.gjt.mm.mysql.Driver)CONNECTION URL: jdbc:oracle:thin:@192.168.3.200:1521:demo (jdbc:mysql://localhost/authority)USER: demoPASSWORD: demo[ MAPPING ]KEY FIELD : idSCHEMAEXPORT : uuid.hex[ CODE ]PACKAGE NAME: com.company.demo.jdo[ OUTPUT ]F:/j_work/working/demo/src For others, use the default value. Click tables in [Tables]. Select the table, and the program will generate the class and XML The generated file is as follows: (1) image. Java package com.company.demo.jdo; import java.io.Serializable;import java.util.Date;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 Image implements Serializable {/** identifier field */private String id;/** nullable persistent field */private String title;/** nullable persistent field */private String message;/** nullable persistent field */private String originalImagePath;/** nullable persistent field */private String smallImagePath;/** nullable persistent field */private long imageId;/** nullable persistent field */private Date systemDate;/** full constructor */public Image(String title, String message, String originalImagePath, String smallImagePath, long imageId, Date systemDate) {this.title = title;this.message = message;this.originalImagePath = originalImagePath;this.smallImagePath = smallImagePath;this.imageId = imageId;this.systemDate = systemDate;}/** default constructor */public Image() {}public String getId() {return this.id;}public void setId(String id) {this.id = id;}public String getTitle() {return this.title;}public void setTitle(String title) {this.title = title;}public String getMessage() {return this.message;}public void setMessage(String message) {this.message = message;}public String getOriginalImagePath() {return this.originalImagePath;}public void setOriginalImagePath(String originalImagePath) {this.originalImagePath = originalImagePath;}public String getSmallImagePath() {return this.smallImagePath;}public void setSmallImagePath(String smallImagePath) {this.smallImagePath = smallImagePath;}public long getImageId() {return this.imageId;}public void setImageId(long imageId) {this.imageId = imageId;}public Date getSystemDate() {return this.systemDate;}public void setSystemDate(Date systemDate) {this.systemDate = systemDate;}public String toString() {return new ToStringBuilder(this).append("id", getId()).toString();}public boolean equals(Object other) {if ( !(other instanceof Image) ) return false;Image castOther = (Image) other;return new EqualsBuilder().append(this.getId(), castOther.getId()).isEquals();}public int hashCode() {return new HashCodeBuilder().append(getId()).toHashCode();}} (2) image. HBM. xml <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">type="string"/><property column="SMALL_IMAGE_PATH" length="200" name="smallImagePath" type="string"/><property column="IMAGE_ID" length="10" name="imageId" type="long"/><property column="SYSTEM_DATE" length="7" name="systemDate" type="timestamp"/></class>Other generated files are omitted! Struts + hibernate Template Development notes --- package definition in the template Com. Company. Demo. datamodule Data Model Com. Company. Demo. Dao Model Com. Company. Demo. Exception exception Com. Company. Demo. JDO hibernate class ing Class loaded when com. Company. Demo. Start starts the JSP Service Com. Company. Demo. tags custom label class Com. Company. Demo. Thread class Com. Company. Demo. util tool class Com. Company. Demo. Web struts class Class called by com. Company. Demo. Facade
2. Include the following library files required by hibernate-2.0.3 in the project. hibernate2.jar commons-beanutils.jar commons-collections.jar commons-dbcp.jar commons-lang.jar commons-logging.jar commons-pool.jar dom4j.jar cglib-asm.jar connector.jar 3. Create the hibernate. cfg. xml file and copy it to the src directory. <?xml version='1.0' encoding='utf-8'?> Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> <HIBERNATE-CONFIGURATION> <SESSION-FACTORY> <property name="connection.datasource">java:comp/env/jdbc/demo</property> <property name="show_sql">true</property> <property name="use_outer_join">true</property> <property name="dialect">net.sf.hibernate.dialect.Oracle9Dialect</property>
</SESSION-FACTORY> </HIBERNATE-CONFIGURATION>
Struts + hibernate Template Development notes --- map database tables to Classes Download the hibernate-extensions compressed package. Modify the environment variables in the setenv. BAT file. Modify the hibernate directory and the database driver file name. For example: @echo off rem -------------------------------------------------------------------rem Setup environment for hibernate toolsrem -------------------------------------------------------------------set JDBC_DRIVER=../../../lib/ojdbc14.jarset HIBERNATE_HOME=../../../hibernate-2.0.3set CORELIB=%HIBERNATE_HOME%/libset LIB=../libset PROPS=%HIBERNATE_HOME%/srcset CP=%JDBC_DRIVER%;%PROPS%;%HIBERNATE_HOME%/hibernate2.jar; %CORELIB%/commons-logging.jar;%CORELIB%/commons-collections.jar;%CORELIB%/commons-lang.jar;%CORELIB%/cglib.jar; %CORELIB%/dom4j.jar;%CORELIB%/odmg.jar;%CORELIB%/xml-apis.jar;%CORELIB%/xerces.jar;%CORELIB%/xalan.jar;%LIB%/jdom.jar;%LIB%/../hibernate-tools.jar Run ddl2hbm. BAT and set the parameters as follows: [ CONNECTION ] DRIVER CLASS : oracle.jdbc.driver.OracleDriver (org.gjt.mm.mysql.Driver)CONNECTION URL: jdbc:oracle:thin:@192.168.3.200:1521:demo (jdbc:mysql://localhost/authority)USER: demoPASSWORD: demo[ MAPPING ]KEY FIELD : idSCHEMAEXPORT : uuid.hex[ CODE ]PACKAGE NAME: com.company.demo.jdo[ OUTPUT ]F:/j_work/working/demo/src For others, use the default value. Click tables in [Tables]. Select the table, and the program will generate the class and XML The generated file is as follows: (1) image. Java package com.company.demo.jdo; import java.io.Serializable;import java.util.Date;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 Image implements Serializable {/** identifier field */private String id;/** nullable persistent field */private String title;/** nullable persistent field */private String message;/** nullable persistent field */private String originalImagePath;/** nullable persistent field */private String smallImagePath;/** nullable persistent field */private long imageId;/** nullable persistent field */private Date systemDate;/** full constructor */public Image(String title, String message, String originalImagePath, String smallImagePath, long imageId, Date systemDate) {this.title = title;this.message = message;this.originalImagePath = originalImagePath;this.smallImagePath = smallImagePath;this.imageId = imageId;this.systemDate = systemDate;}/** default constructor */public Image() {}public String getId() {return this.id;}public void setId(String id) {this.id = id;}public String getTitle() {return this.title;}public void setTitle(String title) {this.title = title;}public String getMessage() {return this.message;}public void setMessage(String message) {this.message = message;}public String getOriginalImagePath() {return this.originalImagePath;}public void setOriginalImagePath(String originalImagePath) {this.originalImagePath = originalImagePath;}public String getSmallImagePath() {return this.smallImagePath;}public void setSmallImagePath(String smallImagePath) {this.smallImagePath = smallImagePath;}public long getImageId() {return this.imageId;}public void setImageId(long imageId) {this.imageId = imageId;}public Date getSystemDate() {return this.systemDate;}public void setSystemDate(Date systemDate) {this.systemDate = systemDate;}public String toString() {return new ToStringBuilder(this).append("id", getId()).toString();}public boolean equals(Object other) {if ( !(other instanceof Image) ) return false;Image castOther = (Image) other;return new EqualsBuilder().append(this.getId(), castOther.getId()).isEquals();}public int hashCode() {return new HashCodeBuilder().append(getId()).toHashCode();}} (2) image. HBM. xml <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">type="string"/><property column="SMALL_IMAGE_PATH" length="200" name="smallImagePath" type="string"/><property column="IMAGE_ID" length="10" name="imageId" type="long"/><property column="SYSTEM_DATE" length="7" name="systemDate" type="timestamp"/></class>Other generated files are omitted! Struts + hibernate Template Development notes --- package definition in the template Com. Company. Demo. datamodule Data Model Com. Company. Demo. Dao Model Com. Company. Demo. Exception exception Com. Company. Demo. JDO hibernate class ing Class loaded when com. Company. Demo. Start starts the JSP Service Com. Company. Demo. tags custom label class Com. Company. Demo. Thread class Com. Company. Demo. util tool class Com. Company. Demo. Web struts class Class called by com. Company. Demo. Facade
|