DMOZ中文网站分类目录-免费收录各类优秀网站的中文网站目录.
  • DmozDir.org
DMOZ目录快速登录入口-免费收录各类优秀网站的中文网站目录.由人工编辑,并提供网站分类目录检索及地区分类目录检索,是站长免费推广网站的有力平台!

SpringMVC+Spring+Mybatis整合

  • SpringMVC+Spring+Mybatis整合

  • 已被浏览: 40 次2020年09月27日    来源:  https://www.cnblogs.com/niCong/p/13741299.html
  • maven工程结构: pom文件的依赖:dependencies !--mybatis环境-- dependency groupId org.mybatis /groupId artifactId mybatis /ar
    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的基础搭建

     


    以上信息来源于网络,如有侵权,请联系站长删除。

    TAG:springmvc Spring mybatis

  • 上一篇:法官设置在特朗普的TikTok下载禁令治
  • 与“SpringMVC+Spring+Mybatis整合”相关的资讯
  • Spring Boot @Enable*注解源码解析及自定义@Enable*
  • Spring Cloud实战 | 第十一篇:Spring Cloud Gateway网关实现对RESTful接口权限和按钮权限细粒度控制
  • Mybatis笔记
  • mybatis Sql语句配置详解
  • Spring 的循环依赖,源码详细分析 → 真的非要三级缓存吗