SpringBoot2.x系列教程07--SpringBoot中以注解方式实现SSM整合

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 前言在上一章节中,壹哥 跟大家讲过,在SpringBoot项目中,我们有两种SSM整合的方式:● 1️⃣. XML方式;● 2️⃣. 注解两种方式。不知道现在你有没有掌握以XML文件方式进行的SSM整合呢?如果你有跟着我之前的文章编写了对应的代码,会发现以XML的实现方式实现SSM整合,虽然比传统的SSM整合过程简单,但是总感觉还是比较麻烦。所以接下来 壹哥 带大家再学习一种更简单的SSM整合方式,本节课咱们来学习以注解方式来实现SSM整合的过程。一. 以注解方式实现SSM整合1. 创建新的项目模块首先请参考我之前的文章,在之前的项目基础之上,创建一个新的module模块。在新的

前言

在上一章节中,壹哥 跟大家讲过,在SpringBoot项目中,我们有两种SSM整合的方式:

  • 1️⃣. XML方式;
  • 2️⃣. 注解两种方式。

不知道现在你有没有掌握以XML文件方式进行的SSM整合呢?如果你有跟着我之前的文章编写了对应的代码,会发现以XML的实现方式实现SSM整合,虽然比传统的SSM整合过程简单,但是总感觉还是比较麻烦。所以接下来 壹哥 带大家再学习一种更简单的SSM整合方式,本节课咱们来学习以注解方式来实现SSM整合的过程。

一. 以注解方式实现SSM整合

1. 创建新的项目模块

首先请参考我之前的文章,在之前的项目基础之上,创建一个新的module模块。在新的module模块中,请添加如下核心依赖包:

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.5.RELEASE</version><relativePath/><!-- lookup parent from repository --></parent><dependencies><!-- web支持: 1、web mvc; 2、restful; 3、jackson支持; 4、aop ........ --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.46</version></dependency><!--spring-boot mybatis依赖:请不要使用1.0.0版本,因为不支持拦截器插件.--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.1</version></dependency><!--mybatis分页插件--><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.2.3</version></dependency><!--简化bean代码--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies>

2. 配置SpringBoot依赖插件

在SpringBoot中以注解方式实现SSM整合时,我们是可以把StudentMapper.xml这样的XML文件删除掉的,但是我们最好在pom.xml文件中设置一下,允许在Java中可以对XML进行识别编译,所以我们仍然需要配置如下项。

<build><!--注意:配置xml资源属性,允许java包下的xml文件可编译--><resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes></resource></resources><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>

3. 配置application.properties文件

接下来我们可以配置自己的application.prpperties文件,在这里请各位修改成自己的数据库信息。

#数据源配置
spring.datasource.username=root
spring.datasource.password=syc
spring.datasource.url=jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#mybatis配置
mybatis.type-aliases-package=com.yyg.boot.domain

4. 编写StudentMapper接口方法

这里我们跟之前类似,还是编写一个Mapper接口类,但是这里我们没有了之前对应的Mapper.xml文件,dao层数据库的CRUD实现以注解方式来完成,这里涉及到了@Select注解,实际上还有@Insert、@Update、@Delete注解。

packagecom.yyg.boot.mapper;
importcom.yyg.boot.domain.Student;
importorg.apache.ibatis.annotations.Param;
importorg.apache.ibatis.annotations.Select;
importjava.util.List;
publicinterfaceStudentMapper {
/*** 以注解的方式实现ssm整合*/@Select("SELECT * FROM student")
List<Student>getAll();
@Select("SELECT * FROM student where id=#{id}")
StudentgetById(@Param("id") intid);
}

@Select():实现查询,参数是原生的查询SQL语句;

@Insert:实现添加,参数是原生的添加SQL语句;

@Update:实现修改,参数是原生的修改SQL语句;

@Delete:实现删除,参数是原生的删除SQL语句。

5. 编写service层实现类

根据三层架构的思想,我们还应该创建一个“com.yyg.boot.service”的包及其子包“impl”。

5.1 StudentService接口
我们在service包里面创建出一个StudentService接口。

packagecom.yyg.boot.service;
importcom.yyg.boot.domain.Student;
importjava.util.List;
publicinterfaceStudentService {
/*** 分页参数*/List<Student>getAll(IntegerpageNum, IntegerpageSize);
Studentget(Integerid);
}

5.2 StudentServiceImpl实现类
然后在impl包里面,创建出对应的StudentServiceImpl实现类。

packagecom.yyg.boot.service.impl;
importcom.github.pagehelper.PageHelper;
importcom.github.pagehelper.PageInfo;
importcom.yyg.boot.domain.Student;
importcom.yyg.boot.mapper.StudentMapper;
importcom.yyg.boot.service.StudentService;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Service;
importjava.util.List;
@ServicepublicclassStudentServiceImplimplementsStudentService {
@AutowiredprivateStudentMapperstudentMapper;
@OverridepublicList<Student>getAll(IntegerpageNum, IntegerpageSize) {
//在查询之前设置分页,利用Mybatis的分页插件实现分页PageHelper.startPage(pageNum, pageSize);
List<Student>list=studentMapper.getAll();
PageInfo<Student>info=newPageInfo<>(list);
returninfo.getList();
    }
@OverridepublicStudentget(Integerid) {
returnstudentMapper.getById(id);
    }
}

