整合前的准备工作
image.png

image.png

image.png

image.png

image.png

对于pom.xml先删除不必要的

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.slin.edu</groupId>
  6. <artifactId>ssm</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <packaging>war</packaging>
  9. <name>ssm Maven Webapp</name>
  10. <!-- FIXME change it to the project's website -->
  11. <url>http://www.example.com</url>
  12. <properties>
  13. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  14. <maven.compiler.source>11</maven.compiler.source>
  15. <maven.compiler.target>11</maven.compiler.target>
  16. </properties>
  17. <dependencies>
  18. <!--单元测试jar-->
  19. <dependency>
  20. <groupId>junit</groupId>
  21. <artifactId>junit</artifactId>
  22. <version>4.12</version>
  23. <scope>test</scope>
  24. </dependency>
  25. <!--spring-data-jpa 需要引⼊的jar,start-->
  26. <dependency>
  27. <groupId>org.springframework.data</groupId>
  28. <artifactId>spring-data-jpa</artifactId>
  29. <version>2.1.8.RELEASE</version>
  30. </dependency>
  31. <dependency>
  32. <groupId>javax.el</groupId>
  33. <artifactId>javax.el-api</artifactId>
  34. <version>3.0.1-b04</version>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.glassfish.web</groupId>
  38. <artifactId>javax.el</artifactId>
  39. <version>2.2.6</version>
  40. </dependency>
  41. <!--spring-data-jpa 需要引⼊的jar,end-->
  42. <!--spring 相关jar,start-->
  43. <dependency>
  44. <groupId>org.springframework</groupId>
  45. <artifactId>spring-aop</artifactId>
  46. <version>5.1.12.RELEASE</version>
  47. </dependency>
  48. <dependency>
  49. <groupId>org.aspectj</groupId>
  50. <artifactId>aspectjweaver</artifactId>
  51. <version>1.8.13</version>
  52. </dependency>
  53. <dependency>
  54. <groupId>org.springframework</groupId>
  55. <artifactId>spring-context</artifactId>
  56. <version>5.1.12.RELEASE</version>
  57. </dependency>
  58. <dependency>
  59. <groupId>org.springframework</groupId>
  60. <artifactId>spring-context-support</artifactId>
  61. <version>5.1.12.RELEASE</version>
  62. </dependency>
  63. <!--spring对orm框架的⽀持包-->
  64. <dependency>
  65. <groupId>org.springframework</groupId>
  66. <artifactId>spring-orm</artifactId>
  67. <version>5.1.12.RELEASE</version>
  68. </dependency>
  69. <dependency>
  70. <groupId>org.springframework</groupId>
  71. <artifactId>spring-beans</artifactId>
  72. <version>5.1.12.RELEASE</version>
  73. </dependency>
  74. <dependency>
  75. <groupId>org.springframework</groupId>
  76. <artifactId>spring-core</artifactId>
  77. <version>5.1.12.RELEASE</version>
  78. </dependency>
  79. <!--spring 相关jar,end-->
  80. <!--hibernate相关jar包,start-->
  81. <dependency>
  82. <groupId>org.hibernate</groupId>
  83. <artifactId>hibernate-core</artifactId>
  84. <version>5.4.0.Final</version>
  85. </dependency>
  86. <!--hibernate对jpa的实现jar-->
  87. <dependency>
  88. <groupId>org.hibernate</groupId>
  89. <artifactId>hibernate-entitymanager</artifactId>
  90. <version>5.4.0.Final</version>
  91. </dependency>
  92. <dependency>
  93. <groupId>org.hibernate</groupId>
  94. <artifactId>hibernate-validator</artifactId>
  95. <!-- 配置 Spring 的配置⽂件-->
  96. <version>5.4.0.Final</version>
  97. </dependency>
  98. <!--hibernate相关jar包,end-->
  99. <!--mysql 数据库驱动jar-->
  100. <dependency>
  101. <groupId>mysql</groupId>
  102. <artifactId>mysql-connector-java</artifactId>
  103. <version>5.1.46</version>
  104. </dependency>
  105. <!--druid连接池-->
  106. <dependency>
  107. <groupId>com.alibaba</groupId>
  108. <artifactId>druid</artifactId>
  109. <version>1.1.21</version>
  110. </dependency>
  111. <!--spring-test-->
  112. <dependency>
  113. <groupId>org.springframework</groupId>
  114. <artifactId>spring-test</artifactId>
  115. <version>5.1.12.RELEASE</version>
  116. </dependency>
  117. </dependencies>
  118. <build>
  119. <plugins>
  120. <plugin>
  121. <groupId>org.apache.maven.plugins</groupId>
  122. <artifactId>maven-compiler-plugin</artifactId>
  123. <version>3.6.1</version>
  124. <configuration>
  125. <source>11</source>
  126. <target>11</target>
  127. </configuration>
  128. </plugin>
  129. <!--要把tomcat的插件引入进来-->
  130. <plugin>
  131. <groupId>org.apache.tomcat.maven</groupId>
  132. <artifactId>tomcat7-maven-plugin</artifactId>
  133. <version>2.2</version>
  134. <configuration>
  135. <port>8080</port><!--指定端口号-->
  136. <path>/</path><!--指定上下文-->
  137. </configuration>
  138. </plugin>
  139. </plugins>
  140. </build>
  141. </project>

开发相应的dao层(回顾Mybatis)

1、添加类和接口

image.png

2、创建一个和接口一样路径及名字的xml文件。

1、xml的命名空间和接口的全限定类名一致
2、sql语句的id要和方法名一致
image.png

