springMVC学习笔记SSM整合

Posted 拐柒

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springMVC学习笔记SSM整合相关的知识,希望对你有一定的参考价值。

SSM整合

SSM=spring+springmvc+mybatis=(spring+mybatis)+springmvc
先整合spring+mabatis
再整合springmvc

spring+mybatis

1、数据库连接池以及事务管理都叫给spring容器完成
2、sqlsessionfactory对象放入spring容器作为单例对象
3、mapper动态代理对象叫给spring管理,从spring容器中直接获得mapper的代理对象

applicationContext-dao.xml

<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
">
    <!--包扫描-->
    <context:component-scan base-package="com.lagou.edu.mapper"/>
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--第三⽅jar中的bean定义在xml中-->
    <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>
<!--    sqlSessionFactory
        原来mybatis中的sqlSessionFactory的构建是需要素材的:SqlMapConfig.xml中的内容
        -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--        别名映射-->
        <property name="typeAliasesPackage" value="com.lagou.edu.pojo"></property>
        <property name="dataSource" ref="dataSource"></property>
    </bean>
<!--    mapper动态代理对象叫给spring容器管理-->
<!--    扫描mapper接口,生成代理对象,存储在ioc容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--        扫描mapper包路径-->
        <property name="basePackage" value="com.lagou.edu.mapper"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
</beans>

applicationContext-service.xml

<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
">
    <!--包扫描-->
    <context:component-scan base-package="com.lagou.edu.service"/>
<!--    事务管理-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
<!--    事务管理注解驱动-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root

springmvc.xml

<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: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/context
 http://www.springframework.org/schema/context/spring-context.xsd
 http://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-mvc.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!--    扫描controller-->
    <context:component-scan base-package="com.lagou.edu.controller"></context:component-scan>
<!--    配置注解驱动,自动注册合适的handlermapper和handleradapter-->
    <mvc:annotation-driven></mvc:annotation-driven>
</beans>

以上是关于springMVC学习笔记SSM整合的主要内容,如果未能解决你的问题,请参考以下文章

SpringMVC-狂神(5.整合SSM框架)学习笔记

万字SpringMVC学习笔记总结(含SSM整合开发步骤)

万字SpringMVC学习笔记总结(含SSM整合开发步骤)

SpringMVC-整合SSM框架(狂神学习笔记)2021-10-03

1024程序员节RestFul,mvc拦截器,SSM基本整合-学习笔记

1024程序员节RestFul,mvc拦截器,SSM基本整合-学习笔记