视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
SSH框架整合详解(精典)
2025-09-29 13:24:26 责编:小OO
文档
已经经过本人测试

第一步:准备工作

    MyEclipse 6.0------Tomcat6.0

    所需jar文件:(所有jar文件已整理ssh_jars文件夹)注意:ssh_test项目中采用的是MySql 数据库。

而本文档中配置的是Oracle数据库,注意其中配置的不同之处哦(在相应的地方有注明)

第二步:(建立项目,包结构,导入所需jar文件)

新建一个web project:

    

建立包结构如下:

    

各包功能说明:

    com.ssh.config : 配置文件(*.properties),如数据库配置信息

    com.ssh.resource :  资源文件 

    com.ssh.util.comm :  公共工具(util)包

    com.ssh.util.orm.hibernate :  hibernate DAO 模板

    com.ssh.model : 各实体类文件

    com.ssh.service : 业务逻辑包  相当于biz 

     com.ssh..test : 测试包 

    com.ssh.action : struts Action 

      包结构可以根据自己需要自行划分

     导入jar文件: 

            新建一个User Library: sshJars

        

    添加jar文件:

         

将新建的User Library: sshJars加入到项目中:

第三步:撰写资源文件

    jdbc.properties

    在jdbc.properties 里写上连接数据库的配置信息,此文件的配置信息用于在spring的配置文件中使用,数据库的配置信息也可以写到Spring 的配置文件中,这在一个jdbc.properties文件中是为了方便更改.

    在com.ssh.resource包中新建下列文件:

        

各文件详细内容及说明如下:

-----------------------------------------------------------------------------------------------------------------------------------------

applicationContext.xml:

    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:context="http://www.springframework.org/schema/context"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"

    default-lazy-init="true">

    Spring公共配置文件

    

        

    

        

            

                classpath*:com/ssh/config/jdbc.properties

            

        

    

    

    

        

        

        

        

    

    

    

    

    

        

        

            

                org.hibernate.dialect.Oracle9Dialect

                true

                true

                org.hibernate.cache.EhCacheProvider

                /ehcache-hibernate.xml

            

        

        

            

                com.ssh.model.User具体配置文件名(*.hbm.xml)-->

            

        

    

    

    

        

    

    

    

    

    

    

-----------------------------------------------------------------------------------------------------------------------------------------

applicationContext-security.xml:

    xmlns:beans="http://www.springframework.org/schema/beans" 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/security http://www.springframework.org/schema/security/spring-security-2.0.xsd"

    default-autowire="byType" default-lazy-init="true">

此文件用于AOP的安全验证,也可不添加

-----------------------------------------------------------------------------------------------------------------------------------------

applicationContext-service.xml

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"

    default-autowire="byType" default-lazy-init="true">

    

    

Service(业务逻辑类)的注入

-----------------------------------------------------------------------------------------------------------------------------------------

ehcache-hibernate.xml:

    

    

    

    

    

Hibernate 缓存的配置(可以不做配置,如果需要才使用缓存才做配置,建议配置上)

-----------------------------------------------------------------------------------------------------------------------------------------

第四步:在com.ssh.util.comm 包下添加下列文件

    

    此包为hibernate DAO模板中用到的各种工具类,各文件可以在打包的ssh_test项目中找到.

第五步:添加hibernate DAO 模板

此包为hibernate DAO模板,SimpleHibernateTemplate.java 中包含了对各实体(增,删,改,查,分页查等)的泛形方法。各文件可以在打包的ssh_test项目中找到.。

第六步:以上的工作还不够,我们还需要进行WEB方面的配置

    

配置web.xml , struts-config.xml , action-servlet.xml

    各文件内容如下:

-----------------------------------------------------------------------------------------------------------------------------------------

    xmlns="http://java.sun.com/xml/ns/javaee"

    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

    id="WebApp_ID" version="2.5">

    

    

        

            org.springframework.web.context.ContextLoaderListener

        

    

    

        contextConfigLocation

        

            classpath:com/ssh/resource/applicationContext*.xml

        

    

    

    

    

        action

        

            org.apache.struts.action.ActionServlet

        

        

            config

            /WEB-INF/struts-config.xml

        

        1

    

    

        action

        *.do

    

    

    

        20

    

    

    

        index.jsp

    

-----------------------------------------------------------------------------------------------------------------------------------------

struts-config.xml: 

          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"

          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

    

    

                    type="org.apache.struts.action.DynaActionForm">

            

        

    

    

        

                    name="loginForm">

            

        

    

    

                    value="org.springframework.web.struts.DelegatingRequestProcessor" />

    

            className="org.springframework.web.struts.ContextLoaderPlugIn" />

-----------------------------------------------------------------------------------------------------------------------------------------

action-servlet.xml: 注意:此文件struts 1.x 必须配置,而struts 2.x 不需要配置

    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:context="http://www.springframework.org/schema/context"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"

    default-autowire="byName" default-lazy-init="true">

-----------------------------------------------------------------------------------------------------------------------------------------

至此,我们的Spring+Struts+Hibernate集成工作完成!

下面我们做测试:

com.ssh.model  包下建立 User.java

package com.ssh.model;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.Table;

import javax.persistence.Entity;

@Entity

@Table(name = "user")

public class User {

    

    private Integer id;

    private String username;

    private String password;

    

    

    public User() {

        super();

    }

    

    @Id

    @GeneratedValue(strategy=GenerationType.IDENTITY)

    public Integer getId() {

        return id;

    }

    public void setId(Integer 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;

    }

}

com.ssh.service 包下建立 UserService.java

package com.ssh.service;

import java.util.List;

import com.ssh.model.User;

import com.ssh.util.orm.hibernate.Page;

import com.ssh.util.orm.hibernate.SimpleHibernateTemplate;

import org.hibernate.SessionFactory;

import org.springframework.beans.factory.annotation.Required;

import org.springframework.transaction.annotation.Transactional;

import org.springframework.util.StringUtils;

/*

 *@author onlyflag

 */

@Transactional

public class UserService {

    private SimpleHibernateTemplate userDao;

    @Required

    public void setSessionFactory(SessionFactory sessionFactory)

    {

        userDao = new SimpleHibernateTemplate(sessionFactory,User.class);

    }

    @Transactional(readOnly=true)

    public User getUser(Long id)

    {

        return userDao.get(id);

    }

    @Transactional(readOnly=true)

    public Page getAllUser(Page page)

    {

        return userDao.findAll(page);

    }

    @Transactional(readOnly=true)

    public User getUserByLoginName(String loginName)

    {

        return userDao.findUniqueByProperty("loginName",loginName);

    }

    

    public void saveUser(User user)

    {

        userDao.save(user);

    }

    

    public void deleteUser(Long id)

    {

        User user = userDao.get(id);

        userDao.delete(user);

    }

    

    public List findByProperty(String propertyName,String value)

    {

        return userDao.findByProperty(propertyName, value);

    }

    public User findUniqueByProperty(String propertyName,String value)

    {

        return userDao.findUniqueByProperty(propertyName, value);

    }

    

    @Transactional

    public boolean isLoginNameUnique(String loginName,String orgLoginName)

    {

        return userDao.isUnique("loginName",loginName, orgLoginName);

    }

    public SimpleHibernateTemplate getUserDao() {

        return userDao;

    }

}       

有了:User  和 UserService 我们就可写一个测试类来进行User 类的各种操作:

    com.ssh.test  下建立 Test.java

package com.ssh.test;

import com.ssh.model.User;

import com.ssh.service.UserService;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

    public static void main(String[] args){

        ApplicationContext ctx=new ClassPathXmlApplicationContext("com.ssh /resource/applicationContext*.xml");

        UserService us=(UserService) ctx.getBean("userService");

        User user=new User();

        user.setPassword("123");

        user.setUsername("onlyflag");

        us.saveUser(user);

    }

}

运行Test.java  我们将成功的在数据库里插入一条数据

关于WEB里的应用例子, 请看ssh_jar 项目

                                            下载本文

显示全文
专题