SSM框架整合 Spring + Spring MVC + MyBatis
Spring MVC 负责实现 MVC 设计模式、MyBatis 负责数据持久层、Spring 负责管理 Spring MVC 和 MyBatis 相关对象的创建和依赖注入
 
整合流程 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 <?xml version="1.0" encoding="UTF-8"?> <project  xmlns ="http://maven.apache.org/POM/4.0.0"  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" >   <modelVersion > 4.0.0</modelVersion >    <packaging > war</packaging >    <name > ssmintegration</name >    <groupId > com.dream.xiaobo</groupId >    <artifactId > ssmintegration</artifactId >    <version > 1.0-SNAPSHOT</version >    <build >      <resources >        <resource >          <directory > src/main/java</directory >          <includes >            <include > **/*.xml</include >          </includes >        </resource >        <resource >          <directory > src/main/resources</directory >          <includes >            <include > **/*.xml</include >            <include > **/*.properties</include >          </includes >        </resource >      </resources >      <plugins >        <plugin >          <groupId > org.mortbay.jetty</groupId >          <artifactId > maven-jetty-plugin</artifactId >          <version > 6.1.7</version >          <configuration >            <connectors >              <connector  implementation ="org.mortbay.jetty.nio.SelectChannelConnector" >                <port > 8888</port >                <maxIdleTime > 30000</maxIdleTime >              </connector >            </connectors >            <webAppSourceDirectory > ${project.build.directory}/${pom.artifactId}-${pom.version}</webAppSourceDirectory >            <contextPath > /</contextPath >          </configuration >        </plugin >      </plugins >    </build >    <dependencies >           <dependency >        <groupId > org.springframework</groupId >        <artifactId > spring-webmvc</artifactId >        <version > 5.0.11.RELEASE</version >      </dependency >      <dependency >        <groupId > org.springframework</groupId >        <artifactId > spring-jdbc</artifactId >        <version > 5.0.11.RELEASE</version >      </dependency >      <dependency >        <groupId > org.springframework</groupId >        <artifactId > spring-aop</artifactId >        <version > 5.0.11.RELEASE</version >      </dependency >      <dependency >        <groupId > org.springframework</groupId >        <artifactId > spring-aspects</artifactId >        <version > 5.0.11.RELEASE</version >      </dependency >      <dependency >        <groupId > org.mybatis</groupId >        <artifactId > mybatis</artifactId >        <version > 3.4.5</version >      </dependency >      <dependency >        <groupId > org.mybatis</groupId >        <artifactId > mybatis-spring</artifactId >        <version > 1.3.1</version >      </dependency >      <dependency >        <groupId > mysql</groupId >        <artifactId > mysql-connector-java</artifactId >        <version > 8.0.11</version >      </dependency >      <dependency >        <groupId > c3p0</groupId >        <artifactId > c3p0</artifactId >        <version > 0.9.1.2</version >      </dependency >      <dependency >        <groupId > javax.servlet</groupId >        <artifactId > jstl</artifactId >        <version > 1.2</version >      </dependency >      <dependency >        <groupId > taglibs</groupId >        <artifactId > standard</artifactId >        <version > 1.1.2</version >      </dependency >           <dependency >        <groupId > javax.servlet</groupId >        <artifactId > javax.servlet-api</artifactId >        <version > 3.1.0</version >      </dependency >      <dependency >        <groupId > org.projectlombok</groupId >        <artifactId > lombok</artifactId >        <version > 1.18.6</version >        <scope > provided</scope >      </dependency >    </dependencies >  </project > 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 <?xml version="1.0" encoding="UTF-8"?> <web-app  version ="2.4"           xmlns ="http://java.sun.com/xml/ns/j2ee"           xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"           xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >   <context-param >      <param-name > contextConfigLocation</param-name >      <param-value > classpath:spring.xml</param-value >    </context-param >    <listener >      <listener-class > org.springframework.web.context.ContextLoaderListener</listener-class >    </listener >    <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 >    </servlet-mapping >    <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-mapping >      <servlet-name > default</servlet-name >      <url-pattern > *.js</url-pattern >    </servlet-mapping >    <servlet-mapping >      <servlet-name > default</servlet-name >      <url-pattern > *.css</url-pattern >    </servlet-mapping >    <servlet-mapping >      <servlet-name > default</servlet-name >      <url-pattern > *.png</url-pattern >    </servlet-mapping >  </web-app > 
 
