zoukankan      html  css  js  c++  java
  • spring-springMVC -mybatis 整合配置

    这个帖子是用来记录ssm 整合框架的简单配置。

     首先这个是我的一个项目构架。在这里我们主要看我们的配置,resources 里面的配置:

    第一个: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"
        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">
        <!-- 开启注解扫描,管理service和dao -->
        <context:component-scan base-package="com.huapei.service">
        </context:component-scan>
        <context:component-scan base-package="com.huapei.dao">
        </context:component-scan>
        
        <context:property-placeholder location="classpath:db.properties"/>
        <!-- 配置连接池 -->
        <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.username}" />
               <property name="password" value="${jdbc.password}" />
           </bean>    
        <!-- 把交给IOC管理 SqlSessionFactory -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
               <property name="dataSource" ref="dataSource" />
               <!-- 传入PageHelper的插件 -->
               <property name="plugins">
                   <array>
                       <!-- 传入插件的对象 -->
                       <bean class="com.github.pagehelper.PageInterceptor">
                           <property name="properties">
                               <props>
                                   <prop key="helperDialect">mysql</prop>
                                   <prop key="reasonable">true</prop>
                               </props>
                           </property>
                       </bean>
                   </array>
               </property>
           </bean>
        <!-- 扫描dao接口 -->
        <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
               <property name="basePackage" value="com.huapei.dao"/>
           </bean>
        
        <!-- 配置Spring的声明式事务管理 -->
        <!-- 配置事务管理器 -->
           <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
               <property name="dataSource" ref="dataSource"/>
           </bean>
      <tx:annotation-driven transaction-manager="transactionManager"/>
    </beans>

    第二个:db.properties

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/ssmdome?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
    jdbc.username=root
    jdbc.password=123456

    第三个:log4j.properties

    # Set root category priority to INFO and its only appender to CONSOLE.
    #log4j.rootCategory=INFO, CONSOLE            debug   info   warn error fatal
    log4j.rootCategory=debug, CONSOLE, LOGFILE
    
    # Set the enterprise logger category to FATAL and its only appender to CONSOLE.
    log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
    
    # CONSOLE is set to be a ConsoleAppender using a PatternLayout.
    log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
    log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
    log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m
    
    
    # LOGFILE is set to be a File appender using a PatternLayout.
    # log4j.appender.LOGFILE=org.apache.log4j.FileAppender
    # log4j.appender.LOGFILE.File=d:axis.log
    # log4j.appender.LOGFILE.Append=true
    # log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
    # log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m
    

    第四个: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"
        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/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
               http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd
               ">
    
        <!-- 扫描controller的注解,别的不扫描 -->
        <context:component-scan base-package="com.huapei.controller">
        </context:component-scan>
        <context:component-scan base-package="com.huapei.utils">
        </context:component-scan>
        <!-- 配置视图解析器 -->
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!-- JSP文件所在的目录 -->
            <property name="prefix" value="/pages/" />
            <!-- 文件的后缀名 -->
            <property name="suffix" value=".jsp" />
        </bean>
    
        <!-- 设置静态资源不过滤 -->
        <mvc:resources location="/css/" mapping="/css/**" />
        <mvc:resources location="/img/" mapping="/img/**" />
        <mvc:resources location="/js/" mapping="/js/**" />
        <mvc:resources location="/plugins/" mapping="/plugins/**" />
       <!-- <mvc:default-servlet-handler/>  ,或者开启默认注释,不过滤静态资源-->
        <!-- 开启对SpringMVC注解的支持 -->
        <mvc:annotation-driven />
    
        <!-- 
            支持AOP的注解支持,AOP底层使用代理技术
            JDK动态代理,要求必须有接口
            cglib代理,生成子类对象,proxy-target-class="true" 默认使用cglib的方式
        -->
        <aop:aspectj-autoproxy proxy-target-class="true"/>
        
    </beans>

     以上是我们的xml 配置。

      除去db.properties(数据库连接数据)  、 log4j.properties(日志配置)

      applicationContext.xml 主要功能:

        1.配置dao、service接口扫描,管理注入bean;

        2.配置数据库连接池

           3.交给IOC管理 SqlSessionFactory ,管理mybatis

        4.以及对spring事务的管理

      springmvc.xm 主要功能:

        1.开启扫描控制层和工具类 的controller 和一些bean;

        2.配置视图解析器 让我们不用写页面的全路径

        3. 设置静态资源不过滤 ,来配置 我们的静态资源不进行过滤

        4. 开启对SpringMVC注解的支持

        5.支持AOP的注解支持,AOP底层使用代理技术

    以上是我们的xml配置以及功能,接下来我们看我们自己的web.xml 的配置

    <?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_4_0.xsd"
             version="4.0">
      <display-name>Archetype Created Web Application</display-name>
      <!-- 配置加载类路径的配置文件 -->
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext.xml</param-value>
      </context-param>
      <!-- 配置监听器 -->
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <!-- 配置监听器,监听request域对象的创建和销毁的 -->
      <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
      </listener>
      <!-- 前端控制器(加载classpath:springmvc.xml 服务器启动创建servlet) -->
      <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 配置初始化参数,创建完DispatcherServlet对象,加载springmvc.xml配置文件 -->
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <!-- 服务器启动的时候,让DispatcherServlet对象创建 -->
        <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>
    
      <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>

      web.xml主要功能:

        1.配置加载类路径的配置文件 如果我们只是ssm的话不用扫描spring-security.xml 关于spring-security ,在我的博客中也有介绍。

        2.配置监听器

        3.配置监听器,监听request域对象的创建和销毁的

        4.前端控制器(加载classpath:springmvc.xml 服务器启动创建servlet)

        5. 解决中文乱码过滤器

      

    以上是一个简单的ssm 整合框架的配置,这些配置后我们可以写一些简单的ssm整合框架项目了。

  • 相关阅读:
    懂一点Python系列——快速入门
    DatagridView 控件列顺序与设置的不一样
    WinForm Column cannot be added because its CellType property is null.
    Rabbitmq消息服务器通讯异常: name must not be blank
    Redis 数据库
    redis 安装与使用
    windows 服务的安装与卸载之bat脚本命令
    以超级管理员方式运行bat文件
    CMD 下切换目录
    C#导出Excel后关闭进程EXCEL.EXE
  • 原文地址:https://www.cnblogs.com/sr990411/p/12928760.html
Copyright © 2011-2022 走看看