首页 > 代码库 > spring+mybatis+springmvc的配置

spring+mybatis+springmvc的配置

1.web.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    
    <!-- 上下文参数(第一启动),指定jdbc配置文件位置  -->
    <context-param>
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:/config/jdbc.xml</param-value>  
      </context-param>     
     
    <!-- spring监听器(第二启动),监听springMvc环境 -->
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
    
    <!-- springMvc编码拦截器(第三启动),springMvc内置的编码拦截器 -->
    <filter>  
        <filter-name>encodingFilter</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
    </filter>  
     
    <filter-mapping>  
        <filter-name>encodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>
      
      <!-- springMvc前置总控制器(第四启动),在分发其它的控制器前都要经过这个总控制器 -->
    <servlet>  
        <servlet-name>spring-mvc</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <init-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:/config/springmvc.xml</param-value>  
    </init-param>  
    <load-on-startup>2</load-on-startup>  
    </servlet>  
     
    <servlet-mapping>  
        <servlet-name>spring-mvc</servlet-name>  
        <url-pattern>*.do</url-pattern>  
    </servlet-mapping>  
    
    <!-- 网站的默认首页   -->
    <welcome-file-list>
        <welcome-file>begin.jsp</welcome-file>
    </welcome-file-list>
</web-app>

2.spring 的配置文件applicationContext-mybatis.xml

<beans xmlns="http://www.springframework.org/schema/beans"    xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"    xmlns:aop="http://www.springframework.org/schema/aop"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd     http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"    default-lazy-init="true">    <context:component-scan base-package="com.ltf.springmvc" />    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"        lazy-init="false">        <property name="dataSource" ref="dataSource" />        <property name="mapperLocations" value="classpath:sqlmapper/*Mapper.xml" />        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />    </bean>    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"        scope="prototype">        <constructor-arg index="0" ref="sqlSessionFactory" />    </bean>    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="com.ltf.springmvc.dao" />        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>    </bean>    <mybatis-spring:scan base-package="com.ltf.springmvc.dao" /></beans>

3.jdbc的配置

<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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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">    <!-- 引入jdbc配置文件 -->    <context:property-placeholder location="classpath:config/jdbc.properties" />    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"        destroy-method="close">        <property name="driverClassName">            <value>${jdbc_driverClassName}</value>        </property>        <property name="url">            <value>${jdbc_url}</value>        </property>        <property name="username">            <value>${jdbc_username}</value>        </property>        <property name="password">            <value>${jdbc_password}</value>        </property>        <!-- 连接池最大使用连接数 -->        <property name="maxActive">            <value>20</value>        </property>        <!-- 初始化连接大小 -->        <property name="initialSize">            <value>1</value>        </property>        <!-- 获取连接最大等待时间 -->        <property name="maxWait">            <value>60000</value>        </property>        <!-- 连接池最大空闲 -->        <property name="maxIdle">            <value>20</value>        </property>        <!-- 连接池最小空闲 -->        <property name="minIdle">            <value>3</value>        </property>        <!-- 自动清除无用连接 -->        <property name="removeAbandoned">            <value>true</value>        </property>        <!-- 清除无用连接的等待时间 -->        <property name="removeAbandonedTimeout">            <value>180</value>        </property>        <!-- 连接属性 -->        <property name="connectionProperties">            <value>clientEncoding=UTF-8</value>        </property>    </bean>
  <!-- 数据源事务管理 -->    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource" />    </bean>        <!-- 自动扫描组件,需要把controller去掉,否则影响事务管理 -->      <context:component-scan base-package="com.springmvc">        <context:exclude-filter type="regex" expression="com.spring.web.*" />    </context:component-scan>     

</beans>

4.jdbc.properties

jdbc_driverClassName=com.mysql.jdbc.Driverjdbc_url=jdbc:mysql://localhost:3306/demo?useUnicode=true&amp;characterEncoding=utf-8jdbc_username=rootjdbc_password=123456

5.springmvc配置

<?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:p="http://www.springframework.org/schema/p"         xmlns:context="http://www.springframework.org/schema/context"         xmlns:mvc="http://www.springframework.org/schema/mvc"         xsi:schemaLocation="                 http://www.springframework.org/schema/beans                 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd                 http://www.springframework.org/schema/context                 http://www.springframework.org/schema/context/spring-context-3.0.xsd                http://www.springframework.org/schema/mvc                 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">              <!-- 启动时扫描所有的controller -->    <context:component-scan base-package="com.springmvc.web" />        <!--  主要作用于@Controller,激活该模式,下面是一种简写形式,完全可以手动配置替代这种简写形式,它会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter,是spring MVC为@Controllers分发请求所必须的   -->    <mvc:annotation-driven />        <!-- 配置js,css等静态文件直接映射到对应的文件夹,不被DispatcherServlet处理 -->    <mvc:resources location="/WEB-INF/resources/**" mapping="/resources" />        <!-- jsp页面解析器,当Controller返回XXX字符串时,先通过拦截器,然后该类就会在/WEB-INF/views/目录下,查找XXX.jsp文件 -->      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/views/" />        <property name="suffix" value=".jsp" />    </bean>   </beans>

 

spring+mybatis+springmvc的配置