5. SpringBoot整合SSM

发布时间 2023-07-05 18:04:53作者: 捞起月亮的小北

SpringBoot 整合 SSM 的步骤(基于 SSM 中的 SSM 整合案例)

  1. pom.xml
    配置起步依赖,必要的资源坐标(druid)
  2. application.yml
    设置数据源、端口等
  3. 配置类
    全部删除
  4. dao
    设置@Mapper
  5. 测试类
  6. 页面
    放置在 resources 目录下的 static 目录中

1. 创建工程

创建 SpringBoot​ 工程,在创建工程时需要勾选 web​、mysql​、mybatis

导入 Druid​ 的坐标

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.16</version>
</dependency>

2. 拷贝代码

image

需要修改的内容如下:

  • Springmvc_11_page​ 中 config​ 包下的是配置类,而 SpringBoot​ 工程不需要这些配置类,所以这些可以直接删除

  • dao​ 包下的接口上在拷贝到 springboot_09-ssm​ 工程中需要在接口中添加 @Mapper​ 注解

  • BookServiceTest​ 测试需要改成 SpringBoot​ 整合 junit​ 的

    @SpringBootTest
    public class BookServiceTest {
    
        @Autowired
        private BookService bookService;
    
        @Test
        public void testGetById(){
            Book book = bookService.getById(2);
            System.out.println(book);
        }
    
        @Test
        public void testGetAll(){
            List<Book> all = bookService.getAll();
            System.out.println(all);
        }
    }
    

3. 配置文件

application.yml​ 配置文件中需要配置如下内容

  • 服务的端口号
  • 连接数据库的信息
  • 数据源

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
    username: root
    password: 200133
    type: com.alibaba.druid.pool.DruidDataSource

4. 静态资源

SpringBoot​ 程序中是没有 webapp​ 目录的,那么在 SpringBoot​ 程序中静态资源需要放在什么位置呢?

静态资源需要放在 resources​ 下的 static​ 下,如下图所示

image

5. 访问页面

image