SpringBoot整合SSM【java全端课38】

50 阅读1分钟

SpringBoot整合SSM

1 新建工程导入依赖

1.1 新建工程

1.2 导入依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>3.0.3</version>
    </dependency>

    <dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter-test</artifactId>
        <version>3.0.3</version>
        <scope>test</scope>
    </dependency>
</dependencies>

2 编写配置文件

#配置数据源(Hikari)
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/0923_demo
    username: root
    password: root
#mvc配置
#web:
#resources:
#static-locations: classpath:/webapp/

#mybatis配置
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.mytest.pojo
  configuration:
    map-underscore-to-camel-case: true
    auto-mapping-behavior: full

3 整合MVC注意事项

3.1 静态资源加载问题

WebProperties源代码中默认解析静态路径:classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/

springboot支持自定义静态资源路径,一旦定义静态资源路径,默认静态资源路径失效

  • 自定义静态资源路径
spring:
    web:
      resources:
          static-locations: classpath:/webapp/

3.2 注册拦截器问题

package com.mytest.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class SpringMVCConfig implements WebMvcConfigurer {
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor1()).addPathPatterns("/interceptorController/testInterceptor");
    }
}

4 整合Mybatis注意事项

@Mapper注解:为当前接口生成代理对象

@MapperScan注解:为当前包下所有Mapper接口生成代理对象

@Configuration
@MapperScan("com.mytest.mapper")
public class MybatisConfig {
}

Banner及其他启动方式

1 banner设置

banner生成器:www.bootschool.net/ascii

自定义banner

  • 编写配置文件:application.properties

    spring.banner.location=classpath:banner.txt
    

2 其他启动方式

2.1 方式一

public static void main(String[] args) {
    //方式一:默认
    ConfigurableApplicationContext ioc
        = SpringApplication.run(Day20SpringbootsmApplication.class, args);
}

2.2 方式二

// 方式2
SpringApplication springApplication = new SpringApplication(SpringBoot01ConfigApplication.class);
// 设置参数
springApplication.setBannerMode(Banner.Mode.OFF);
//springApplication.setEnvironment();
//springApplication.addListeners();
// 运行项目
springApplication.run(args);

2.3 方式三

// 方式3
SpringApplicationBuilder springApplicationBuilder = new SpringApplicationBuilder();
springApplicationBuilder
    .sources(SpringBoot01ConfigApplication.class)
    // 设置参数
    .bannerMode(Banner.Mode.OFF)
    //.environment()
    //.listeners()
    // 启动
    .run(args);