6. 编写web层接口

接着我们再创建出一个“com.yyg.boot.web”包,在该包里面创建一个StudentController类,在该类中创建一下Web接口。

packagecom.syc.boot.web;
importcom.syc.boot.domain.Student;
importcom.syc.boot.service.StudentService;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.*;
importjava.util.List;
@RestController@RequestMapping("/stu")
publicclassStudentController {
@AutowiredprivateStudentServicestudentService;
@RequestMapping(value="/find", method=RequestMethod.GET)
publicList<Student>find(@RequestParam(name="pageNum",required=false,defaultValue="1") IntegerpageNum,@RequestParam(name="pageSize" ,required=false,defaultValue="20") IntegerpageSize) {
returnstudentService.getAll(pageNum,pageSize);
    }
@RequestMapping(value="/{id}", method=RequestMethod.GET)
publicStudentget(@PathVariable(name="id") Integerid) {
returnstudentService.get(id);
    }
}

7. 编写程序入口

最后我们在“com.yyg.boot”包的根目录下,创建一个程序入口类SSMApplication!这里一定要注意在该类上配置@MapperScan注解,扫描mapper接口文件所在包!

packagecom.yyg.boot;
importorg.mybatis.spring.annotation.MapperScan;
importorg.springframework.boot.SpringApplication;
importorg.springframework.boot.autoconfigure.SpringBootApplication;
/*** 以XML方式整合SSM.* MapperScan:扫描Mybatis接口文件*/@MapperScan("com.yyg.boot.mapper")
@SpringBootApplicationpublicclassSSMApplication {
publicstaticvoidmain(String[] args) {
SpringApplication.run(SSMApplication.class, args);
    }
}

8. 项目包结构

最后,我们项目的核心配置与核心代码就都编写完毕了,整个项目的代码结构如下图所示:

二. 验证结果

接下来我们就把项目启动起来,看看SSM整合有没有实现。

1. 测试ssm整合及分页效果

我们通过启动入口类,把项目启动起来,进行测试。

2. 测试分页接口

仍然可以实现SSM整合,以注解的方式实现明显更简单一些!

结语

至此,我们就以注解的方式,实现了SSM整合功能。你会发现,以注解方式实现SSM整合比以XML方式更简单,基本上通过几个简单的配置就实现了。今天的内容你学会了吗?可以在评论区留言告诉我哦。

今日小作业:

以注解方式实现SSM整合,编写一个带有页面的学生信息管理功能。

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
1月前
|
Java 数据库
【问题记录(已解决)】springboot整合ssm报错‘url‘ attribute is not specified and no embedded datasource
【问题记录(已解决)】springboot整合ssm报错‘url‘ attribute is not specified and no embedded datasource
|
7月前
|
Java 数据库连接 Maven
使用maven快速搭建SpringBoot的SSM项目(下)
使用maven快速搭建SpringBoot的SSM项目(下)
60 0
|
3月前
|
Java 关系型数据库 MySQL
springboot基于ssm框架实现的家具商城管理系统
springboot基于ssm框架实现的家具商城管理系统
|
3月前
|
供应链 前端开发 Java
基于SSM框架springBoot实现的企业级进销存ERP系统【源码+数据库+毕设】
基于SSM框架springBoot实现的企业级进销存ERP系统【源码+数据库+毕设】
|
7月前
|
前端开发 Java Maven
使用maven快速搭建SpringBoot的SSM项目(上)
使用maven快速搭建SpringBoot的SSM项目
67 0
|
8月前
|
前端开发 Java 关系型数据库
SpringBoot实现SSM(附上例子)
SpringBoot实现SSM(附上例子)
63 0
|
8月前
|
JSON 前端开发 JavaScript
SpringBoot仅会SSM强撸项目--【JSB项目实战】
SpringBoot仅会SSM强撸项目--【JSB项目实战】 CSDN 转过来的,所以格式与内容有些许错误请见谅
|
9月前
|
Java
SpringBoot定时任务 注解方式开启
SpringBoot定时任务 注解方式开启
SpringBoot定时任务 注解方式开启
|
10月前
|
Java 数据库 Spring
基于SpringBoot的SSM整合案例 -- SpringBoot快速入门保姆级教程(四)
基于SpringBoot的SSM整合案例 -- SpringBoot快速入门保姆级教程(四)
|
10月前
|
druid Java 数据库连接
SSM搭建:Springboot整合mybatis
SSM框架现在开发届中非常的流行,而且使用起来也非常简单,使用springboot更是让我们开发效率提高无数倍;好了,下面写一下自己整合的过程;
96 0