SSM整合

步骤

  1. 创建工程

  2. SSM整合

    • Spring
      • SpringConfig
    • MyBatis
      • MybatisConfig
      • JdbcConfig
      • jdbc.properties
    • SpringMVC
      • ServletConfig
      • SpringMvcConfig

    3.功能模块

    • 表和实体类

    • Dao(接口+自动代理)

    • service(接口+测试类)

      • 业务层接口(整合Junit)
    • Controller

      • 表现层接口测试(PostMan)

一:导入相关依赖

<dependencies>    <dependency>        <groupId>org.springframework</groupId>        <artifactId>spring-webmvc</artifactId>        <version>5.2.10.RELEASE</version>    </dependency>    <dependency>        <groupId>org.springframework</groupId>        <artifactId>spring-jdbc</artifactId>        <version>5.2.10.RELEASE</version>    </dependency>    <dependency>        <groupId>org.springframework</groupId>        <artifactId>spring-test</artifactId>        <version>5.2.10.RELEASE</version>    </dependency>    <dependency>        <groupId>org.mybatis</groupId>        <artifactId>mybatis</artifactId>        <version>3.5.6</version>    </dependency>    <dependency>        <groupId>org.mybatis</groupId>        <artifactId>mybatis-spring</artifactId>        <version>1.3.0</version>    </dependency>    <dependency>        <groupId>mysql</groupId>        <artifactId>mysql-connector-java</artifactId>        <version>5.1.47</version>    </dependency>    <dependency>        <groupId>com.alibaba</groupId>        <artifactId>druid</artifactId>        <version>1.1.16</version>    </dependency>    <dependency>        <groupId>junit</groupId>        <artifactId>junit</artifactId>        <version>4.12</version>        <scope>test</scope>    </dependency>    <dependency>        <groupId>javax.servlet</groupId>        <artifactId>javax.servlet-api</artifactId>        <version>3.1.0</version>        <scope>provided</scope>    </dependency>    <dependency>        <groupId>com.fasterxml.jackson.core</groupId>        <artifactId>jackson-databind</artifactId>        <version>2.9.0</version>    </dependency></dependencies><build>    <plugins>        <plugin>            <groupId>org.apache.tomcat.maven</groupId>            <artifactId>tomcat7-maven-plugin</artifactId>            <version>2.1</version>            <configuration>                <port>80</port>                <path>/</path>            </configuration>        </plugin>    </plugins></build>

二:创建相应包和类

三:编写相应配置类

3.1:Spring配置类

@Configuration@Import({JdbcConfig.class,MybatisConfig.class})@ComponentScan("com.ysj.service")@PropertySource("classpath:jdbc.properties")public class SpringConfig {}

3.2:MyBatis配置类

3.2.1:Mybatis

public class MybatisConfig {    @Bean   public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){       SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();       sqlSessionFactoryBean.setTypeAliasesPackage("com.ysj.Domain");        sqlSessionFactoryBean.setDataSource(dataSource);       return sqlSessionFactoryBean;    }    @Bean    public MapperScannerConfigurer mapperScannerConfigurer(){        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();        mapperScannerConfigurer.setBasePackage("com.ysj.Dao");        return mapperScannerConfigurer;    }}

3.2.2:Jdbc配置类

public class JdbcConfig {//    jdbc.driver=com.mysql.jdbc.Driver//    jdbc.url=jdbc:mysql://localhost:3308/mybatis//    jdbc.username=root//    jdbc.password=yangshijie    @Value("${jdbc.driver}")    private String driver;    @Value("${jdbc.url}")    private String url;    @Value("${jdbc.username}")    private String username;    @Value("${jdbc.password}")    private String password;    @Bean    public DataSource dataSource(){        DruidDataSource dataSource = new DruidDataSource();        dataSource.setDriverClassName(driver);        dataSource.setUrl(url);        dataSource.setUsername(username);        dataSource.setPassword(password);        return dataSource;    }}

3.2.3:Jdbc.properties

jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/mybatisjdbc.username=rootjdbc.password=root

4:SpringMVC配置类

