Struts + hibernate Template Development Notes (1)

Source: Internet
Author: User

This article describes the entire process of using the Struts + hibernate development template. This article has the following content:

1. Create a database connection pool

2. Establish a Test Database

3. Create a hibernate Configuration

4. map database tables to Classes

5. Package definition in the template

Vi. Design Mode: Singleton

VII. Design Model: Facade

8. Design Mode: Dao

9. Design Mode: MVC

10. Custom tags

11. Solve Chinese character encoding Problems

Struts + hibernate Template Development notes-create a database connection pool

 

1. Merge the database driverTomcat5.0 in the common/lib directory.

For example, copy ojdbc14.jar from Oracle9i

2. Modify the tomcat configuration file CONF/server. xml. Add the following code before

<Context path="/demo" docBase="F:/j_work/working/demo/demo" debug="5" reloadable="true" crossContext="true"> <Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_DBTest_log." suffix=".txt" timestamp="true"/> <Resource name="jdbc/demo" auth="Container" type="javax.sql.DataSource"/> <ResourceParams name="jdbc/demo"> <parameter> <name>factory</name> <value>org.apache.commons.dbcp.BasicDataSourceFactory</value> </parameter> <parameter> <name>driverClassName</name> <value>oracle.jdbc.driver.OracleDriver</value> </parameter> <parameter> <name>url</name> <value>jdbc:oracle:thin:@192.168.3.200:1521:demo</value> </parameter> <parameter> <name>username</name> <value>demo</value> </parameter> <parameter> <name>password</name> <value>demo</value> </parameter> <parameter> <name>maxActive</name> <value>20</value> </parameter> <parameter> <name>maxIdle</name> <value>10</value> </parameter> <parameter> <name>maxWait</name> <value>-1</value> </parameter> </ResourceParams> </Context>
Struts + hibernate Template Development notes-create a Test Database
 
 

  

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.