• springMVC总结


    最近想了解下,弄了个最基本的dmeo看看。

    步骤如下:

    1. web.xml配置:

     1.1 首先定义spring的上下文:

    <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring.xml,classpath:spring-mybatis.xml</param-value>
    </context-param>

      加载监听器:

    <listener>
          <description>spring监听器</description>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

      这样在web项目启动之后,就会自动在加载classpath里定义的文件spring.xml和spring-mybatis.xml;

     1.2 定义一个springMVC的servlet:DispatcherServlet

      

    <servlet>
          <servlet-name>springMVC</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
              <discription>spring mvc 配置文件</discription>
              <param-name>contextConfigLocation</param-name>
              <param-value>classpath:spring-mvc.xml</param-value>
          </init-param>
          <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
          <servlet-name>springMVC</servlet-name>
          <url-pattern>/</url-pattern>
      </servlet-mapping>

    springMVC默认的配置文件名是:[servlet-name]-servlet.xml,这里我自定义为spring-mvc.xml通过classpath文件位置

    以上三个配置文件:spring.xml,spring-mybatis.xml,spring-mvc.xml都放在resources目录下

    2. 配置一个JDBC的属性文件:config.properties

     spring.xml:

     主要代码如下:

     

    <!-- cservice包(自动注入) -->
        <context:component-scan base-package="com.bx.service"/>
        <!-- 引入属性文件 -->
        <context:property-placeholder location="classpath:config.properties" />
        <!-- <bean class="com.comm.util.PropertiesUtil" /> -->
        <!-- 任务扫描 -->
        <task:annotation-driven/> 

     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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    ">
    
        <!-- JNDI方式配置数据源 -->
        <!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="${jndiName}"></property> </bean> -->
    
        <!-- 配置数据源 -->
        <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
            <property name="url" value="${jdbc_url}" />
            <property name="username" value="${jdbc_username}" />
            <property name="password" value="${jdbc_password}" />
    
            <!-- 初始化连接大小 -->
            <property name="initialSize" value="0" />
            <!-- 连接池最大使用连接数量 -->
            <property name="maxActive" value="20" />
            <!-- 连接池最小空闲 -->
            <property name="minIdle" value="0" />
            <!-- 获取连接最大等待时间 -->
            <property name="maxWait" value="60000" />
    
            <!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->
    
            <property name="validationQuery" value="${validationQuery}" />
            <property name="testOnBorrow" value="false" />
            <property name="testOnReturn" value="false" />
            <property name="testWhileIdle" value="true" />
    
            <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
            <property name="timeBetweenEvictionRunsMillis" value="60000" />
            <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
            <property name="minEvictableIdleTimeMillis" value="25200000" />
    
            <!-- 打开removeAbandoned功能 -->
            <property name="removeAbandoned" value="true" />
            <!-- 1800秒,也就是30分钟 -->
            <property name="removeAbandonedTimeout" value="1800" />
            <!-- 关闭abanded连接时输出错误日志 -->
            <property name="logAbandoned" value="true" />
    
            <!-- 监控数据库 -->
            <!-- <property name="filters" value="stat" /> -->
            <property name="filters" value="mergeStat" />
        </bean>
    
        <!-- mybatis文件 -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <!-- 自动扫描entity目录,省略Configuration.xml里手工配置 -->
            <property name="configLocation" value="classpath:mybatis-config.xml"></property>
            <property name="mapperLocations" value="classpath:mapper/*.xml" />
        </bean>
    
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.bx.dao" />
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        </bean>
    
    
    
        <!-- 配置事务管理器 -->
        <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
        </bean>
    
        <!-- 注解方式配置事物 -->
        <!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->
    
        <!-- 拦截器方式配置事物 -->
         <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="add*" propagation="REQUIRED" />
                <tx:method name="append*" propagation="REQUIRED" />
                <tx:method name="save*" propagation="REQUIRED" />
                <tx:method name="update*" propagation="REQUIRED" />
                <tx:method name="modify*" propagation="REQUIRED" />
                <tx:method name="edit*" propagation="REQUIRED" />
                <tx:method name="insert*" propagation="REQUIRED" />
                <tx:method name="delete*" propagation="REQUIRED" />
                <tx:method name="remove*" propagation="REQUIRED" />
                <tx:method name="confirm*" propagation="REQUIRED" />
                <tx:method name="start*" propagation="REQUIRED" />
                <tx:method name="sendMes" propagation="REQUIRED" />
                <tx:method name="read*" propagation="REQUIRED" />
                
                <tx:method name="get*" propagation="REQUIRED" read-only="true" />
                <tx:method name="find*" propagation="REQUIRED" read-only="true" />
                <tx:method name="load*" propagation="REQUIRED" read-only="true" />
                <tx:method name="search*" propagation="REQUIRED" read-only="true" />
                <tx:method name="datagrid*" propagation="REQUIRED" read-only="true" />
                
                <tx:method name="*" propagation="REQUIRED" />
            </tx:attributes>
        </tx:advice> 
        <aop:config>
            <aop:pointcut id="transactionPointcut" expression="execution(* com.bx.service..*Impl.*(..))" />
            <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
        </aop:config>
    
    </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:tx="http://www.springframework.org/schema/tx"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:aop="http://www.springframework.org/schema/aop" 
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:task="http://www.springframework.org/schema/task"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
            http://www.springframework.org/schema/task  
            http://www.springframework.org/schema/task/spring-task-4.0.xsd">
        
        <!-- 启用spring mvc注解 -->        
        <mvc:annotation-driven />
        <!-- controller包(自动注入) -->
        <context:component-scan base-package="com.bx.controller"/>
        
        <mvc:resources mapping="/resources/**" location="/resources/" />
        
        <!-- 对静态资源的访问 -->
        <mvc:default-servlet-handler/>
        
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    </beans>
  • 相关阅读:
    用UILocalNotification实现一个闹钟(Swift)
    Swift
    iOS判断一些权限是否被禁止
    ofbiz学习笔记
    POJ1062 昂贵的聘礼 【DFS】
    echarts 应用数个样例
    java 中缀转后缀(逆波兰)
    开放是否能让苹果成为智能家居的标准制定者?
    2015阿里校招前端在线题目
    hql中不能写count(1)能够写count(a.id)
  • 原文地址:https://www.cnblogs.com/xulzong/p/4797281.html
Copyright © 2020-2023  润新知