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

简介: 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_26654727/article/details/70161145 Sprin...
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_26654727/article/details/70161145

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

目录
相关文章
|
17天前
|
前端开发 Java Apache
Springboot整合shiro,带你学会shiro,入门级别教程,由浅入深,完整代码案例,各位项目想加这个模块的人也可以看这个,又或者不会mybatis-plus的也可以看这个
本文详细讲解了如何整合Apache Shiro与Spring Boot项目,包括数据库准备、项目配置、实体类、Mapper、Service、Controller的创建和配置,以及Shiro的配置和使用。
136 1
Springboot整合shiro,带你学会shiro,入门级别教程,由浅入深,完整代码案例,各位项目想加这个模块的人也可以看这个,又或者不会mybatis-plus的也可以看这个
|
9天前
|
Java 关系型数据库 MySQL
springboot学习五:springboot整合Mybatis 连接 mysql数据库
这篇文章是关于如何使用Spring Boot整合MyBatis来连接MySQL数据库,并进行基本的增删改查操作的教程。
14 0
springboot学习五:springboot整合Mybatis 连接 mysql数据库
|
11天前
|
Java 数据库连接 API
springBoot:后端解决跨域&Mybatis-Plus&SwaggerUI&代码生成器 (四)
本文介绍了后端解决跨域问题的方法及Mybatis-Plus的配置与使用。首先通过创建`CorsConfig`类并设置相关参数来实现跨域请求处理。接着,详细描述了如何引入Mybatis-Plus插件,包括配置`MybatisPlusConfig`类、定义Mapper接口以及Service层。此外,还展示了如何配置分页查询功能,并引入SwaggerUI进行API文档生成。最后,提供了代码生成器的配置示例,帮助快速生成项目所需的基础代码。
|
15天前
|
前端开发 Java 应用服务中间件
【Spring】Spring MVC的项目准备和连接建立
【Spring】Spring MVC的项目准备和连接建立
34 2
|
13天前
|
Java 数据库连接 Maven
Spring整合Mybatis
Spring整合Mybatis
|
1月前
|
消息中间件 NoSQL 安全
(转)Spring Boot加载 不同位置的 application.properties配置文件顺序规则
这篇文章介绍了Spring Boot加载配置文件的顺序规则,包括不同位置的application.properties文件的加载优先级,以及如何通过命令行参数或环境变量来指定配置文件的名称和位置。
|
5月前
|
Java 容器 Spring
Spring的加载配置文件、容器和获取bean的方式
Spring的加载配置文件、容器和获取bean的方式
51 3
Spring的加载配置文件、容器和获取bean的方式
|
XML Java 数据格式
Spring源码深度解析01-debug式看如何加载xml配置文件
Spring源码深度解析01-debug式看如何加载xml配置文件
147 0
|
Java Spring
Spring Boot - 花式加载配置文件
Spring Boot - 花式加载配置文件
88 0
Spring Boot - 花式加载配置文件
|
前端开发 Java Nacos
微服务架构 | *2.3 Spring Cloud 启动及加载配置文件源码分析(以 Nacos 为例)
Spring Cloud 要实现统一配置管理,需要解决两个问题:如何获取远程服务器配置和如何动态更新配置;在这之前,我们先要知道 Spring Cloud 什么时候给我们加载配置文件;
311 0
微服务架构 | *2.3 Spring Cloud 启动及加载配置文件源码分析(以 Nacos 为例)