struts2.3.4+hibernate4.2.4+mysql6.0 Integration

Source: Internet
Author: User
Tags generator

1. Project Construction Process:

(1). Create a Web Project. Import the jar packages for Struts2 and hibernate. (If you don't know the Struts2 jar package, you can find the sample program contained under the Apps folder in the download struts jar package, one of which is Struts2-blank.war, unzip, and copy the jar package under the Lib package.) The jar package required by the Hibernate program, from the Hibernate jar, has a folder called Lib\required, which is the required package.

(2). Configure the filter for struts in Web. Xml.

<filter>       <filter-name>struts2</filter-name>    
<filter-class>org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter- class></filter><filter-mapping> <filter-name>struts2</filter-name> <url-pattern></url-pattern></filter-mapping>

(3). Create hibernate core configuration file under src path hibernate.cfg.xml

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-Configuration Public"-//hibernate/hibernate Configuration DTD 3.0//en" "Http://hibernate.sourceforge.net/hibernate-configuration-3.0.d TD "><!--user name and password to connect to the database--<property name= "Hibernate.connection.username" >root</property> <p Roperty name= "Hibernate.connection.password" >password1!</property>
<!--MySQL database driver-<property name= "Hibernate.connection.driver_class" >com.mysql.jdbc.driver</ property> <!--URL, and set the encoding format, where &amp; is the escape of & in HTML--<property name= "hibernate.connection . url ">jdbc:mysql://localhost:3306/Database name?useunicode=true&amp;characterencoding=utf-8</property>
<!--set the dialect to MySQL--<property name= "Hibernate.dialect" >org.hibernate.dialect.MySQLDialect</property>
< print SQL statements--<property name= ' show_sql ' > When you perform an action!--true</property>
<!--formatted output SQL statement--<property name= "Format_sql" >true</property>
<!--table Generation strategy--<property name= "Hbm2ddl.auto" >update</property>
<!--use Getcurrentsession to get the session object, you need to add this sentence--<property name= "Hibernate.current_session_context_c Lass ">thread</property> </session-factory>

(4). Create a struts.xml configuration file, plus a shell. Prepare for subsequent configuration of action and result.

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.1//en" "http://struts.apache.org/dtds/ Struts-2.1.dtd "><struts>
<!--developer Mode open for easy commissioning-- <constant name= "Struts.devmode" value= "true" ></constant> < Packageextends= "Struts-default" ></struts>

(5). Create an entity class (JavaBean notation) according to your business needs. and create corresponding mapping files for these entity classes.

(entity class slightly)Student. Cfg.xml mapping file is as follows:

<?XML version= "1.0"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd "><!--Generated 2016-7-23 10:35:39 by Hibernate Tools 3.4.0.CR1 -<hibernate-mapping>    <!--the Name property value of class must be referenced starting with the package name -    <classname= "Com.imooc.entity.Student"Table= "Student">        <IDtype= "Java.lang.String"name= "Sid"column= "SID"length= "8">
<!--primary key growth strategy, assigned means adding a primary key in a custom way rather than automatically generated by the database - <Generatorclass= "Assigned"></Generator> </ID>

< Propertyname= "Sname"type= "Java.lang.String"> <columnname= "SNAME" /> </ Property> < Propertyname= "Sgender"type= "Java.lang.String"> <columnname= "Sgender" /> </ Property>    <!--type , you can write the Hibernate mapping type date (only displays date) time (only displayed) timestamp (month and day seconds), or write Java corresponding type: java.util.Date - < Propertyname= "Sbirthday"type= "Date"> <columnname= "Sbirthday" /> </ Property> < Propertyname= "Saddress"type= "Java.lang.String"> <columnname= "Saddress" /> </ Property> </class></hibernate-mapping>

Add the mapping file to hibernate's core configuration file: Hibernate.cfg.xml

< session-factory > ... <  resource= "Com/imooc/entity/student.hbm.xml"/><  Resource = "Com/imooc/entity/user.hbm.xml" /> ... </ session-factory >

(6). Based on business requirements, layer, create DAO interface, DAO implementation class. Before that, create a tool class that provides an instance of the Sessionfactory object. (Singleton mode)

 Public classSessionfactoryutil {Private Staticsessionfactory sessionfactory; //single-case mode    PrivateSessionfactoryutil () {} Public Staticsessionfactory getsessionfactory () {if(Sessionfactory = =NULL) {            //Get configuration informationConfiguration Configuration =NewConfiguration (). Configure (); //Get Service Registration ObjectServiceregistry Serviceregistry =NewServiceregistrybuilder (). Applysettings (Configuration.getproperties ()). Buildse            Rviceregistry (); Sessionfactory=configuration.buildsessionfactory (serviceregistry); }        returnsessionfactory; }}

In the specific DAO implementation class, when the entity class is added and removed, it is summarized as follows:

When the session object is being checked for additional deletions, the transaction is opened.

An object is obtained by Get/load method, and the operation of the save,delete,update is changed separately.

If we use the query, we use the HQL:HQL default syntax is: From entity class where property ...

If you want to query a specific column of content, the syntax is: Select Property 1, Property 2, Property 3 ... from entity class where ..., the default return type is List<object[]>

Session session =sessionfactoryutil.getsessionfactory (). getcurrentsession (); Transaction=session.begintransaction ();//the premise of this approach is that the constructor that contains the option column must exist in the entity class student toString hql = "Select New Student (SID, Sname, Sgender, Sbirthday, saddress) from Student"; List<Student> s =query.list ();//String hql = "Select New List (SID, Sname, Sgender, Sbirthday, saddress) from Student";//list<list> lists = Query.list ();//String hql = "Select New Map (SID, Sname, Sgender, Sbirthday, saddress) from Student";//list<map> maps= query.list ();//default//String hql = "Select Sid, Sname, Sgender, Sbirthday, saddress from Student";//list<object[]> lists = Query.list ();

(7). Front desk, the JSP page jumps to the action.

<form name= "LoginForm" action= "<%=path%>/user/user_login.action" method= "POST" >
Explain what the user is behind <%=path%>. What is User_login again?
First, the user represents namespace. The following User_login matches the action with a wildcard character

<package name= "Student" namespace= "/student" extends= "Struts-default" >
<action name= "*_*" class= "com.imooc.action. {1} Action "method=" {2} ">
<result name= "List_success" >/student/Students_query_success.jsp</result>
<result name= "List_failer" >/student/Students_query_success.jsp</result>
<result name= "Add_success" >/student/Students_add_success.jsp</result>
<result name= "Modify_middle" >/student/Students_modify.jsp</result>
<result name= "Save_success" >/student/Students_modify_success.jsp</result>
<result name= "Delete_success" type= "chain" >Student_query</result><!--note the path. The type in chain represents the page to jump to another request. You can't jump to a page--
</action>
</package>
To say here, jump to the other action,type= "chain" in result. If you jump to another JSP page by result, the default type= "dispatcher"

For the moment, you are welcome to correct me!

struts2.3.4+hibernate4.2.4+mysql6.0 Integration

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.