`

搭建基于全注解的Spring+Spring MVC+MyBatis框架

阅读更多

以实例讲解Spring+Spring MVC+MyBatis框架的搭建步骤:

 

一、配置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">
 
 <!-- 配置log4j日志   -->
 <context-param>
  <param-name>log4jConfigLocation</param-name>
  <param-value>/WEB-INF/classes/log4j.properties</param-value>
 </context-param>
 <listener>
  <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
 </listener>
 
 <!-- 字符集过滤器 -->
 <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>
 
 <!-- Spring view分发器(中心控制器) -->
 <servlet>
  <servlet-name>dispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <!-- 初始化参数   -->
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/classes/spring/*.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>dispatcher</servlet-name>
  <!-- 表示拦截所有/XX的请求  -->
  <url-pattern>/</url-pattern>
 </servlet-mapping>
 
 <!-- 激活静态资源的默认配置,解决Rest风格兼容 -->
 <servlet-mapping>
  <servlet-name>default</servlet-name>
  <url-pattern>*.css</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 <servlet-name>default</servlet-name>
  <url-pattern>*.js</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
  <servlet-name>default</servlet-name>
  <url-pattern>*.gif</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
  <servlet-name>default</servlet-name>
  <url-pattern>*.jpg</url-pattern>
 </servlet-mapping>
 
 <welcome-file-list>
  <!--<welcome-file>/index.jsp</welcome-file>-->
  <welcome-file></welcome-file>
 </welcome-file-list>
 
</web-app>

二、配置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:p="http://www.springframework.org/schema/p"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       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://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-2.5.xsd"
       default-init-method="init">    
     
    <!-- 引入jdbc配置文件 -->
    <context:property-placeholder location="classpath:jdbc.properties" />
    
    <!--创建jdbc数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
    </bean>
 
    <!-- 创建SqlSessionFactory,同时指定数据源 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 配置sql映射文件所在位置  注意:默认与mapper类位置相同   -->
        <property name="mapperLocations" value="classpath:sqlmap/*.xml" />
    </bean>
 
 	<!-- 配置事务管理器,全注解声明式事务  -->
    <!--
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" />
    -->
	
	<!-- 配置事务管理器,使用tx标签配置的拦截器声明式事务  -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 配置事务的传播特性  -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
        	<!-- 
        		REQUIRED:如果存在一个事务,则支持当前事务;如果没有事务,则开启一个新事务。
        		NESTED:如果当前存在事务,则在嵌套事务内执行;如果当前没有事务,则开启一个新事务。
			-->
            <tx:method name="add*" propagation="NESTED" />
            <tx:method name="del*" propagation="NESTED" />
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>
    <!-- 配置事务的切入点  -->
    <aop:config>
        <aop:pointcut id="targetMethod" expression="execution(* com.xieke.test.service.impl.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="targetMethod" />
    </aop:config>
 
    <!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.xieke.test.mapper" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        <!-- 默认情况下会自动注入id=sqlSessionFactory的bean,也可以按上述方式指定sqlSessionFactory -->
    </bean>
</beans>

三、配置dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:aop="http://www.springframework.org/schema/aop" 
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" 
       xmlns:tx="http://www.springframework.org/schema/tx" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
        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 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
        
        <!-- 自动扫描注解组件并且自动注入 -->
   		<context:component-scan base-package="com.xieke.test" />
        
        <!--视图解析器-->
	    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	        <property name="prefix" value="/WEB-INF/jsp/" />
	        <property name="suffix" value=".jsp" />      
	    </bean>
	    
	    <!-- 激活spring mvc的注解驱动  -->
	    <mvc:annotation-driven/>
	    <!-- 配置spring mvc的默认首页  -->
	    <mvc:view-controller path="/" view-name="redirect:/car/index" />
</beans>

 

   至此搭建完成,以上配置都是主要配置,详细搭建过程请下载源码:http://pan.baidu.com/s/1bn4pkwn

   转载请注明出处: http://xieke90.iteye.com/blog/2241054

   

 

1
3
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics