SSM整合总结笔记-1(nf4slide项目笔记 )

苍宝
2023-12-01

Spring

SpringMVC

分离控制器、模型对象、分派器以及处理程序对象的角色

配置springmvc-servlet.xml文件、配置web.xml文件

Mybatis

本为ibatis。基于Java的持久层框架,(SQL  Maps 和DAO即Data Access Objects)。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。(配置Mapping映射文件)

整合

  • 配置spring-mvc-servlet.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:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- scan the package and the sub package -->
    <!-- 自动扫描该包,SpringMVC认为包下@controller注解的类是控制器 -->  
    <context:component-scan base-package="com.qtu404"/>

    <!-- don't handle the static resource -->
    <mvc:default-servlet-handler/>

    <!-- if you use annotation you must configure following setting -->
    <!-- 扩充了注解驱动,可以将请求参数绑定到控制器参数 -->
    <mvc:annotation-driven/>

<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->  
<!-- 处理文件上传-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
          p:defaultEncoding="UTF-8">
    </bean>

    <bean id="logger" class="com.qtu404.common.aop.Logger"/>
    <aop:config>

        <!--用户操作日志-->
        <aop:aspect id="userLog" ref="logger">
            <aop:pointcut id="userPointcut"
                          expression="execution(* com.qtu404.user.controller.UserController.*(..))"/>
            <aop:after method="saveLog" pointcut-ref="userPointcut"/>
        </aop:aspect>

        <!--幻灯片操作日志-->
        <aop:aspect id="slideLog" ref="logger">
            <aop:pointcut id="slidePointcut"
                          expression="execution(* com.qtu404.slide.controller.SlideController.*(..))"/>
            <aop:after method="saveLog" pointcut-ref="slidePointcut"/>
        </aop:aspect>

        <!--文件操作日志-->
        <aop:aspect id="fileLog" ref="logger">
            <aop:pointcut id="filePointcut"
                          expression="execution(* com.qtu404.slide.controller.FileController.*(..))"/>
            <aop:after method="saveLog" pointcut-ref="filePointcut"/>
        </aop:aspect>

        <!--文件夹作日志-->
        <aop:aspect id="folderLog" ref="logger">
            <aop:pointcut id="folderPointcut"
                          expression="execution(* com.qtu404.folder.controller.FolderController.*(..))"/>
            <aop:after method="saveLog" pointcut-ref="folderPointcut"/>
        </aop:aspect>
    </aop:config>

    <!-- configure the InternalResourceViewResolver -->
 <!-- 定义跳转的文件的前后缀 ,视图模式配置-->  
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          id="internalResourceViewResolver">
<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->  
        <!-- 前缀 -->
        <property name="prefix" value="/"/>
        <!-- 后缀 -->
        <property name="suffix" value=".html"/>
    </bean>
</beans>
  • 配置web.xml文件,配置的spring-mvc的Servlet就是为了完成SpringMVC+MAVEN的整合。
  • Log4j:使用日志来输出信息 Log4j配置详解
<?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">
    <display-name>nf404</display-name>

    <welcome-file-list>
        <welcome-file>homeA.html</welcome-file>
    </welcome-file-list>

    <!--spring配置文件的位置-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <!--log4j配置-->
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:log4j.properties</param-value>
    </context-param>
 <!-- Spring监听器 -->  
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

<!-- Spring MVC servlet --> 
    <servlet>
        <servlet-name>dispatch</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatch</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

<!-- 编码过滤器 -->  
    <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>
</web-app>
  • 配置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>
    <properties resource="db.propreties"/>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <!--maxActive: 最大连接数量-->
                <property name="maxActive" value="150"/>
                <!--minIdle: 最小空闲连接-->
                <property name="minIdle" value="5"/>
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
                <property name="removeAbandoned" value="true"/>
                <!--removeAbandonedTimeout: 超时时间(以秒数为单位)-->
                <property name="removeAbandonedTimeout" value="30"/>
                <!--maxWait: 超时等待时间以毫秒为单位 -->
                <property name="maxWait" value="20000"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/qtu404/mapper/slideMapper.xml"/>
        <mapper resource="com/qtu404/mapper/userMapper.xml"/>
        <mapper resource="com/qtu404/mapper/answerMapper.xml"/>
        <mapper resource="com/qtu404/mapper/folderMapper.xml"/>
        <mapper resource="com/qtu404/mapper/optionMapper.xml"/>
        <mapper resource="com/qtu404/mapper/publishMapper.xml"/>
        <mapper resource="com/qtu404/mapper/questionMapper.xml"/>
        <mapper resource="com/qtu404/mapper/attendanceMapper.xml"/>
    </mappers>
</configuration>
  • 设置db.properties文件
driver=com.mysql.jdbc.Driver
url = 
username = 
password = 

 @Deprecated:若某类或某方法加上该注解之后,表示此方法或类不再建议使用,调用时也会出现删除线,但并不代表不能用,只是说,不推荐使用,因为还有更好的方法可以调用。

 类似资料: