SSH-SSH Integration (struts2+sprinig+hibernate)

Source: Internet
Author: User

Before that we have already explained the SSM and SSM2 integration development, today we do SSH integration, before there is an integrated SSH article, which is based on the development of annotations, today explained for the configuration file-based injection method for development.
Idea: Spring manages the creation of Hibernate-related session factories and the transactions that manage hibernate, while the spring container manages the implementation of the service layer and the Struts2 action, so to speak, we get to the point.
In the same way, we use a user login case to explain.
Development software:
Eclipse Neon
Tomcat7.0
Jdk1.7
Struts2.3
Spring2.5
Hiberbate3.0

The project structure is as follows:


Project source Download: Click to download
Online Demo: Click to watch
SSH development We are also divided into the standard three-tier development, first of all we carry out the model layer development.
Model
1, first we write PO object-user.java
[Java] View plain copy print? On code to view a snippet derived from my Code slice
Package com.sw.domain;
/*
* @Author Swxctx
* @time April 27, 2017
* @Explain: User Table Po Object
*id: Number
*username: User Name
*password: Password
*/
public class User {
private int id;
Private String username;
private String password;
public int getId () {
return ID;
}
public void setId (int id) {
This.id = ID;
}
Public String GetUserName () {
return username;
}
public void Setusername (String username) {
This.username = Username;
}
Public String GetPassword () {
return password;
}
public void SetPassword (String password) {
This.password = password;
}
}

2, write the corresponding hibernate mapping file-user.hbm.xml
[HTML] View plain copy print? On code to view a snippet derived from my Code slice
<?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" >
<class name= "Com.sw.domain.User" table= "User" >
<!--Configuring the primary key--
<id name= "id" type= "int" >
<!--column Definition--
<column name= "id" not-null= "true" sql-type= "int" ></column>
<!--generation Strategy--
<generator class= "UUID" ></generator>
</id>
<!--field Configuration--
<property name= "username" type= "string" >
<column name= "username" sql-type= "varchar" ></column>
</property>

<property name= "Password" type= "string" >
<column name= "password" sql-type= "varchar" ></column>
</property>
</class>

3. Next we need to write hibernate configuration file-hibernate.cfg.xml
[HTML] View plain copy print? On code to view a snippet derived from my Code slice
<?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" >
<session-factory>
<property name= "Hibernate.connection.username" >root</property>
<property name= "Hibernate.connection.password" >0707</property>
<property name= "Hibernate.connection.driver_class" >com.mysql.jdbc.Driver</property>
<property name= "Hibernate.connection.url" >jdbc:mysql://localhost:3306/bs</property>

<!--transactions are automatically submitted, this configuration is required with sessionfactory-
<property name= "Hibernate.connection.autocommit" >true</property>

<!--configuring dialects---
<property name= "Hibernate.dialect" >org.hibernate.dialect.MySQL5Dialect</property>
<!--operations Database form-
<property name= "Hibernate.hbm2ddl.auto" >update</property>
<!--SQL statements--
<property name= "Hibernate.show_sql" >true</property>


<!--map Files--
<mapping resource= "Com/sw/domain/user.hbm.xml"/>
</session-factory>

3, now the basic configuration has been completed, we can carry out the development of the DAO layer, writing Userdao.java file
[Java] View plain copy print? On code to view a snippet derived from my Code slice
Package Com.sw.dao;

Import Com.sw.domain.User;

/*
* @Author Swxctx
* @time May 10, 2017
* @Explain:D AO layer interface
*/
Public interface Userdao {
Public final static String service_name = "Userdaoimpl";

Login
Public User Finduserbyusername (String username);

}

4, next need to write implementation class-userdaoimpl.java
[Java] View plain copy print? On code to view a snippet derived from my Code slice
Package Com.sw.dao.impl;

Import Org.hibernate.Criteria;
Import org.hibernate.Session;
Import org.hibernate.criterion.Restrictions;
Import Org.springframework.orm.hibernate3.support.HibernateDaoSupport;

Import Com.sw.dao.UserDao;
Import Com.sw.domain.User;

/*
* @Author Swxctx
* @time May 10, 2017
* @Explain:D AO layer implementation
*/
public class Userdaoimpl extends Hibernatedaosupport implements Userdao {

@Override
Public User Finduserbyusername (String username) {
Session Session=null;
User User=new user ();
try {
Session=this.gethibernatetemplate (http://www.yiqianou.cn/). Getsessionfactory (). Opensession ();
Criteria Criteria= (criteria) Session.createcriteria (user.class);
Join condition Query
Criteria.add (Restrictions.eq ("username", www.22yigouyule.cn/username));
User= (User) Criteria.uniqueresult ();
} finally {
if (session!=null) {
Session.close ();
}
}
return user;
}

}

5. Next we need to manage the ssqlsessionfactoty through spring, and we need to write the Applicatiobcontext.xml file by spring to manage the DAO layer method.
[HTML] View plain copy print? On code to view a snippet derived from my Code slice
<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
xmlns:context= "Http://www.springframework.org/schema/context"
xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
xmlns:tx= "Http://www.springframework.org/schema/tx"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-2.5.xsd
Http://www.springframework.org/schema/tx
Http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
Http://www.springframework.org/schema/aop
Http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">

<!--configuration Annotations Auto Scan range--
<context:component-scan base-package= "COM.SW" ></context:component-scan>

<!--Create a sessionfactory factory--
<bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
<property name= "Configlocation" >
<value>
Classpath:/hibernate/hibernate.cfg.xml
</value>
</property>
</bean>

<!--Configure the DAO layer (inject sessionfactory)--
<bean id= "Userdaoimpl" class= "Com.sw.dao.impl.UserDaoImpl" >
<!--injected Sessionfactory--www.xyseo.net>
<property name= "Sessionfactory" ref= "Sessionfactory" ></property>
</bean>

</beans>
6, next we need to write the spring configuration file load of the tool class, in the previous SSM and SSM2 has been mentioned, this is not repeated here.
To load a configuration file class:
[Java] View plain copy print? On code to view a snippet derived from my Code slice
Package Com.sw.container;

Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;

/*
* @Author Swxctx
* @time May 15, 2017
* @Explain: Service class, load Applicationcontext.xml file with user
*/
public class Serviceprovidecord {

protected static ApplicationContext ApplicationContext;

public static void Load (string[www.feifanyule.cn] fileName) {
ApplicationContext = new Classpathxmlapplicationcontext (fileName);
}
}

Get the class for the Bean service:
[Java] View plain copy print? On code to view a snippet derived from my Code slice
Package Com.sw.container;

Import Org.apache.commons.lang.StringUtils;

/*
* @Author Swxctx
* @time May 15, 2017
* @Explain: Service Layer Services Class
*/
@SuppressWarnings ("Static-access")
public class Swserviceprovider {
private static Serviceprovidecord Serviceprovidecord;

Static loading
static{
Serviceprovidecord = new Serviceprovidecord ();
Serviceprovidecord.load (New string[]{"Classpath:/spring/applicationcontext-service.xml",
"Classpath:/spring/applicationcontext-dao.xml",
"Classpath:/spring/applicationcontext-transaction.xml",
"Classpath:/spring/applicationcontext-action.xml"});
}

public static Object GetService (String serviceName) {
Service name is empty
if (Stringutils.isblank (ServiceName)) {
throw new RuntimeException ("The current service name does not exist ...");
}
Object object = null;
if (ServiceProvideCord.applicationContext.containsBean (ServiceName)) {
Get Bean
Object = ServiceProvideCord.applicationContext.getBean (ServiceName);
}
if (object==null) {
throw new RuntimeException ("Service node under service name" "+servicename+" does not exist ... ");
}
return object;
}
}
7, as above, we have completed the writing, can be tested.
[Java] View plain copy print? On code to view a snippet derived from my Code slice
Package com.sw.test;


Import Org.junit.Test;

Import Com.sw.container.SwServiceProvider;
Import Com.sw.dao.UserDao;
Import Com.sw.domain.User;

/*
* @Author Swxctx
* @time May 18, 2017
* @Explain: Hibernate (027yeshenghuowang.com dao) test
*/
public class Daotest {


@Test
public void Testfinduserbyname (www.yigouyule2019.cn) {
Userdao userdao= (Userdao) Swserviceprovider.getservice (userdao.service_name);
User user= (user) Userdao.finduserbyusername ("BS");
if (user!=null) {
String String=user.getpassword ();
System.out.println (string);
}else{
System.out.println ("www.yuheng119.com/");
}
}

}

The test results are as follows:

As shown above, it indicates that the development of the model layer is correct and that the development of the service layer is noted.

SSH-SSH Integration (struts2+sprinig+hibernate)

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.