  • 控制表现层(这个配置类如果导入SpringConfig中,SpringMVC控制的bean也会被Spring控制)

    @Configuration@ComponentScan("com.ysj.Controller")@EnableWebMvcpublic class SpringMvcConfig {}
  • web配置类

    //web配置类public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {    protected Class<" />

    9.2:拦截器和过滤器的区别

    9.2.1 精简版

    • 过滤器是Servlet技术,其可以对几乎所有的请求进行增强
    • 拦截器是SpringMVC技术,仅仅对SpringMVC技术进行增强

    9.2.2 详细版

    以下信息来源于html中文网

    9.3:拦截器入门

    • 拦截器在表现层生效,所以建议在controller包下新建Interceptor包

    • 在该包下新建所需拦截器类(类继承HandlerInterceptor接口)

    //继承HandlerInterceptor接口//注意当前类必须受Spring容器控制@Componentpublic class TestProjectInterceptor implements HandlerInterceptor {    @Override    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {        //中间可以加上所需业务逻辑        return false;//这个返回值决定了后续的原生方法等是否会执行    }//原始方法调用后执行的内容    @Override    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {    }//原始方法调用完成后执行的内容    @Override    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {    }}
    • 将其加入SpringMvc控制中

    • 方法:

      • 新建一个类,继承WebMvcConfigurationSupport

        @Configurationpublic class SpringMvcSupport extends WebMvcConfigurationSupport {    @Autowired    private TestProjectInterceptor testProjectInterceptor;//    *    匹配单个字符,如 /user/* 匹配的是 /user/aa,/user/bb 等,又如  /user/*/ab 匹配到 /user/p/ab;//            **   匹配任意多字符(包括多级路径),如:/user/** 匹配到 /user/aa、/user/p/bb 等;    //配置相应的静态资源放行    @Override    protected void addResourceHandlers(ResourceHandlerRegistry registry) {        registry.addResourceHandler("pages/**").addResourceLocations("pages/");    }    @Override    protected void addInterceptors(InterceptorRegistry registry) {        registry.addInterceptor(testProjectInterceptor).addPathPatterns("books/","books/*");    }}
      • 直接在SpringMvc配置类中进行

        • 让SpringMvc配置类继承WebMvcConfigurer接口
      @Configuration@ComponentScan({"com.ysj.controller"})@EnableWebMvc//实现WebMvcConfigurer接口可以简化开发,但具有一定的侵入性public class SpringMvcConfig implements WebMvcConfigurer {    @Autowired    private ProjectInterceptor projectInterceptor;    @Autowired    private ProjectInterceptor2 projectInterceptor2;    @Override    public void addInterceptors(InterceptorRegistry registry) {        //配置多拦截器        registry.addInterceptor(projectInterceptor).addPathPatterns("/books","/books/*");        registry.addInterceptor(projectInterceptor2).addPathPatterns("/books","/books/*");    }}

    9.4拦截器参数

    • request:请求对象

    • response:响应对象

    • handler:一个被调用的处理器对象,其本质上是一个方法对象(HandlerMethod),对反射技术中的

      ​ Method进行的再包装。

    • ModelAndView:若处理器执行完成具有返回结果,可以读取到对应数据与页面信息,并进行调整

    • Exception:获取程序运行过程中的异常对象(可通过SpringMvc自定义的异常处理器完美代替)

    9.5拦截器链

    • 创建所需的多个拦截器类

    • 配置多拦截器

          @Override    public void addInterceptors(InterceptorRegistry registry) {        //配置多拦截器        registry.addInterceptor(projectInterceptor).addPathPatterns("/books","/books/*");        registry.addInterceptor(projectInterceptor2).addPathPatterns("/books","/books/*");    }}
    • 多拦截器执行顺序

      • 按添加顺序运行
      • 若其中一个拦截器触发拦截条件,返回值为false,则直接跳到afterCompletion方法处,然后(出栈思想)

    ** 好了。SSM整合包含的知识点和步骤到这里已经总结完了,如有不足请指正,欢迎各位来一起交流讨论。
    最后,创作不易,给个三连支持下呗。**