• spring整合springmvc和mybatis【jar,xml】


    1.spring

    1.1 jar包

     1.2 spring基本配置,包扫描注解

        <!-- 自动扫描 -->
        <context:component-scan base-package="com.getword">
            <!-- 扫描时跳过 @Controller 注解的JAVA类(控制器) -->
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>

    2 mybatis

    2.1 jar包

    额外的jar包:

    cjlib字节码增强、fileupload、io增强等

    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"
           xmlns:mybatis="http://mybatis.org/schema/mybatis-spring" xmlns:p="http://www.springframework.org/schema/p"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                                  http://www.springframework.org/schema/beans/spring-beans.xsd
                                  http://www.springframework.org/schema/aop 
                                  http://www.springframework.org/schema/aop/spring-aop.xsd
                                  http://www.springframework.org/schema/context 
                                  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">
    
        <!-- mybatis:scan会扫描com.getword.dao包里的所有接口当作Spring的bean配置,之后可以进行依赖注入-->
        <mybatis:scan base-package="com.getword.dao"/>
    
        <!-- 扫描com.getword包下面的java文件,有Spring的相关注解的类,则把这些类注册为Spring的bean -->
        <!--<context:component-scan base-package="com.getword"/>-->
    
        <!-- 自动扫描 -->
        <context:component-scan base-package="com.getword">
            <!-- 扫描时跳过 @Controller 注解的JAVA类(控制器) -->
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>
    
        <!-- 使用PropertyOverrideConfigurer后处理器加载数据源参数 -->
        <context:property-override location="classpath:db.properties"/>
    
        <!-- 配置c3p0数据源 -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"/>
    
        <!-- 2.配置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>
    
        <!-- JDBC事务管理器 -->
        <bean id="transactionManager"
              class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
              p:dataSource-ref="dataSource"/>
    
        <!-- 启用支持annotation注解方式事务管理 -->
        <tx:annotation-driven transaction-manager="transactionManager"  proxy-target-class="true"/>
    
    
        <!---文件下载,处理器-->
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <property name="defaultEncoding" value="UTF-8"/>
            <!-- maxUploadSize设置-1 不限制文件大小 -->
            <property name="maxUploadSize" value="-1"/>
        </bean>
    
    </beans>
    View Code

     3 spring mvc 

    3.1 fastjson的使用

    配置:

       <!-- 设置配置方案 -->
        <mvc:annotation-driven>
            <!-- 设置不使用默认的消息转换器 -->
            <mvc:message-converters register-defaults="false">
                <!-- 配置Spring的转换器 -->
                <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
                <bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"/>
                <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
                <bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter"/>
                <!-- 配置fastjson中实现HttpMessageConverter接口的转换器 -->
                <bean id="fastJsonHttpMessageConverter"
                      class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                    <!-- 加入支持的媒体类型:返回contentType -->
                    <property name="supportedMediaTypes">
                        <list>
                            <!-- 这里顺序不能反,一定先写text/html,不然ie下会出现下载提示 -->
                            <value>text/html;charset=UTF-8</value>
                            <value>application/json;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
            </mvc:message-converters>
        </mvc:annotation-driven>

    对于返回String类型的数据乱码:

    <!-- 配置Spring的转换器, 字符编码 -->
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <constructor-arg value="UTF-8" index="0"/>
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/plain;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>

     web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             version="3.1">
        <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>
        <!--前端控制器-->
        <servlet>
            <servlet-name>sprintmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springmvc-config.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>sprintmvc</servlet-name>
            <url-pattern>*.do</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>
    View Code

    end

  • 相关阅读:
    Eclipse+EPIC+PadWalker
    Commit message 和 Change log 编写指南
    把perl脚本编译成exe
    Qt使用中碰到的问题
    Python——函数 7、位置参数与默认参数之间的关系
    Python——函数 6、默认参数
    Python——函数 5、位置参数与关键字参数
    Python——函数 4、形参的应用
    Python——函数 3、实参与形参
    Python——函数 2、返回值
  • 原文地址:https://www.cnblogs.com/zhuxiang1633/p/9837713.html
Copyright © 2020-2023  润新知