SSH整合

9 篇文章 0 订阅
8 篇文章 0 订阅

上次分别讲解了一下strut2和spring的整合,spring和hibernate的整

合,接下来就是struts2,spring和hibernate三大框架的整合了。

现在SSH框架用的人还是比较多,但springmvc 和springboot框架使用力度也比较大了,等有时间了,我会更新一些关于springmvc的使用教程。

SSH整合的步骤:

一:加入jar包

这里写图片描述

这里写图片描述

二:编写struts2的配置文件:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
    <struts>
        <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
        <package name="default" extends="struts-default">
            <action name="emp-*" class="employaction" method="{1}">
            <result name="employerlist">employ_list.jsp</result>

            </action>
        </package>

    </struts>

spring配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <!-- 导入配置文件 -->
    <context:property-placeholder location="classpath:db.properties"/>
    <!-- 配置连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
    </bean>

    <!-- 配置sessionfactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        <property name="mappingLocations" value="classpath:com/ssh/en/*.hbm.xml"></property>

    </bean>

    <!-- 配置spring申明式事务 -->
    <!-- 配置hibernate的事务管理器 -->
    <bean id="TransactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!-- 配置事务属性 -->
    <tx:advice id="txAdvice" transaction-manager="TransactionManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

    <!-- 配置事务切入点 -->
    <aop:config>
        <aop:pointcut expression="execution(* com.ssh.service.*.*(..))" id="txPointcut"/>

        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>

    </aop:config>

    <bean id="employdao" class="com.ssh.dao.Employerdao">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <bean id="employservice" class="com.ssh.service.Employservice">
        <property name="employdao" ref="employdao"></property>
    </bean>

    <bean id="employaction" class="com.ssh.action.Employer">
        <property name="employerservice" ref="employservice"></property>
    </bean>





</beans>

hibernate的配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>

    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>

    <property name="hibernate.show_sql">true</property>

    <property name="hibernate.format_sql">true</property>

    <property name="hbm2ddl.auto">update</property>
    </session-factory>
</hibernate-configuration>

web.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SSHTest</display-name>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <!-- 配置spring的监听器 -->
    <!-- needed for ContextLoaderListener -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:ApplicationContext.xml</param-value>
    </context-param>

    <!-- Bootstraps the root web application context before servlet initialization -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 配置struts2的过滤器 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>



</web-app>

三大框架的整合就主要体现在上面的配置文件上了

下面是示例代码:

实体类:

package com.ssh.en;

import java.util.Date;

public class employ {

    private Integer id;

    private String employName;

    private String email;

    private Date birthday;

    private Date createTime;

    private Department department;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getEmployName() {
        return employName;
    }

    public void setEmployName(String employName) {
        this.employName = employName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }


}

映射文件:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-8-23 18:23:12 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="com.ssh.en.employ" table="SSH_EMPLOY">

        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>

        <property name="employName" type="java.lang.String">
            <column name="EMPLOY_NAME" />
        </property>

        <property name="email" type="java.lang.String">
            <column name="EMAIL" />
        </property>

        <property name="birthday" type="java.util.Date">
            <column name="BIRTHDAY" />
        </property>

        <property name="createTime" type="java.util.Date">
            <column name="CREATETIME" />
        </property>

        <many-to-one name="department" class="com.ssh.en.Department" lazy="false">
            <column name="DEPARTMENT_ID" />
        </many-to-one>
    </class>
</hibernate-mapping>

实体类:

package com.ssh.en;

public class Department {
   private Integer id;

   private String departmentName;

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getDepartmentName() {
    return departmentName;
}

public void setDepartmentName(String departmentName) {
    this.departmentName = departmentName;
}


}

映射文件:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-8-23 18:23:12 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="com.ssh.en.Department" table="SSH_DEPARTMENT">
        <id name="id" type="java.lang.Integer">
            <column name="DEPARTMENT_ID" />
            <generator class="native" />
        </id>
        <property name="departmentName" type="java.lang.String">
            <column name="DEPARTMENT_NAME" />
        </property>
    </class>
</hibernate-mapping>

dao层:

package com.ssh.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.ssh.en.employ;



public class Employerdao {

    private SessionFactory sessionfactory;

    public void setSessionFactory(SessionFactory sessionfactory)
    {
        this.sessionfactory=sessionfactory;

        if(sessionfactory==null)
        {
            System.out.println("dao层中的sessionfactory为空");
        }

        System.out.println(sessionfactory+"sessionfactory对象被加载到dao中........");
    }

    public Session getSession()
    {
        Session session= sessionfactory.getCurrentSession();  //获得当前session
        if(session==null)
        {
            System.out.println("当前获得的session为空");
        }
        return session;
    }

    public List<employ> GetAll()
    {
        String hql="FROM employ";
        return getSession().createQuery(hql).list();
    }

}

service层:

package com.ssh.service;

import java.util.List;

import com.ssh.dao.Employerdao;
import com.ssh.en.employ;

public class Employservice {

    private Employerdao employdao;



    public Employerdao getEmploydao() {

        return employdao;
    }



    public void setEmploydao(Employerdao employdao) {
        this.employdao = employdao;
        if(employdao==null)
        {
            System.out.println("service层中的dao对象为空");
        }

        System.out.println(employdao+"employdao对象被加载到service中........");
    }



    public List<employ> getAll()
    {
        return employdao.GetAll();
    }

}

action:

package com.ssh.action;

import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh.en.employ;
import com.ssh.service.Employservice;

public class Employer extends ActionSupport implements RequestAware{

    private static final long serialVersionUID = 1L;

    private Employservice employerservice;

    private Map<String, Object> request;

    public Employservice getEmployerservice() {

        return employerservice;
    }

    public void setEmployerservice(Employservice employerservice) {

        this.employerservice = employerservice;

        if(this.employerservice==null)
        {
            System.out.println("action中的service对象为空");
        }

        System.out.println("action中的employservice被初始化为"+employerservice);

    }


    public String employerlist()
    {
        List<employ> employlist = employerservice.getAll();

        System.out.println(employlist.size());
        request.put("list", employlist);
        return "employerlist";
    }

    @Override

    public void setRequest(Map<String, Object> arg0) {
        this.request=arg0;
    }


}

SSH框架的整合就上面的一些东西了

在整合的时候,可能会遇到下面的一些异常:

重: Exception occurred during processing request: Could not open Hibernate Session for transaction; nested exception is java.lang.NoClassDefFoundError: org/hibernate/engine/transaction/spi/TransactionContext
org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is java.lang.NoClassDefFoundError: org/hibernate/engine/transaction/spi/TransactionContext

引起这个异常的原因不止一个 ,但有可能是jar包的版本问题

将hibernate-core-5.2.8.Final.jar换成hibernate-core-4.2.4.Final.jar就好了

第二异常:

:action层中的service对象注入成功之后,但是在action中的处理方法中service层的对象为空

这个问题的原因在strut2的配置文件中的action的类名应该写为在springbean配置文件中action类的id号

第三个异常:


Method "list" failed for object com.ssh.action.Employer@6cfe145b [java.lang.NoClassDefFoundError: javassist/util/proxy/Proxy]

这个问题也是jar包版本问题,把javassist.jar包的版本换成4.3版本就好了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值