聊聊SSH框架

前期准备工作

习惯性安装有

jrebel(热加载,后台会自动帮忙部署项目)

如果需要,还要先安装jrebel,然后破解下

如果在项目中安装成功,则会自动生成rebel.xml文件

lombok(根据字段,自动生成对应的set和get方法)

eclipse中的使用方法

我叙述的是IDEA中的使用,越用越舒服

先安装lombok plugin插件,再添加lombok.jar包

log4j(日志打印)

所需jar包

log4j-core-2.10.0.jar
log4j-api-2.10.0.jar

配有log4j2.xml(因为log4j-api-2.10.0jar包是2.0版本,若是1.0版本,需要配置的是log4j.properties文件)

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>
        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="com.opensymphony.xwork2" level="info"/>
        <Logger name="org.apache.struts2" level="info"/>
        <Logger name="org.demo.rest" level="debug"/>
        <Root level="warn">
            <AppenderRef ref="STDOUT"/>
        </Root>
    </Loggers>
</Configuration>

log4j.properties

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{} %5p %c{1}:%L - %m%n

### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c\:mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###
# error warn info debug trace
log4j.rootLogger= info, stdout

开启注解

设置-构建,执行,部署-编译器-Annotation Processors

勾起Enable annotation processing

Hibernate框架

需要的jar包

antlr-2.7.7.jar
byte-buddy-1.8.12.jar
c3p0-0.9.5.2.jar
classmate-1.3.4.jar
commons-beanutils-1.8.3.jar
dom4j-1.6.1.jar
hibernate-c3p0-5.3.1.Final.jar
hibernate-commons-annotations-5.0.3.Final.jar
hibernate-core-5.3.1.Final.jar
jandex-2.0.3.Final.jar
javassist-3.22.0-GA.jar
javax.persistence-api-2.2.jar
jboss-logging-3.3.2.Final.jar
jboss-transaction-api_1.2_spec-1.1.1.Final.jar
junit-4.9.jar
log4j-1.2.16.jar
lombok.jar
mchange-commons-java-0.2.11.jar
mysql-connector-java-5.1.7-bin.jar

jdbc.properties

jdbc.driverclass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///spring
jdbc.username=root
jdbc.password=123456

com.le.domain.Account类

package com.le.domain;
import lombok.Getter;
import lombok.Setter;
@Setter@Getter
public class Account {
    private Integer id;
    private String name;
    private Double money;
    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}

com.le.domain.Account.hbm.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.le.domain.Account" table="account" >
        <!--建立类属性哪一个是主键  还要跟数据库当中主键进行对象-->
        <id name="id" column="id" >
            <generator class="native"/>
        </id>
        <!--建立类中的普通属性与数据库当中字段进行关联-->
        <property name="name" column="name" />
        <property name="money" column="money"/>
    </class>
</hibernate-mapping>

hibernateApplication.xml

<?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: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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
">
    <!--配置hibernate-->
    <!--引入属性文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driverclass}" />
        <!--属性文件当中的名称不能和name名称一样-->
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
    <!-- Spring整合Hibernate -->
    <!-- 引入Hibernate的配置的信息===== spring-orm.jar========== -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <!-- 注入连接池 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 配置Hibernate的相关属性 -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
        <!-- 设置映射文件 -->
        <property name="mappingResources">
            <list>
                <value>com/le/domain/Account.hbm.xml</value>
            </list>
        </property>
    </bean>
    <!--配置事务管理器-->
    <bean id="transactionManager"
          class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <!--开启注解  增强-->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

Struts框架

需要的jar包

asm-5.2.jar
asm-commons-5.2.jar
asm-tree-5.2.jar
commons-fileupload-1.3.3.jar
commons-io-2.5.jar
commons-lang3-3.6.jar
freemarker-2.3.26-incubating.jar
javassist-3.20.0-GA.jar
junit-4.9.jar
log4j-api-2.10.0.jar
log4j-core-2.10.0.jar
ognl-3.1.15.jar
struts2-core-2.5.16.jar

web/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!--Spring的核心监听器-->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <!--
     加载Spring的配置文件的路径的
     默认加载的/WEB-INF/applicationContext.xml
     -->
    <context-param>
        <param-name> contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
</web-app>

struts.xml

其中action交由spring管理

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>

    <package name="struts" extends="struts-default">
        <!--spring 当中的id-->
        <action name="account_*" class="accountAction" method="{1}"/>
    </package>
</struts>

com.le.web.AccountAction

package com.le.web;
import com.le.domain.Account;
import com.le.service.AccountService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class AccountAction extends ActionSupport implements ModelDriven<Account> {
    private Account account = new Account();
    @Override
    public Account getModel()
    {
        return account;
    }

    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }

    AccountService accountService;
    public String save()
    {
        accountService.save(account);
        System.out.println("AccountAction  保存了" + account.toString());
        return  null;
    }
}

com.le.service.AccountService

package com.le.service;
import com.le.domain.Account;

public interface AccountService {
    public void save(Account account);
}

com.le.service.AccountServiceImpl

package com.le.service;

import com.le.dao.AccountDao;
import com.le.domain.Account;
import lombok.Setter;
import org.springframework.transaction.annotation.Transactional;

