天天看点

ssm框架整合,ssm框架环境搭建

ssm框架整合,ssm框架环境搭建

SSM框架(业务层)–Spring-SpringMVC(表现层)-Mybatis框架(持久层),是后端开发的一个较为方便的一个后端框架。搭建好环境以后,可根据需求选择使用xml或注解进行代码编译,也可选择两者共同使用的方式。

SSM框架由Spring-业务层来整合其他两个框架。

Mybatis(持久层,负责与数据库进行交互,只关心SQL语句的写法,对数据库执行不同操作只需要修改SQL语句即可。业务层调用持久层的方法从数据库中获取想要得到的数据。然后返回给业务层。)

Spring(业务层,负责与持久层和表现层进行交互,作为中间的统筹工作。表现层调用业务层的方法后,业务层进行处理后,调用持久层方法。获取到持久层的数据后返回给表现层。)

SpringMVC(表现层,负责与前端负责交互数据,前端发送请求给后端后,由表现层接受处理后,调用业务层的方法。获取到业务层的数据后返回给前端。)

配置一个applicationContext.xml文件。这里的配置是Spring框架整合Mybatis框架的一些相关配置。

<?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">

    <!--spring框架创建ioc容器的时候需要扫描的包
    开启注解的扫描,希望处理service和dao,controller不需要Spring框架去处理-->
    <context:component-scan base-package="com.classdesign">
        <!--配置哪些注解不扫描-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--Spring整合MyBatis框架-->
    <!--配置连接池,数据源用于连接数据库-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql:///database?characterEncoding=utf8"/>
        <property name="user" value="root"/>
        <property name="password" value="123"/>
    </bean>

    <!--配置SqlSessionFactory工厂,注入数据源,用于创建sqlsession对象,
    使用sqlsession对象来创建dao接口的代理对象,从而调用dao接口的方法去访问数据库-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!--配置Dao接口所在包,配置service-->
    <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.classdesign.dao"/>
    </bean>

    <!--配置Spring框架声明式事务管理-->
    <!--配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!--配置事务通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="*" isolation="DEFAULT"/>
        </tx:attributes>
    </tx:advice>

    <!--配置AOP增强,建立切入点表达式和事务通知的对应关系-->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.classdesign.service.impl.*ServiceImpl.*(..))"/>
    </aop:config>


</beans>
           

配置SpringMVC框架,这里是SpringMVC的主要配置文件。创建一个springmvc.xml文件。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
       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
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!--开启注解扫描,只扫描Controller注解-->
    <context:component-scan base-package="com.classdesign">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

    <!--配置的视图解析器对象-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--过滤静态资源-->
<!--    <mvc:resources location="/css/" mapping="/css/**" />-->
<!--    <mvc:resources location="/images/" mapping="/images/**" />-->
<!--    <mvc:resources location="/js/" mapping="/js/**" />-->

    <!--开启SpringMVC注解的支持-->
    <mvc:annotation-driven/>
    </beans>
           

配置web.xml文件,这里是Spring整合SpringMVC框架。

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <!--配置Spring的监听器,默认只加载WEB-INF目录下的applicationContext.xml配置文件-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!--设置配置文件的路径-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
<!--  <context-param>-->
<!--    <param-name/>-->
<!--    <param-value/>-->
<!--  </context-param>-->

  <!--配置前端控制器-->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--加载springmvc.xml配置文件-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <!--启动服务器,创建该servlet-->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</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>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
</web-app>
           

配置到这里SSM框架的整合就已经完毕了,我们可以根据需要进行编写相关的代码。

这里是SSM框架的三个环境搭建的传送门。

传送门:Mybatis环境搭建

Spring框架环境搭建

SpringMVC框架环境搭建