Use struts2.0 + spring2.5 + Hibernate framework in netbeans (1)

Source: Internet
Author: User
Tags netbeans

Netbeans 6.1AddedSpring 2.5,Struts,HibernateTo meet the needs of most enterprise applications.SSHFramework development requirements. This document uses a simple login example to describe howNetbeans6.1Used inSSHFramework for enterprise-level development. This section describes the sample preparation, or ing, and related configuration methods. The next section describes the key technologies of page layer and framework integration.

Software requirements:

1, 1, jdk1.6
2. netbeans 6.1 , download and install struts 2.0 plug-in , hibernate 3.5 plug-ins supporting and library files
3. MySQL 5 database and JDBC driver
4. STRUTS + spring the JDBC driver and integration library of the integration solution library are here .

Tutorial steps:

1. 1, In MySQL Create a database Sshdemo , Execute the following script:
Set foreign_key_checks = 0;
------------------------------
-- Table structure for user
------------------------------
Create Table 'user '(
 'Userid' bigint (20) not null auto_increment,
 'Username' varchar (20) not null,
 'Password' varchar (30) not null,
 Primary Key ('Userid ')
) Engine = InnoDB default charset = utf8;

------------------------------
-- Records
------------------------------
Insert into 'user' values ('1', 'mg ', '123 ');

2, 2,InNetbeans6.1CreateWebProject, select when selecting the frameworkSpring web mvc2.5AndStruts2.0AndHibernate, NamedSshdemo, As shown in:

 

Click the project right-click the project-repository jarfile and add the two packages in the ssh_lib.zip package.

3, 3, Create a new package in the source package Po , Dao , service , action four packages, ORM ing, database operations, business logic, and struts action .

4, 4,CreateModel, InPoCreate a class in the packageUser,CodeAs follows:

Public class user {
Private string name;
Private long ID;
Private string password;
}

Right-click and select "refactor"-"Encapsulate fields", select all fields for encapsulation, and generateGetAndSetMethod.

5, 5,CreateModelIng with the database. GeneratePoPackage, new-Others-Hibernate-Hibernate configuration file, As shown in:

 

Click Next and name itHibernate1.cfg,Click Next and goDatabase ConnectionTo the bottom of the drop-down box and click "Create Database Connection", as shown in:

 

Note that you can specify the user name and password. Click Finish to generate the default package in the source package.Hibernate1.cfg. xmlOfHibernateConfiguration file:

<? 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.dtd">
<Hibernate-configuration>
 <Session-factory name = "session1">
<Property name = "hibernate. dialect"> org. hibernate. dialect. mysqldialect </property>
<Property name = "hibernate. Connection. driver_class"> com. MySQL. JDBC. Driver </property>
<Property name = "hibernate. Connection. url"> JDBC: mysql: // localhost: 3306/sshdemo </property>
<Property name = "hibernate. Connection. username"> root </property>
<Property name = "hibernate. Connection. Password"> root </property>
 </Session-factory>
</Hibernate-configuration>

InPoPackage, right-click, and select new-Others-Hibernate mapping file,Name itUser. HBM, As shown in the following Configuration:

 

Click Finish to generateUser. HBM. xml, Add the ing relationship as follows:

<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype hibernate-mapping public "-// hibernate/hibernate mapping DTD 3.0 // en" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<Hibernate-mapping>
<Class dynamic-insert = "false" dynamic-update = "false" mutable = "true" name = "po. user "optimistic-lock =" version "polymorphism =" implicit "select-before-update =" false "table =" user ">
<ID column = "userid" name = "ID">
<Generator class = "Identity"/>
</ID>
<Property column = "username" name = "name" type = "string"/>
<Property column = "admin" name = "admin" type = "integer"/>
<Property column = "password" name = "password" type = "string"/>
</Class>
</Hibernate-mapping>

6, 6,InSpring IoCContainer DefinitionHibernateOfSessionfactory. OpenWebPage-WEB-INF-applicationcontext. XML,First, add the data source definition:

<Bean id = "datasource" class = "org. springframework. JDBC. datasource. drivermanagerdatasource">
<! -- SpecifyDriver-->
<Property name = "driverclassname" value = "com. MySQL. JDBC. Driver"/>
<! -- SpecifyURL -->
<Property name = "url" value = "JDBC: mysql: // localhost: 3306/sshdemo"/>
<! -- User name for database connection-->
<Property name = "username" value = "root"/>
<! -- Password used to connect to the database-->
<Property name = "password" value = "root"/>
</Bean>

Then join Hibernate Of Sessionfactory Definition:
<Bean id = "sessionfactory" class = "org. springframework. Orm. hibernate3.localsessionfactorybean">
<Property name = "datasource" ref = "datasource"/>
<Property name = "mappingresources">
<List>
<Value> po/user. HBM. xml </value>
</List>
</Property>
<Property name = "hibernateproperties">
<Value> hibernate. dialect = org. hibernate. dialect. mysqldialect </value>
  </Property>
</Bean>

SpringThe configuration file has the following powerful prompt functions:

7. 7,CreateDaoInterface and implementation. InDaoInterfaces created in the packageUserdao,The Code is as follows:

Public interface userdao {
Void save (User user) throws exception;
User getuser (long userid );
User getuser (string username );
}

Create implementation classUserdaoimpThe Code is as follows:

Package Dao;
Import org. springframework. Orm. hibernate3.support. hibernatedaosupport;
Import Po. user;

/**
 *
 * @ Author Administrator
 */

Public class userdaoimpl extends hibernatedaosupport implements userdao {
Public void save (User user) throws exception
{
Try
 {
Gethibernatetemplate (). Save (User );
}
Catch (exception ERR)
{
Throw err;
}

}

public user getuser (long userid)
{
return (User) gethibernatetemplate (). get (user. class, userid);
}

Public user getuser (string username ){
Return (User) gethibernatetemplate (). Find ("from user as u where u. Name =? ", Username). Get (0 );
}

}

This classInherited from Org. springframework. Orm. hibernate3.support. hibernatedaosupport To facilitate use Gethibernatetemplate () Obtain Hibernate Yes. For example Getuser Method, you can use Gethibernatetemplate () Method To obtain factory objects Sessionfactory , Call Find You can use Hql Statement to search for objects without having to write complicated SQL Statement. You only need to call the insert operation for other operations on the object. Save Method. Because Dao Class must be called Sessionfactory Therefore Spring IoC Container is Dao Class Injection Sessionfactory . In Applicationcontext. xml Add the following Configuration:






now, or ing is complete. The next section describes the page layer technology, including the internationalization solution.

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.