程序员人生 网站导航

06_MyBatis,Spring,SpringMVC整合

栏目:php教程时间:2015-01-28 09:05:25

  1. 项目结构

  1. Spring的配置:

beans.xml

<?xml version="1.0" encoding="UTF⑻"?>

<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"

    xmlns:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

       http://www.springframework.org/schema/beans/spring-beans⑶.0.xsd

       http://www.springframework.org/schema/mvc

       http://www.springframework.org/schema/mvc/spring-mvc⑶.0.xsd

       http://www.springframework.org/schema/context

       http://www.springframework.org/schema/context/spring-context⑶.0.xsd

       http://www.springframework.org/schema/aop

       http://www.springframework.org/schema/aop/spring-aop⑶.0.xsd

       http://www.springframework.org/schema/tx

       http://www.springframework.org/schema/tx/spring-tx⑶.0.xsd ">                      

      

       <context:component-scan base-package="com.rl"/>

      

       <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

           <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>

           <property name="url" value="jdbc:mysql://localhost:3306/mybatis"></property>

           <property name="username" value="root"></property>

           <property name="password" value="123456"></property>

       </bean>

      

       <!--

           使用spring来管理sqlSessionFactory

        -->

       <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

           <property name="configLocation" value="classpath:sqlMapConfig.xml"></property>

           <property name="dataSource" ref="dataSource"></property>

       </bean>

      

       <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

           <property name="dataSource" ref="dataSource"></property>

       </bean>

      

       <tx:advice id="txAdvice" transaction-manager="txManager">

           <tx:attributes>

              <tx:method name="save*" propagation="REQUIRED"/>

              <tx:method name="update*" propagation="REQUIRED"/>

              <tx:method name="delete*" propagation="REQUIRED"/>

              <tx:method name="select*" read-only="true"/>

           </tx:attributes>

       </tx:advice>

      

       <aop:config>

           <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.rl.service..*.*(..))"/>

       </aop:config>

</beans>

  1. springmvc.xml的配置内容以下:

<?xml version="1.0" encoding="UTF⑻"?>

<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"

    xmlns:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

       http://www.springframework.org/schema/beans/spring-beans⑶.0.xsd

       http://www.springframework.org/schema/mvc

       http://www.springframework.org/schema/mvc/spring-mvc⑶.0.xsd

       http://www.springframework.org/schema/context

       http://www.springframework.org/schema/context/spring-context⑶.0.xsd

       http://www.springframework.org/schema/aop

       http://www.springframework.org/schema/aop/spring-aop⑶.0.xsd

       http://www.springframework.org/schema/tx

       http://www.springframework.org/schema/tx/spring-tx⑶.0.xsd ">                       

      

       <context:component-scan base-package="com.rl.controller"/>

       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

           <property name="prefix" value="/WEB-INF/jsp/"></property>

           <property name="suffix" value=".jsp"></property>

       </bean>

</beans>

  1. web.xml的配置内容以下:

<?xml version="1.0" encoding="UTF⑻"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

    id="WebApp_ID" version="2.5">

   

    <listener>

       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

    <context-param>

       <param-name>contextConfigLocation</param-name>

       <param-value>classpath:beans.xml</param-value>

    </context-param>

   

    <servlet>

       <servlet-name>springmvc</servlet-name>

       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

       <init-param>

           <param-name>contextConfigLocation</param-name>

           <param-value>classpath:springmvc.xml</param-value>

       </init-param>

    </servlet>

    <servlet-mapping>

       <servlet-name>springmvc</servlet-name>

       <url-pattern>*.do</url-pattern>

    </servlet-mapping>

   

    <filter>

        <filter-name>SpringCharacterEncodingFilter</filter-name>

        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

        <init-param>

------分隔线----------------------------
------分隔线----------------------------

最新技术推荐

1Java并发编程的艺术(六)——线程间的通信 2Java并发编程的艺术(三)——volatile 3Node核心模块之Events 4myeclipse源码相关操作 5springMVC源码分析--RequestParamMethodArgumentResolver参数解析器(三) 6Java基本数据类型总结 7java结合testng,利用XML做数据源的数据驱动示例