开发相应的service层(回顾Spring)

image.png

配置Spring的配置文件

整合目标 数据库连接池以及事务管理都交给Spring容器来完成

image.png

SqlSessionFactory对象应该放到Spring容器中作为单例对象管理 (任务一第三点)

SSM整合 - 图10

原来的sqlMapConfig.xml就是上面那样,需要数据源,和Mapper.xml的全路径
image.png

Mapper动态代理对象交给Spring管理,我们从Spring容器中直接获得Mapper的代理对象

image.png

完整的applicationContext.xml文件

<?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
">

     <!--包扫描-->
    <context:component-scan base-package="com.slin.edu"/>

    <!--整合目标 数据库连接池以及事务管理都交给Spring容器来完成-->
    <!--引入外部资源文件-->
    <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>

     <!--事物管理-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--添加注解驱动(才可以在添加注解事物,才可以到service层添加事物控制)-->
    <tx:annotation-driven transaction-manager="transactionManager"/>


    <!--SqlSessionFactory对象应该放到Spring容器中作为单例对象管理
     原来mybaits中sqlSessionFactory的构建是需要素材的:SqlMapConfig.xml中的内容
    -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--别名映射扫描-->
        <property name="typeAliasesPackage" value="com.slin.edu.pojo"/>
        <!--数据源dataSource-->
        <property name="dataSource" ref="dataSource"/>
     </bean>


    <!--Mapper动态代理对象交给Spring管理,我们从Spring容器中直接获得Mapper的代理对象
       Mybatis中是通过工厂来产生会话,通过会话获取getMapper代理对象
     -->
    <!--通过扫描器,扫描mapper接口,成代理对象存储在ioc容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--需要扫描的是哪个包。也就是mapper接口包路径,mapper包下面都会扫描生成代理对象存在ioc中-->
        <property name="basePackage" value="com.slin.edu.mapper"/>
        <!--以前是通过工厂产生会话,通过会话生成代理对象。现在也还是需要去获取代理对象,通过sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName"  value="sqlSessionFactory"/>
    </bean>


</beans>

测试类

image.png

到此Mybatis和Spring的整合OK

接下来将SpringMVC整合进来

把SpringMVC的入门案例整合进来即可(在已有工程基础之上开发一个SpringMVC入门案例)

1、先在pom.xml引入坐标

     <!--SpringMVC-->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>5.1.12.RELEASE</version>
      </dependency>
      <!--jsp-api&servlet-api-->
      <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>jsp-api</artifactId>
          <version>2.0</version>
          <scope>provided</scope>
      </dependency>
      <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.1.0</version>
          <scope>provided</scope>
      </dependency>
      <!--页面使用jstl表达式-->
      <dependency>
          <groupId>jstl</groupId>
          <artifactId>jstl</artifactId>
          <version>1.2</version>
      </dependency>
      <dependency>
          <groupId>taglibs</groupId>
          <artifactId>standard</artifactId>
          <version>1.1.2</version>
      </dependency>

      <!--json数据交互所需jar,start-->
      <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-core</artifactId>
          <version>2.9.0</version>
      </dependency>
      <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-databind</artifactId>
          <version>2.9.0</version>
      </dependency>
      <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-annotations</artifactId>
          <version>2.9.0</version>
      </dependency>
      <!--json数据交互所需jar,end-->

2、在web.xml中添加DispatcherServlet

image.png

3、springmvc.xml文件的配置

image.png

4、Controller层中调用service层。

image.png

5、将Spring框架启动,实现ssm三个框架启动

SpringMVC是如何启动呢?
是通过web.xml中的Servlet初始化,加载springmvc.xml文件。
在springmvc.xml中只是添加了Controller的扫描。
service,和dao的扫描是Spring在扫,但是Spring没启动。如果要Spring启动,需要
在servlet中还要配置上一个监听器,
image.png

6、部署发布

image.png

将applicationContext.xml拆分为dao层和service层

为了让后期管理更方便可以

<?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
">

    <!--包扫描-->
    <context:component-scan base-package="com.slin.edu.service"/>

     <!--事物管理-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--添加注解驱动(才可以在添加注解事物,才可以到service层添加事物控制)-->
    <tx:annotation-driven transaction-manager="transactionManager"/>


</beans>
<?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
">

    <!--包扫描-->
    <context:component-scan base-package="com.slin.edu.mapper"/>

    <!--整合目标 数据库连接池以及事务管理都交给Spring容器来完成-->
    <!--引入外部资源文件-->
    <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对象应该放到Spring容器中作为单例对象管理
     原来mybaits中sqlSessionFactory的构建是需要素材的:SqlMapConfig.xml中的内容
    -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--别名映射扫描-->
        <property name="typeAliasesPackage" value="com.slin.edu.pojo"/>
        <!--数据源dataSource-->
        <property name="dataSource" ref="dataSource"/>
     </bean>


    <!--Mapper动态代理对象交给Spring管理,我们从Spring容器中直接获得Mapper的代理对象
       Mybatis中是通过工厂来产生会话,通过会话获取getMapper代理对象
     -->
    <!--通过扫描器,扫描mapper接口,成代理对象存储在ioc容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--需要扫描的是哪个包。也就是mapper接口包路径,mapper包下面都会扫描生成代理对象存在ioc中-->
        <property name="basePackage" value="com.slin.edu.mapper"/>
        <!--以前是通过工厂产生会话,通过会话生成代理对象。现在也还是需要去获取代理对象,通过sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName"  value="sqlSessionFactory"/>
    </bean>


</beans>