web.xml 中配置一些 SpringMVC、Spring、字符编码过滤器、加载静态资源
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 <?xml version="1.0" encoding="UTF-8"?> <beans  xmlns ="http://www.springframework.org/schema/beans"         xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"         xmlns:context ="http://www.springframework.org/schema/context"         xmlns:p ="http://www.springframework.org/schema/p"         xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd " >         <bean  name ="dataSource"  class ="com.mchange.v2.c3p0.ComboPooledDataSource" >          <property  name ="user"  value ="root"  />          <property  name ="password"  value ="xiaobo"  />          <property  name ="jdbcUrl"  value ="jdbc:mysql://localhost:3306/db_mybatis?useUnicode=true& charcaterEncodeing=UTF-8& serverTimezone=UTC"  />          <property  name ="driverClass"  value ="com.mysql.cj.jdbc.Driver"  />          <property  name ="initialPoolSize"  value ="5"  />          <property  name ="maxPoolSize"  value ="10"  />      </bean >           <bean  id ="sqlSessionFactory"  class ="org.mybatis.spring.SqlSessionFactoryBean" >          <property  name ="dataSource"  ref ="dataSource" > </property >          <property  name ="mapperLocations"  value ="classpath:mapper/*.xml" > </property >                   <property  name ="configLocation"  value ="classpath:mybatis-config.xml" > </property >      </bean >           <bean  class ="org.mybatis.spring.mapper.MapperScannerConfigurer" >          <property  name ="basePackage"  value ="com.dream.xiaobo.dao"  />      </bean >  </beans > 
 
在 spring.xml 中配置 MyBatis 和 Spring 的整合
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <?xml version="1.0" encoding="UTF-8"?> <beans  xmlns ="http://www.springframework.org/schema/beans"         xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"         xmlns:context ="http://www.springframework.org/schema/context"         xmlns:mvc ="http://www.springframework.org/schema/mvc"         xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd" >         <mvc:annotation-driven > </mvc:annotation-driven >           <context:component-scan  base-package ="com.dream.xiaobo" > </context:component-scan >           <bean  class ="org.springframework.web.servlet.view.InternalResourceViewResolver" >          <property  name ="prefix"  value ="/"  />          <property  name ="suffix"  value =".jsp"  />      </bean >  </beans > 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration  PUBLIC  "-//mybatis.org//DTD Config 3.0//EN"  "http://mybatis.org/dtd/mybatis-3-config.dtd" > <configuration >     <settings >                   <setting  name ="logImpl"  value ="STDOUT_LOGGING"  />      </settings >      <typeAliases >                   <package  name ="com.southwind.entity" />      </typeAliases >  </configuration > 
 
config.xml 配置一些 MyBatis 辅助信息、比如打印 SQL 等
 
1 2 3 4 5 6 7 @Data public  class  User   {    private  Integer id;     private  String username;     private  String password; } 
 
1 2 3 public  interface  UserRepository   {    public  List<User> findAll ()  ; } 
 
1 2 3 4 5 6 7 8 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper  PUBLIC  "-//mybatis.org//DTD Mapper 3.0//EN"  "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper  namespace ="com.dream.xiaobo.dao.UserRepository" >     <select  id ="findAll"  resultType ="com.dream.xiaobo.entity.User" >          SELECT * FROM user     </select >  </mapper > 
 
1 2 3 public  interface  UserService   {    public  List<User> findAll ()  ; } 
 
1 2 3 4 5 6 7 8 9 10 @Service public  class  UserServiceImpl  implements  UserService   {    @Autowired      private  UserRepository userRepository;     public  List<User> findAll ()   {         return  userRepository.findAll();     } } 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 @Controller @RequestMapping(value = "/user") public  class  UserController   {    @Autowired      private  UserServiceImpl userService;     @RequestMapping(value = "/findAll")      public  ModelAndView findAll ()  {         ModelAndView modelAndView = new  ModelAndView();         modelAndView.setViewName("index" );         modelAndView.addObject("list" ,userService.findAll());         return  modelAndView;     } } 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <%@ page contentType="text/html;charset=UTF-8"  language="java"  %> <%@ page isELIgnored="false"  %> <%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core"  %> <html> <head>     <title>Title</title> </head> <body>     <c:forEach items="${list }"  var ="user" >         ${user.id}         ${user.username}         ${user.password}<br/>     </c:forEach> </body> </html> 
 
db 运行结果 流程图示分析 
正确的开始、微小的长进、然后持续、嘿、我是小博、带你一起看我目之所及的世界……