zoukankan      html  css  js  c++  java
  • spring整合springmvc和Mybatis

    1.jar包

    2.web.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     5          version="3.1">
     6     <!-- 确定配置文件位置 -->
     7     <context-param>
     8         <param-name>contextConfigLocation</param-name>
     9         <param-value>/WEB-INF/applicationContext.xml</param-value>
    10     </context-param>
    11     <!-- 配置spring 监听器,加载xml配置文件 -->
    12     <listener>
    13         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    14     </listener>
    15 
    16     <!-- spring MVC config start-->
    17     <servlet>
    18         <servlet-name>spring</servlet-name>
    19         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    20         <init-param>
    21             <param-name>contextConfigLocation</param-name>
    22             <!-- 此处指向的的是SpringMVC的配置文件 -->
    23             <param-value>/WEB-INF/springmvc-config.xml</param-value>
    24         </init-param>
    25         <!--配置容器在启动的时候就加载这个servlet并实例化-->
    26         <load-on-startup>1</load-on-startup>
    27     </servlet>
    28 
    29     <servlet-mapping>
    30         <servlet-name>spring</servlet-name>
    31         <url-pattern>/</url-pattern>
    32     </servlet-mapping>
    33     <!-- spring MVC config end-->
    34 
    35     <!--  字符集过滤  -->
    36     <filter>
    37         <filter-name>encodingFilter</filter-name>
    38         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    39         <init-param>
    40             <param-name>encoding</param-name>
    41             <param-value>UTF-8</param-value>
    42         </init-param>
    43         <init-param>
    44             <param-name>forceEncoding</param-name>
    45             <param-value>true</param-value>
    46         </init-param>
    47     </filter>
    48     <filter-mapping>
    49         <filter-name>encodingFilter</filter-name>
    50         <url-pattern>/*</url-pattern>
    51     </filter-mapping>
    52 </web-app>
    web.xml

    3.applicationContext.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xmlns:context="http://www.springframework.org/schema/context"
     5        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
     6        xmlns:mybatis="http://mybatis.org/schema/mybatis-spring" xmlns:p="http://www.springframework.org/schema/p"
     7        xsi:schemaLocation="http://www.springframework.org/schema/beans
     8                               http://www.springframework.org/schema/beans/spring-beans.xsd
     9                               http://www.springframework.org/schema/aop 
    10                               http://www.springframework.org/schema/aop/spring-aop.xsd
    11                               http://www.springframework.org/schema/context 
    12                               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://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">
    13 
    14     <!-- mybatis:scan会扫描com.getword.dao包里的所有接口当作Spring的bean配置,之后可以进行依赖注入-->
    15     <mybatis:scan base-package="com.getword.dao"/>
    16 
    17     <!-- 扫描com.getword包下面的java文件,有Spring的相关注解的类,则把这些类注册为Spring的bean -->
    18     <!--<context:component-scan base-package="com.getword"/>-->
    19 
    20     <!-- 自动扫描 -->
    21     <context:component-scan base-package="com.getword">
    22         <!-- 扫描时跳过 @Controller 注解的JAVA类(控制器) -->
    23         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    24     </context:component-scan>
    25 
    26     <!-- 1 datasource -->
    27     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    28         <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    29         <property name="url" value="jdbc:mysql://47.94.157.43:3306/getword"></property>
    30         <property name="username" value="root"></property>
    31         <property name="password" value="zx@123"></property>
    32         <property name="maxActive" value="15"></property>
    33         <property name="maxIdle" value="5"></property>
    34 
    35         <!--<property name="driverClass" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>-->
    36         <!--<property name="jdbcUrl" value="jdbc:sqlserver://localhost:1433;databaseName=getword"></property>-->
    37         <!--<property name="user" value="sa"></property>-->
    38         <!--<property name="password" value="zx@123"></property>-->
    39     </bean>
    40 
    41     <!-- 2.配置sqlsessionfactory -->
    42     <bean class="org.mybatis.spring.SqlSessionFactoryBean">
    43         <!--注入数据源-->
    44         <property name="dataSource" ref="dataSource"></property>
    45         <!-- 设置mybatis配置文件路径 -->
    46         <!--<property name="configLocation" value="classpath:mybatis-config.xml"></property>-->
    47     </bean>
    48 
    49     <!-- JDBC事务管理器 -->
    50     <bean id="transactionManager"
    51           class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
    52           p:dataSource-ref="dataSource"/>
    53 
    54     <!-- 启用支持annotation注解方式事务管理 -->
    55     <tx:annotation-driven transaction-manager="transactionManager"  proxy-target-class="true"/>
    56 
    57 
    58     <!---文件下载,处理器-->
    59     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    60         <property name="defaultEncoding" value="UTF-8"/>
    61         <!-- maxUploadSize设置-1 不限制文件大小 -->
    62         <property name="maxUploadSize" value="-1"/>
    63     </bean>
    64 
    65 </beans>
    applicationContext.xml

     4.springmvc-config.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-4.2.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd     
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    
        <!-- 启动注解驱动的spring MVC功能,注册请求url和注解POJO类方法的映射-->
        <mvc:annotation-driven />
        <!-- spring可以自动去扫描base-pack下面的包或者子包下面的java文件,
            如果扫描到有Spring的相关注解的类,则把这些类注册为Spring的bean -->
        <context:component-scan base-package="com.getword.controller"/>
        
        <!-- 视图解析器  -->
         <bean id="viewResolver"
              class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
            <property name="prefix">
                <value>/WEB-INF/views/</value>
            </property>
            <!-- 
            <property name="suffix">
                <value>.jsp</value>
            </property>
             -->
        </bean>
        <!-- 对静态资源的映射-->
        <!-- 对静态资源文件的访问  方案一 (二选一) -->
        <mvc:default-servlet-handler/>
        <!--<mvc:resources mapping="/js/**" location="resource/js/" />-->
        <!--<mvc:resources mapping="/css/**" location="resource/css/" />-->
        <!--<mvc:resources mapping="/img/**" location="resource/img/" />-->
    </beans>
    springmvc-config.xml

     5.log4j.properties

    #全局日志配置
    log4j.rootLogger=ERROR,stdout
    #MyBatis的日志配置
    log4j.logger.com.getword=DEBUG
    #控制台输出
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%5p [%t] -%m%n
    log4j.properties
  • 相关阅读:
    eth私有节点搭建
    angularjs 文件类型格式校验
    Windows Server 2019, 2016 or 2012 安装telnet
    Oracle导致Redo日志暴增的SQL语句排查
    log4j waf 绕过技巧
    jtemplate使用笔记
    NPOI2.0
    关于java同一个类互相调用,spring事务失效问题
    java 将redis的数据批量取出来的时候遇到json格式转换异常
    超级好用的文档转换工具
  • 原文地址:https://www.cnblogs.com/zhuxiang1633/p/8631701.html
Copyright © 2011-2022 走看看