SSM框架运行原理及配置文件

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>orchid.ssm</groupId>
    <artifactId>ssm</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <!--依赖:JUnit、数据库驱动、连接池、Servlet、jsp、mybatis、mybatis-spring、spring-->
    <dependencies>
        <!--JUnit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!--数据库驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.2</version>
        </dependency>
        <!--数据库连接池-->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>
        <!--Servlet、jsp-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!--Mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <!--Mybatis与spring整合jar包-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>
        <!--Spring-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.2</version>
        </dependency>
    </dependencies>

    <!--静态资源导出问题-->
    <build>
        <!--保证各级子目录下的资源文件被打包-->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
        <plugins>
            <!-- java编译插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <modules>
        <module>ssm_dao</module>
        <module>ssm_domain</module>
        <module>ssm_service</module>
        <module>ssm_utils</module>
        <module>ssm_web</module>
    </modules>

</project>

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<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"
         version="3.1">
         
    <display-name>Archetype Created Web Application</display-name>

    <!--1.前端控制器(加载classpath:spring-mvc.xml 服务器启动创建Servlet)-->
    <!-- servlet 标签给 Tomcat 配置 Servlet 程序 -->
    <servlet>
        <!--servlet-name 标签 Servlet 程序起一个别名(一般是类名) -->
        <servlet-name>dispatcherServlet</servlet-name>
        <!--servlet-class 是 Servlet 程序的全类名-->
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--配置初始化参数,创建完DispatcherServlet对象,加载springmvc.xml配置文件-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <!--applicationContext.xml中导入了springmvc.xml配置文件-->
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <!--服务器启动的时候,让DispatcherServlet对象创建-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!--servlet-mapping 标签给 servlet 程序配置访问地址-->
    <servlet-mapping>
        <!--servlet-name 标签的作用是告诉服务器,我当前配置的地址给哪个 Servlet 程序使用-->
        <servlet-name>dispatcherServlet</servlet-name>
        <!--url-pattern 标签配置访问地址  / 斜杠在服务器解析的时候,表示地址为:http://ip:port/工程路径 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--2.解决中文乱码过滤器-->
    <filter>
        <!--给 filter 起一个别名-->
        <filter-name>characterEncodingFilter</filter-name>
        <!--配置 filter 的全类名-->
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <!--filter-mapping 配置 Filter 过滤器的拦截路径-->
    <filter-mapping>
        <!--filter-name 表示当前的拦截路径给哪个 filter 使用-->
        <filter-name>characterEncodingFilter</filter-name>
        <!--url-pattern 配置拦截路径 / 表示请求地址为:http://ip:port/工程路径/ 映射到 IDEA 的 web 目录-->
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--3.设置session有效时间-->
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>

    <!--4.声明应用范围(整个WEB项目)内的上下文初始化参数,配置加载类路径的配置文件
        spring-security.xml可以在applicationContext.xml中引入,不应在spring-mvc.xml中引入
        applicationContext.xml相当于父容器,spring-mvc.xml相当于子容器
        子容器可以访问父容器,父容器不能访问子容器
        如果外界需要访问父容器则应该从子容器中去访问
    -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!--
        5.配置监听器,可以使spring随项目的创建而创建,随项目的关闭而关闭,避免创建多个applicationContext,
        默认会以 /WEB-INF/applicationContext.xml作为配置文件
    -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--配置监听器,监听request域对象的创建和销毁-->
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

    <!--定制错误页面,根据错误码选择出现错误时的跳转文件。<exception-type>可以捕捉未设置错误码的跳转文件-->
    <!--
    <error-page>
      <error-code>403</error-code>
      <location>/403.jsp</location>
    </error-page>
    <error-page>
      <exception-type>java.io.IOException</exception-type>
      <location>/errorIO.html</location>
    </error-page>
    -->

    <!--指定默认加载页面-->
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    
</web-app>

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"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
">
    <import resource="classpath:spring-mybatis.xml"></import>
    <import resource="classpath:spring-service.xml"></import>
    <import resource="classpath:spring-mvc.xml"></import>

</beans>

spring-mybatis.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"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
">

    <!--Spring整合Mybatis
        1.关联数据库配置文件 (可省略)
        2.配置连接池 (可省略)
        3.SQLSessionFactory
        4.配置dao接口扫描包,动态实现了dao接口注入到spring容器中
    -->
    <!--1.关联数据库配置文件-->
    <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
    <!--2.连接池
        dbcp:半自动化操作,不能自动连接
        c3p0:自动化操作(自动化加载配置文件,自动设置到对象中)
        druid、hikari
    -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClassName}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <!--c3p0连接池最大连接数-->
        <property name="maxPoolSize" value="30"></property>
        <!--c3p0连接池最小连接数-->
        <property name="minPoolSize" value="10"></property>
        <!--关闭连接后不自动连接-->
        <property name="autoCommitOnClose" value="false"></property>
        <!--获取连接超时时间-->
        <property name="checkoutTimeout" value="1000"></property>
        <!--当获取连接失败后重连次数-->
        <property name="acquireRetryAttempts" value="2"></property>
    </bean>
    <!--3.SQLSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"></property>
        <!--绑定Mybatis的配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>
    <!--4.配置dao接口扫描包,动态实现了dao接口注入到spring容器中,如果没有发现注解,则查找映射文件xxx.xml-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--注入sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        <!--需要扫描的dao包-->
        <property name="basePackage" value="orchid.dao"></property>
    </bean>

</beans>

spring-service.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"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
">


    <!--Spring整合Service
        1.扫描service包
        2.将我们所有的业务类,注入到spring中(xml/annotation)
        3.声明式事务配置
        4.开启对注解事务的支持
    -->
    <!--1.扫描service包,如果某个类的头上带有特定的注解 @Component,@Repository,@Service,@Controller,就会将这个对象作为Bean注册进Spring容器。-->
    <!--<context:component-scan base-package="orchid.service"></context:component-scan>-->
    <!--2.将我们所有的业务类,注入到spring中(xml),类上使用了@Service则可以省略此步骤-->
    <bean id="BookServiceImpl" class="orchid.service.impl.BookServiceImpl">
        <property name="bookDao" ref="IBookDao"></property>
    </bean>
    <!--3.声明式事务配置-->
    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--4.开启对注解事务的支持或者在类上使用@EnableTransactionManagement,作用是使@Transactional生效-->
    <!--<tx:annotation-driven transaction-manager="dataSourceTransactionManager" />-->

</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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!--1.注解驱动,可以将请求参数绑定到控制器参数(处理请求映射,决定是哪个controller的哪个方法来处理当前请求,异常处理)-->
    <mvc:annotation-driven></mvc:annotation-driven>
    <!--2.静态资源过滤,如果发现是静态资源的请求,就将该请求转由Web应用服务器默认的Servlet处理,
          如果不是静态资源的请求,才由DispatcherServlet分发器继续处理,转由我们自己编写的Servlet处理。-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>
    <!--3.需要扫描注解的包:controller,使包下的@Component,@Repository,@Service,@Controller,@Autowired,@Resource,@PostConstruct,@PreDestroy,@PersistenceContext,@Required生效-->
    <context:component-scan base-package="orchid.controller"></context:component-scan>
    <!--4.视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--自动给后面action的方法return的字符串加上前缀和后缀,变成一个可用的url地址 -->
        <property name="prefix" value="/WEB-INF/page/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值