最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 正文

SSH框架整合详解(精典)

来源:动视网 责编:小OO 时间:2025-09-29 13:24:26
文档

SSH框架整合详解(精典)

已经经过本人测试第一步:准备工作MyEclipse6.0------Tomcat6.0所需jar文件:(所有jar文件已整理ssh_jars文件夹)注意:ssh_test项目中采用的是MySql数据库。而本文档中配置的是Oracle数据库,注意其中配置的不同之处哦(在相应的地方有注明)第二步:(建立项目,包结构,导入所需jar文件)新建一个webproject:建立包结构如下:各包功能说明:com.ssh.config:配置文件(*.properties),如数据库配置信息com.ssh.re
推荐度:
导读已经经过本人测试第一步:准备工作MyEclipse6.0------Tomcat6.0所需jar文件:(所有jar文件已整理ssh_jars文件夹)注意:ssh_test项目中采用的是MySql数据库。而本文档中配置的是Oracle数据库,注意其中配置的不同之处哦(在相应的地方有注明)第二步:(建立项目,包结构,导入所需jar文件)新建一个webproject:建立包结构如下:各包功能说明:com.ssh.config:配置文件(*.properties),如数据库配置信息com.ssh.re
已经经过本人测试

第一步:准备工作

    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 项目

                                            

文档

SSH框架整合详解(精典)

已经经过本人测试第一步:准备工作MyEclipse6.0------Tomcat6.0所需jar文件:(所有jar文件已整理ssh_jars文件夹)注意:ssh_test项目中采用的是MySql数据库。而本文档中配置的是Oracle数据库,注意其中配置的不同之处哦(在相应的地方有注明)第二步:(建立项目,包结构,导入所需jar文件)新建一个webproject:建立包结构如下:各包功能说明:com.ssh.config:配置文件(*.properties),如数据库配置信息com.ssh.re
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top