Java架构师之旅(二十二)- 关于ssh

2022-08-08,,,,

夜光序言:

“我夜光的未来将由我自己创造!我夜光的天地将由我自己开创!”

 

 

 

正文:

前提要掌握ssh最好

 

  1. spring4.X
  2. Struts2
  3. Hibernate5.X

 

 

 

A  \   Hibernate5.X框架:

我们需要掌握orm思想~~:对象关系映射

我们需要知道数据库信息

我们需要知道映射配置

 

核心:如果我们单纯使用Hibernate框架,核心配置文件名称hibernate.cfg.xml和位置src下面固定的

Hibernate和spring整合时,Hibernate核心配置文件名称和位置没有固定要求

 

 

精华:使用spring对Hibernate框架进行封装,我们可以使用HibernateTemplate

 

 

 

 

B \      Struts2框架:

 

我们还需要了解一下:

1、Action操作:

① Action创建三种方式

第一种是写一个普通类

第二种是写一个接口

 

第三种就是继承类actionsupport【这是我们最常使用的一个方法】

 

②配置action访问路径

使用通配符方式创建

 

  • 在action获取表单提交数据

获取request对象【使用servletactionContext】

属性封装

模型驱动【夜光:我们在开发中用的最多的还是这个】

表达式封装

 

 

  • 在action域操作对象

使用servletactionContext获取域对象

 

 

 

  • 配置stuts2过滤器

 

这就是过滤器名字

 

 

2、值栈

我们在开发中用的并不是特别多

① 向值栈放数据

Get方法

Push方法

定义变量,生成get方法

 

② 从值栈获取数据

在jsp中使用stuts2标签+ognl获取

<s:property>

<s:iterator>

 

3、拦截器

  • aop和责任链模式
  • 自定义拦截器

继承:MethodFilterInterceptor

重写类里面的方法

配置拦截器和action关联

 

 

===================

C  \   Spring框架

 

1、核心配置文件

 

① 名称和位置没有固定要求

② 在spring核心配置文件中引入schema约束

 

 

2、创建对象

① XML配置方式:<bean id=”” class=””></bean>

② 注解方式:四个注解

 

3、注入属性

  • XML配置方式
  • 注解方式,两个注解

 

4、使用servletcontext对象和监听器实现

  • 在服务器启动时候,加载spring配置文件,创建对象
  • 配置spring监听器
  • 指定spring配置文件位置

 

5、jdbcTemplate

 

6、spring事务配置

  • xml方式
  • 注解方式

 

 

三大框架已经学完,我们在开发中需要整合开发,单独发挥不了最大作用,整合起来用更加舒服~~

 

“我夜光的未来将由我自己创造!我夜光的天地将由我自己开创!”

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

第一个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/tx

        http://www.springframework.org/schema/tx/spring-tx.xsd

        http://www.springframework.org/schema/aop

        http://www.springframework.org/schema/aop/spring-aop.xsd"> 

        

        

        

        <!-- bean definitions here -->

 

<!-- 夜光:配置c3p0的一个连接池 -->

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

            <!-- 注入属性值 -->

<property name="driverClass" value="com.mysql.jdbc.Driver"></property>

<property name="jdbcUrl" value="jdbc:mysql:///studentsmanager"></property>

<property name="user" value="root"></property>

<property name="password" value="1111"></property>

 

</bean>

 

 

<bean id="ordersService" class="cn.Genius.service.OrdersService">

       <property name="ordersDao" ref="ordersDao"></property>

</bean>

<bean id="ordersDao" class="cn.Genius.dao.OrdersDao">

<!-- 夜光:name是类中属性的名字 ,千万不可以写错-->

       <property name="jdbcTemplate" ref="jdbcTemplate"></property>

</bean>

 

 

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">

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

</bean>

 

 

 

</beans>

 

 

 

 

 

 

 

 

第二个类:

package cn.Genius.service;

 

import cn.Genius.dao.OrdersDao;

 

public class OrdersService {

 

private OrdersDao ordersDao;

 

public void setOrdersDao(OrdersDao ordersDao) {

this.ordersDao = ordersDao;

}

 

//夜光:调用dao里面的方法

//业务逻辑层,写转账业务

public void accountMoney(){

//小动转账1000给小林

 

//小动少1000

ordersDao.lessMoney();

 

//小林多1000

ordersDao.moreMoney();

}

 

 

}

 

第三个类:

package cn.Genius.dao;

 

import org.springframework.jdbc.core.JdbcTemplate;

 

public class OrdersDao {

 

//夜光:注入jdbcTemplate模板

private JdbcTemplate jdbcTemplate;

 

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {

this.jdbcTemplate = jdbcTemplate;

}

 

 

/*夜光:dao里面,一般就是做对数据库的操作

 *

 * 不写业务操作~~

 *

 * ~~*/

 

 

//夜光:少钱的方法,我们写方法就要见名思意~~写sql语句就可以了

public void lessMoney(){

String sql = "update account set salary=salary-? where username=?";

jdbcTemplate.update(sql, 1000,"小动");

}

 

 

 

//夜光:多钱的方法

public void moreMoney(){

String sql = "update account set salary=salary+? wher username=?";

jdbcTemplate.update(sql, 1000,"小林");

}

 

 

 

}

 

 

最后写一个测试类:

 

 

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

import cn.Genius.service.OrdersService;

 

public class TestService {

 

 

@Test

public void TestDemo(){

ApplicationContext context =

new ClassPathXmlApplicationContext("bean1.xml");

OrdersService service = (OrdersService) context.getBean("ordersService");

service.accountMoney();

}

}

本文地址:https://blog.csdn.net/weixin_41987706/article/details/85990076

《Java架构师之旅(二十二)- 关于ssh.doc》

下载本文的Word格式文档,以方便收藏与打印。