SpringMVC+Spring+Mybatis整合

niCong 2020-09-27 原文


SpringMVC+Spring+Mybatis整合

  1. SpringMVC+Spring+Mybatis整合

maven工程结构:

 

pom文件的依赖:

 <dependencies>
        <!–mybatis环境–>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.3</version>
        </dependency>
        <!–mysql环境–>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <!–spring整合jdbc–>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <!–spring整合mybatis–>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.3</version>
        </dependency>
        <!–druid连接池–>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.16</version>
        </dependency>
        <!–分页插件坐标–>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.2</version>
        </dependency>
        <!–springmvc环境–>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <!–jackson相关坐标3个–>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.0</version>
        </dependency>
        <!–servlet环境–>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!–其他组件–>
        <!–junit单元测试–>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!–spring整合junit–>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
    </dependencies>

配置文件:

spring和mybatis的整合

  • 配置service层包扫描
  • 配置连接池数据源
  • 配置mybatis的核心类(用于整合)
  • 配置mybatis的映射扫描器(用于整合)
  • 配置mybatis的分页插件
  • 配置事务管理

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans”
       xmlns:context=”http://www.springframework.org/schema/context”
       xmlns:tx=”http://www.springframework.org/schema/tx”
       xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
       xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd”>
    <!–主程序对应的配置文件–>

    <!–开启bean注解扫描–>
    <context:component-scan base-package=”com.XXX.service”/>

    <!–加载properties文件–>
    <context:property-placeholder location=”classpath*:jdbc.properties”/>

    <!–数据源–>
    <bean id=”dataSource” class=”com.alibaba.druid.pool.DruidDataSource”>
        <property name=”driverClassName” value=”${jdbc.driver}”/>
        <property name=”url” value=”${jdbc.url}”/>
        <property name=”username” value=”${jdbc.username}”/>
        <property name=”password” value=”${jdbc.password}”/>
    </bean>

    <!–整合mybatis到spring中–>
    <bean class=”org.mybatis.spring.SqlSessionFactoryBean”>
        <property name=”dataSource” ref=”dataSource”/>
        <property name=”typeAliasesPackage” value=”com.itheima.domain”/>
        <property name=”plugins”><!–分页插件–>
            <array>
                <bean class=”com.github.pagehelper.PageInterceptor”>
                    <property name=”properties”>
                        <props>
                            <prop key=”helperDialect”>mysql</prop>
                            <prop key=”reasonable”>true</prop>
                        </props>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

    <!–映射扫描–>
    <bean class=”org.mybatis.spring.mapper.MapperScannerConfigurer”>
        <property name=”basePackage” value=”com.itheima.dao”/>
    </bean>

    <!–开启注解式事务–>
    <tx:annotation-driven transaction-manager=”txManager”/>

    <!–事务管理器–>
    <bean id=”txManager” class=”org.springframework.jdbc.datasource.DataSourceTransactionManager”>
        <property name=”dataSource” ref=”dataSource”/>
    </bean>
</beans>

 

SpringMVC环境配置

  • 注解的扫描
  • 处理器适配器和处理器映射器
  • 释放静态资源

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans”
       xmlns:context=”http://www.springframework.org/schema/context”
       xmlns:mvc=”http://www.springframework.org/schema/mvc”
       xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
       xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd”>

    <mvc:annotation-driven/>

    <context:component-scan base-package=”com.xxxx.controller”/>

    <mvc:default-servlet-handler/>
</beans>

注意:   <mvc:default-servlet-handler/>这个释放静态资源路不配置也是可以的 在web.xml中可以拦截*.do的请求是一样的效果

web.xml文件配置(1+1+1

 

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app xmlns=”http://xmlns.jcp.org/xml/ns/javaee”
         xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
         xsi:schemaLocation=”http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd”
         version=”3.1″>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext.xml</param-value>
    </context-param>
    <!–启动服务器时,通过监听器加载spring运行环境–>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</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>

    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:spring-mvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>(这里就可以写*.do那么就不需要配置释放静态资源了)
    </servlet-mapping>

</web-app>

到此再写dao service controller 层就可以了  访问数据库测试一下就完成了ssm的基础搭建

 

发表于
2020-09-27 19:07 
statics 
阅读(0
评论(0
编辑 
收藏

 

版权声明:本文为niCong原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/niCong/p/13741299.html

SpringMVC+Spring+Mybatis整合的更多相关文章

随机推荐

  1. 有关于腾讯地图服务端IP定位接口的获取当前城市的处理

    接口说明:http://apis.map.qq.com/ws/location/v1/ip 说明里面写了ip可 […]...

  2. 2021年有哪些优秀的免费PSD样机素材下载的网站?

    2021年有哪些优秀的免费PSD样机素材下载的网站? 无论是ui设计师还是平面设计师,为避免无休止的加班,平时 […]...

  3. Java实现BF算法

    package 串的算法; public class BF { public static void main […]...

  4. Linux服务器(Centos)上安装jexus

      哈子是Jexus Jexus是一款Linux平台上的高性能WEB服务器和负载均衡网关,Jexus Web […]...

  5. JIRA官方:JIRA亮点介绍

    操作超级简单 简单不意味着要以牺牲功能作为代价。JIRA提供了友好、直观的可配置的Web界面,并支持大量的快捷 […]...

  6. ApplicationInsights迁移国内躺的坑

    由于之前有段时间访问国际版的Application Insights实在是慢,而且又不是每个人都有梯子,然后国 […]...

  7. 数据结构与算法 —— 链表linked list(04)

    我们在上篇文章里面提到了链表的翻转,给定一个链表,对每两个相邻的节点作交换,并返回头节点,今天的这道题是它的升 […]...

  8. 前后端分离初体验一:前端环境搭建

    参考:https://www.bilibili.com/video/BV137411B7vB B站UP:楠哥教 […]...

展开目录

目录导航