ssm回顾笔记(一)

这两天来到了农银,这边即将进行的一个项目是将ssh框架的电商项目迁移到springboot+ssm框架上,所以我基本上是三门技术在同时进行学习,当然以前学过ssm,现在只是回顾.

spring 注解

@Component 下细化了@Controller,@Service,@Repository,分别注解表示层,业务逻辑层,持久化层 @Scope,默认是 singleton,可以配置为 prototype
@Autowired 默认根据类型装配,可以装配多个参数的 setter 方法
@Resource,是 Java 的,必须是 JDK6 及以上,默认根据名称装配,如果找不到会根据
类型装配
RequestMapping 可以加在类和方法上,指定接收请求的路径
类:作用相当于命名空间
方法:请求的具体资源的路径
ResponseBody 将数据放到响应体中

回顾以前不用 maven 搭建开发环境的步骤(必须熟悉)

resource目录下的配置文件

在 web.xml 文件中进行配置

> spring 监听器 该监听器在服务器启动阶段被创建,解析 spring 的配置文件,监听实现了 ServletContextListener 接口,在创建 ServletContext 对象创建和销毁的时候,监听对象中的方法会被执行
> *过滤器:CharacterEncodingFilter解决 post 请求中文乱码的问题
> spring 的前端控制器 DispatcherServlet主要用来拦截符合要求的外部请求,并把请求分发到不同的控制器去处理,根据控制器处理后的结果,生成相应的响应发送到客户端默认解析在 WEB-INF 目录下找[servlet-name]-servlet.xml 文件,但是我们一般通过参数指定配置文件,而且尽量让这个配置文件的形式和 spring 总体的配置文件形式相同,那么在服务器启动的时候,前端控制器实例也会被创建

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

    <!-- 指定spring配置文件路径 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-*.xml</param-value>
    </context-param>
    <!-- 配置spring监听器,该监听器在服务器启动阶段会被实例化,解析spring的配置文件;该监听器实现了ServletContextListener,监听ServletContext
    对象,在ServletContext对象创建或者销毁的时候,监听器中的方法会被执行 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!-- 解决post请求中文乱码的过滤器 -->
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <!-- 配置springMVC中央调度器 -->
    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 指定springMVC配置文件的路径,不是必须要指定的;默认情况下,会去WEB-INF/[servlet-name]-servlet.xml,
        但是一般我们都手动指定springMVC配置文件的路径,而且springMVC文件的命名尽量和spring配置文件风格一致 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <!-- 在服务器启动阶段实例化该servlet -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

spring-base.xml(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: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">
        
    <!-- 注册组件扫描器 -->
    <context:component-scan base-package="com.bjpowernode.epay.service.impl"/>
        
    <!-- 引入外部的配置文件,读取数据库配置信息 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    
    <!-- 数据源  c3p0 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    
    <!-- sessionFactory -->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations">
            <list>
                <value>classpath:com/bjpowernode/epay/dao/*.xml</value>
            </list>
        </property>
    </bean>
    
    <!-- 配置mapper扫描器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.bjpowernode.epay.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sessionFactory"/>
    </bean>
    
    <!-- TransactionManager -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    
    <!-- 事务通知Advice  Aspect是横切面,是一个抽象的概念,Advice是Aspect的具体实现 -->
    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="java.lang.Exception"/>    
            <tx:method name="get*" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    
    <!-- AOP切入 -->
    <aop:config>
        <aop:advisor advice-ref="transactionAdvice" pointcut="execution(* com.bjpowernode.epay.service.*.*(..))"/>
    </aop:config>
</beans>

spring-mvc.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"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
    <!-- 注册组件扫描器  扫描所有处理器类 -->
    <context:component-scan base-package="com.bjpowernode.epay.web.controller"/>
    
    <!-- 注册mvc注解驱动 -->
    <mvc:annotation-driven/>
    
    <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">    
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    
    <!-- 注册拦截器 -->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/*/*.do"/>
            <mvc:exclude-mapping path="/main/login.do"/>
            <bean class="com.bjpowernode.epay.web.AuthInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>
</beans>

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <!-- 解决插入的时候有空值的情况 -->
        <setting name="jdbcTypeForNull" value="NULL"/>
    </settings>
    <!-- 设置别名 -->
    <typeAliases>
        <package name="com.bjpowernode.epay.domain"/>
    </typeAliases>    
</configuration>
原文地址:https://www.cnblogs.com/goujh/p/8671119.html