Java 整合SSH三大框架(完整详解)

三大框架架构(整合原理)

Java 整合SSH三大框架(完整详解)_第1张图片
链接: https://pan.baidu.com/s/1QFXZ1oAfUJSHno5wON1Ybg 密码: jdst

导包(41个)

hibernate

hibernate/lib/required

Java 整合SSH三大框架(完整详解)_第2张图片

hibernate/lib/jpa | java persist api java的持久化规范(接口)

Java 整合SSH三大框架(完整详解)_第3张图片

数据库驱动

这里写图片描述

struts2

struts-blank.war/WEB-INF/lib/*

Java 整合SSH三大框架(完整详解)_第4张图片
注意:javassist-3.18.1-GA.jar包与hibernate中的重复

struts整合spring插件包

这里写图片描述
注意:这个包一旦导入,那么struts2在启动时就会寻找spring容器.找不到将会抛出异常

spring

基本:4+2

core|beans|context|expression|logging|log4j

整合web:web包

spring-web

整合aop:4个

spring-aop|spring-aspect|aop联盟|aopweaving

整合Hibernate和事务:4个

spring-jdbc|spring-tx|c3p0|spring-orm

正junit4测试:test包

spring-test

标签库

standard.jar
jstl-1.2.jar

单独配置Spring容器

创建配置文件,并导入约束(4个) beans | context | aop | tx


<beans 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd 
                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">

<bean name="userAction" class="cn.zdfy.web.action.UserAction">bean>
beans>

配置spring随项目启动

web.xml文件


    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:applicationContext.xmlparam-value>
    context-param>

单独配置struts2

配置struts2主配置文件


   
<struts>
    <package name="crm" namespace="/" extends="struts-default">
        <action name="UserAction_*" class="cn.zdfy.web.action.UserAction"
            method="{1}">
            <result name="success">/success.jspresult>
        action>
    package>
struts>

配置struts2核心过滤器到web.xml


    <filter>
        <filter-name>struts2filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
    filter>
    <filter-mapping>
        <filter-name>struts2filter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

struts2与spring整合

导包 struts2-spring-plugin-2.3.24.jar

配置常量


<constant name="struts.objectFactory" value="spring">constant>

整合方案1:struts2自己创建action,spring负责组装依赖属性


        <action name="UserAction_*" class="cn.zdfy.web.action.UserAction"
            method="{1}">
            <result name="success">/success.jspresult>
        action>

不推荐理由:最好由spring完整管理action的生命周期.spring中功能才应用到Action上.

整合方案2:spring负责创建action以及组装.【推荐】

applicationContext.xml



<bean name="userAction" class="cn.zdfy.web.action.UserAction">
    <property name="userService" ref="userService">property>
bean>

struts.xml

    
        <action name="UserAction_*" class="userAction"
            method="{1}">
            <result name="success">/success.jspresult>
        action>

单独配置hibernate

导入实体类&orm元数据

Java 整合SSH三大框架(完整详解)_第5张图片

配置主配置文件



<hibernate-configuration>
    <session-factory>

        
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driverproperty>
        
        <property name="hibernate.connection.url">jdbc:mysql:///crm_32property>
        
        <property name="hibernate.connection.username">rootproperty>
        
        <property name="hibernate.connection.password">rootproperty>
        
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialectproperty>


        
        <property name="hibernate.show_sql">trueproperty>
        
        <property name="hibernate.format_sql">trueproperty>
        
        <property name="hibernate.hbm2ddl.auto">updateproperty>

        
        <mapping resource="cn/zdfy/domain/Customer.hbm.xml" />
        <mapping resource="cn/zdfy/domain/LinkMan.hbm.xml" />
        <mapping resource="cn/zdfy/domain/User.hbm.xml" />

    session-factory>
hibernate-configuration>

spring整合hibernate

整合原理

将sessionFactory对象交给spring容器管理

在spring中配置sessionFactory

配置方案一:


<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
    <property name="configLocation" value="classpath:hibernate.cfg.xml" >property> 
bean>

配置方案二:【推荐】


<bean name="sessionFactory"
    class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">

    <property name="dataSource" ref="dataSource">property>

    <property name="hibernateProperties">
<props>


    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
    prop>

    <prop key="hibernate.show_sql">trueprop>
    <prop key="hibernate.format_sql">trueprop>
    <prop key="hibernate.hbm2ddl.auto">updateprop>
props>
property>

<property name="mappingDirectoryLocations" value="classpath:cn/zdfy/domain">property>
bean>

spring整合c3p0连接池

1.配置db.properties

jdbc.jdbcUrl=jdbc:mysql:///crm_32
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root

2.引入连接池到spring中


<context:property-placeholder location="classpath:db.properties" />

<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="jdbcUrl" value="${jdbc.jdbcUrl}">property>
    <property name="driverClass" value="${jdbc.driverClass}">property>
    <property name="user" value="${jdbc.user}">property>
    <property name="password" value="${jdbc.password}">property>
bean>

3.将连接池注入给SessionFactory


    <property name="dataSource" ref="dataSource">property>

spring整合hibernate环境操作数据库

Dao类创建:继承HibernateDaoSupport

//HibernateDaoSupport 为dao注入sessionFactory
public class UserDaoImpl extends HibernateDaoSupport implements UserDao

hibernate模板的操作

execute

package cn.zdfy.dao.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.orm.hibernate5.HibernateCallback;

import cn.zdfy.dao.UserDao;
import cn.zdfy.domain.User;

//HibernateDaoSupport 为dao注入sessionFactory
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
    @Override
    public User getByUserCode(final String usercode) {
        // HQL
         return getHibernateTemplate().execute(new HibernateCallback() {
         @Override
         public User doInHibernate(Session session) throws HibernateException {
         String hql = "from User where user_code = ?";
         Query query = session.createQuery(hql);
         query.setParameter(0, usercode);
         return (User) query.uniqueResult();
         }
         });
    }

}

findByCriteria

package cn.zdfy.dao.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.orm.hibernate5.HibernateCallback;

import cn.zdfy.dao.UserDao;
import cn.zdfy.domain.User;

//HibernateDaoSupport 为dao注入sessionFactory
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
    @Override
    public User getByUserCode(final String usercode) {
        // Criteria
        DetachedCriteria dc = DetachedCriteria.forClass(User.class);
        dc.add(Restrictions.eq("user_code", usercode));
        List list = (List) getHibernateTemplate().findByCriteria(dc);
        if (list != null && list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }

}

spring中配置dao


<bean name="userDao" class="cn.zdfy.dao.impl.UserDaoImpl">
    
    <property name="sessionFactory" ref="sessionFactory">property>
bean>

spring的aop事务

准备工作 配置核心事务管理器


<bean name="transactionManager"class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory">property>
bean>

xml配置aop事务

配置通知


    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            
            <tx:method name="save*" isolation="REPEATABLE_READ"
                propagation="REQUIRED" read-only="false" />
            <tx:method name="persist*" isolation="REPEATABLE_READ"
                propagation="REQUIRED" read-only="false" />
            <tx:method name="update*" isolation="REPEATABLE_READ"
                propagation="REQUIRED" read-only="false" />
            <tx:method name="modify*" isolation="REPEATABLE_READ"
                propagation="REQUIRED" read-only="false" />
            <tx:method name="delete*" isolation="REPEATABLE_READ"
                propagation="REQUIRED" read-only="false" />
            <tx:method name="remove*" isolation="REPEATABLE_READ"
                propagation="REQUIRED" read-only="false" />
            <tx:method name="get*" isolation="REPEATABLE_READ"
                propagation="REQUIRED" read-only="true" />
            <tx:method name="find*" isolation="REPEATABLE_READ"
                propagation="REQUIRED" read-only="true" />
        tx:attributes>
    tx:advice>

配置织入

    
    <aop:config>
        
        <aop:pointcut expression="execution(* cn.zdfy.service.impl.*ServiceImpl.*(..))"
            id="txPc" />
        
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" />
    aop:config>

注解配置aop事务

开启注解事务

 
 <tx:annotation-driven transaction-manager="transactionManager"/>

Service类中使用注解

@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)
public class UserServiceImpl implements UserService {
//======================================================//
@Override
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)
public void saveUser(User u) {
    ud.save(u);
}

扩大session作用范围

为了避免使用懒加载时出现no-session问题.需要扩大session的作用范围

配置filter


<filter>
    <filter-name>openSessionInViewfilter-name>
    <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilterfilter-class>
filter>
<filter-mapping>
    <filter-name>openSessionInViewfilter-name>
    <url-pattern>/*url-pattern>
filter-mapping>

你可能感兴趣的:(Java,Java,Spring)