@Setter
@Transactional
public class AccountServiceImpl implements AccountService {
    private AccountDao accountDao;

    @Override
    public void save(Account account) {
        System.out.println("已经来到业务类"+account);
        //调用dao保存到数据库当中
        accountDao.save(account);
    }
}

com.le.dao.AccountDao

package com.le.dao;
import com.le.domain.Account;

public interface AccountDao {
    public void save(Account account);
}

com.le.dao.AccountDaoImpl

package com.le.dao;
import com.le.domain.Account;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

public class AccountDaoImpl extends HibernateDaoSupport implements AccountDao {

    @Override
    public void save(Account account) {
        System.out.println("AccountDaoImpl 保存到数据库");
        this.getHibernateTemplate().save(account);
    }
}

Spring框架

需要的jar包

antlr-2.7.7.jar
asm-5.2.jar
asm-commons-5.2.jar
asm-tree-5.2.jar
byte-buddy-1.8.12.jar
c3p0-0.9.5.2.jar
classmate-1.3.4.jar
com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.apache.commons.pool-1.5.3.jar
com.springsource.org.apache.log4j-1.2.15.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-beanutils-1.8.3.jar
commons-fileupload-1.3.3.jar
commons-io-2.5.jar
commons-lang3-3.6.jar
dom4j-1.6.1.jar
druid-1.0.15.jar
freemarker-2.3.26-incubating.jar
hamcrest-core-1.3.jar
hibernate-c3p0-5.3.1.Final.jar
hibernate-commons-annotations-5.0.3.Final.jar
hibernate-core-5.3.1.Final.jar
jandex-2.0.3.Final.jar
javassist-3.22.0-GA.jar
javax.persistence-api-2.2.jar
jboss-logging-3.3.2.Final.jar
jboss-transaction-api_1.2_spec-1.1.1.Final.jar
junit-4.12.jar
log4j-api-2.10.0.jar
log4j-core-2.10.0.jar
mchange-commons-java-0.2.11.jar
mysql-connector-java-5.1.7-bin.jar
ognl-3.1.15.jar
spring-aop-5.0.7.RELEASE.jar
spring-aspects-5.0.7.RELEASE.jar
spring-beans-5.0.7.RELEASE.jar
spring-context-5.0.7.RELEASE.jar
spring-core-5.0.7.RELEASE.jar
spring-expression-5.0.7.RELEASE.jar
spring-jdbc-5.0.7.RELEASE.jar
spring-orm-5.0.7.RELEASE.jar
spring-test-5.0.7.RELEASE.jar
spring-tx-5.0.7.RELEASE.jar
spring-web-5.0.7.RELEASE.jar
struts2-core-2.5.16.jar
struts2-spring-plugin-2.5.16.jar

applicationContext.xml

<?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: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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
     http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
">

    <!--导入hibernate相关配置-->
    <import resource="hibernateApplication.xml"/>

    <!--action   scope="prototype"-->
    <bean id="accountAction" class="com.le.web.AccountAction" scope="prototype">
        <property name="accountService" ref="accountService"/>
    </bean>
    <!--service-->
    <bean id="accountService" class="com.le.service.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"/>
    </bean>
    <!--dao-->
    <bean id="accountDao" class="com.le.dao.AccountDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
</beans>

我有话说

看完三大框架后,我有点想法

Spring相当于管理者,Struts和Hibernate这则相当于员工

Spring虽然没有具体的实现底层的方法,但是Struts和Hibernate有啊

于是Spring就通过招入Struts和Hibernate,负责帮它管理项目

负责Domain层的Hibernate

先从Hibernate说起,它的作用负责从数据库取出数据,并封装成对象供Spring和Struts使用。

负责web层的Struts

Struts是Spring的助理,负责帮Spring处理接收请求。

简化最后的Spring的工作,封装接收的参数为对象。

负责Service和Dao层的Spring

Spring手段众多,能运用控制反转和依赖注入等手段,处理服务层和使用底层数据的操作。

将连接池和模板交给Spring管理,配置文件配置Bean,使用jdbcTemplate注解插入数据。

Aop手段则是帮它处理一些特殊需求,添加些特殊服务。

JDBC手段能够更好的操作数据,增删改查。

最后附上我的项目结构图

自动根据名称注入信息(导入struts2-spring-plugin-2.5.16.jar后)

action中的class自动注入

spring整合web(开启自动注入,跳过这步骤)

ServletContext相当于Web应用上下文

ServletContext官方叫servlet上下文。

服务器会为每一个工程创建一个对象,这个对象就是ServletContext对象。

这个对象全局唯一,而且工程内部的所有servlet都共享这个对象。

所以叫全局应用程序共享对象。

作用

  1. 是一个域对象
  2. 可以读取全局配置参数
  3. 可以搜索当前工程目录下面的资源文件
  4. 可以获取当前工程名字(了解)

WebApplicationContext相当于是Spring的工厂

struts2-spring-plugin-2.5.16.jar

是为了开启常量,将default.properties中struts.objectFactory.spring.autoWire=name后的语句生效

posted @ 2019-04-22 12:23  海韵༒听心  阅读(488)  评论(0编辑  收藏  举报