码迷,mamicode.com
首页 > 编程语言 > 详细

Spring+SpringMVC +MyBatis整合配置文件案例66666

时间:2017-06-27 20:10:16      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:允许   ppi   cto   jdbc   缓存   data-   生成   cxf   location   

Spring+SpringMVC +MyBatis整合配置文件案例

标签: springspringmvcmybatismvcjava
技术分享 分类:

目录(?)[+]

Spring+SpringMVC +MyBatis整合配置文件案例

针对spring/SpringMVC/MyBatis三个框架的整合有很多的方式,经过最近的学习我来总结一下其配置文件的设置以及三大框架之间的一些关系.代码配置后面附上,仅作为建议.

三大框架之间的关系图如下:
技术分享

配置文件配置的对应关系:
技术分享

1.Spring配置文件

  • applicationContext.xml

    <context:component-scan base-package="service"/>
    <context:property-placeholder location="classpath:/c3p0.properties"/>
    
    <!-- 注册数据库的资源 -->
    <bean id="dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value = "${c3p0.driver}"></property>
        <property name="jdbcUrl" value = "${c3p0.url}"></property>
        <property name="user" value = "${c3p0.user}"></property>
        <property name="password" value = "${c3p0.password}"></property>
    </bean>
     <!-- 声明式的事务处理 -->
    <bean  id = "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="datasource"></property>
    </bean>
    
    <!-- 创建通知 配置切面和切入点-->
    <tx:advice id="advice">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="del*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>
    
    <aop:config >
        <aop:pointcut expression="execution(* servlet..*.*(..))" id="pc"/>
        <aop:advisor advice-ref="advice" pointcut-ref="pc"/>
    </aop:config>
    
    <!-- 整合MyBatis -->
    <bean id = "SqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref = "dataSource"></property>
    
        <!-- 导入核心配置文件 -->
            <property name="configLocation" value = "classpath:/sqlMapConfig.xml"></property>
    
        <!-- 导入映射文件 -->
        <property name="mapperLocations" value = "classpath:/pojo/*.xml"></property>
    </bean>
    
    <!-- spring为mapper接口创建代理对象 -->
    <bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer" >
        <property name="basePackage" value = "mapper"></property>
    </bean>
    

2.SpringMVC配置文件

  • applicationContext-mvc.xml

    1. <!-- 开启mvc注解 -->
    <mvc:annotation-driven />
    <context:component-scan base-package="controller"></context:component-scan>
    2. <!-- 内部资源视图管理器 -->
    <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/"></property>
        <property name="suffix" value = ".jsp"></property>
    </bean>
    

3.MyBatis配置文件

  • sqlMapConfig.xml

    1. MyBatis核心配置文件
    <configuration>
        <!-- 可以设置其缓存和其他一些事务-->
    </configuration>
    
  • UserMapper.xml

    <mapper namespace="mapper.UserMapper">
    
        <!-- 映射配置文件指定开启二级缓存 -->
    <cache/>
    
    <!-- 复用sql语句 -->
    <sql id="selectUser"> 
        select * from user
    </sql>
    
    
    <select id="findAll" resultType="pojo.User">
        <include refid="selectUser"/> 
    </select>
    

4.web.xml配置文件

  • web.xml

    1. <!--配置过滤器-->
    <filter>
        <filter-name>filter</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>filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <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:/applicationContext*.xml</param-value>
        </init-param>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
    

不太善于言辞,希望得到大家的支持,谢谢!

写于2017/04/13

Spring+SpringMVC +MyBatis整合配置文件案例66666

标签:允许   ppi   cto   jdbc   缓存   data-   生成   cxf   location   

原文地址:http://www.cnblogs.com/zftxdh/p/7086501.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!