1. 程式人生 > >ssh-ssh整合(Struts2+Sprinig+hibernate)

ssh-ssh整合(Struts2+Sprinig+hibernate)

ref www @override ces 相關 tor net pin ride

在之前呢我們已經講解過ssm以及ssm2的整合開發,今天我們進行ssh的整合,在之前已經有一篇整合ssh的文章,那是基於註解開發的,今天講解的為基於配置文件註入方式進行開發。
思路:spring管理hibernate相關會話工廠的創建以及負責管理hibernate的事務,同時spring容器管理service層的實現以及struts2的action,話不多說,我們進入正題。
同樣的,我們以一個用戶登錄的案例進行講解。
開發軟件:
Eclipse neon
Tomcat7.0
Jdk1.7
Struts2.3
Spring2.5
Hiberbate3.0

項目結構如下所示:


項目源碼下載:點擊下載
在線演示:點擊觀看
ssh的開發我們同樣的分為標準的三層進行開發,首先我們進行Model層的開發。
Model
1、首先我們編寫po對象-User.Java
[java] view plain copy print?在CODE上查看代碼片派生到我的代碼片
package com.sw.domain;
/*
[email protected]

/* */ swxctx
[email protected] 2017年4月27日
[email protected]:用戶表po對象
*id:編號
*username:用戶名
*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、編寫對應的hibernate映射文件-User.hbm.xml
[html] view plain copy print?在CODE上查看代碼片派生到我的代碼片
<?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 name="com.sw.domain.User" table="user">
<!-- 配置主鍵 -->
<id name="id" type="int">
<!-- 列定義 -->
<column name="id" not-null="true" sql-type="int"></column>
<!-- 生成策略 -->
<generator class="uuid"></generator>
</id>
<!-- 字段配置 -->
<property name="username" type="string">
<column name="username" sql-type="varchar(50)"></column>
</property>

<property name="password" type="string">
<column name="password" sql-type="varchar(50)"></column>
</property>
</class>
</hibernate-mapping>

3、接下來我們需要編寫Hibernate配置文件-hibernate.cfg.xml
[html] view plain copy print?在CODE上查看代碼片派生到我的代碼片
<?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>
<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>

<!-- 事務自動提交,使用sessionFactory需要進行此項配置 -->
<property name="hibernate.connection.autocommit">true</property>

<!-- 配置方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 操作數據庫的形式 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- sql語句 -->
<property name="hibernate.show_sql">true</property>


<!-- 映射文件 -->
<mapping resource="com/sw/domain/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>

3、現在基本配置已經完成了,我們可以進行dao層的開發了,編寫UserDao.java文件
[java] view plain copy print?在CODE上查看代碼片派生到我的代碼片
package com.sw.dao;

import com.sw.domain.User;

/*
[email protected] swxctx
[email protected] 2017年5月10日
[email protected]:Dao層接口
*/
public interface UserDao {
public final static String SERVICE_NAME = "UserDaoImpl";

//登錄
public User findUserByUsername(String username);

}

4、接下來需要編寫實現類-UserDaoImpl.java
[java] view plain copy print?在CODE上查看代碼片派生到我的代碼片
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;

/*
[email protected] swxctx
[email protected] 2017年5月10日
[email protected]:Dao層實現
*/
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);
//加入條件查詢
criteria.add(Restrictions.eq("username",www.22yigouyule.cn/ username));
user=(User) criteria.uniqueResult();
} finally {
if(session!=null){
session.close();
}
}
return user;
}

}

5、接下來我們需要通過Spring來管理SsqlsessionFactoty,同時需要通過spring管理dao層的方法,編寫applicatiobContext.xml文件
[html] view plain copy print?在CODE上查看代碼片派生到我的代碼片
<?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">

<!-- 配置註解自動掃描範圍 -->
<context:component-scan base-package="com.sw"></context:component-scan>

<!-- 創建SessionFactory工廠 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>
classpath:/hibernate/hibernate.cfg.xml
</value>
</property>
</bean>

<!-- 配置dao層(註入sessionFactory) -->
<bean id="UserDaoImpl" class="com.sw.dao.impl.UserDaoImpl">
<!-- 註入sessionFactory --www.xyseo.net>
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

</beans>
6、接下來我們需要編寫spring配置文件加載的工具類,在之前的ssm以及ssm2中已經講到,這裏不再重復。
加載配置文件類:
[java] view plain copy print?在CODE上查看代碼片派生到我的代碼片
package com.sw.container;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/*
[email protected] swxctx
[email protected] 2017年5月15日
[email protected]:服務類,用用戶加載applicationContext.xml文件
*/
public class ServiceProvideCord {

protected static ApplicationContext applicationContext;

public static void load(String[www.feifanyule.cn] fileName){
applicationContext = new ClassPathXmlApplicationContext(fileName);
}
}

獲取bean服務的類:
[java] view plain copy print?在CODE上查看代碼片派生到我的代碼片
package com.sw.container;

import org.apache.commons.lang.StringUtils;

/*
[email protected] swxctx
[email protected] 2017年5月15日
[email protected]:Service層服務類
*/
@SuppressWarnings("static-access")
public class SwServiceProvider {
private static ServiceProvideCord serviceProvideCord;

//靜態加載
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){
//服務名稱為空
if(StringUtils.isBlank(serviceName)){
throw new RuntimeException("當前服務名稱不存在...");
}
Object object = null;
if(serviceProvideCord.applicationContext.containsBean(serviceName)){
//獲取bean
object = serviceProvideCord.applicationContext.getBean(serviceName);
}
if(object==null){
throw new RuntimeException("服務名稱為【"+serviceName+"】下的服務節點不存在...");
}
return object;
}
}
7、如上,我們已經完成了編寫,可以進行測試了。
[java] view plain copy print?在CODE上查看代碼片派生到我的代碼片
package com.sw.test;


import org.junit.Test;

import com.sw.container.SwServiceProvider;
import com.sw.dao.UserDao;
import com.sw.domain.User;

/*
[email protected] swxctx
[email protected] 2017年5月18日
[email protected]:hibernate(027yeshenghuowang.com dao)測試
*/
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/");
}
}

}

測試結果如下:

如上所示,表明model層的開發已經無誤;記下來進行service層的開發。

ssh-ssh整合(Struts2+Sprinig+hibernate)