- 前言
- 包结构
- 环境搭建
- 案例实现
前面我们使用SpringMvc整合了ssm,涉及到了前后端数据协议,异常处理,Restful风格请求数据等,内容在: SpringMvc+Spring+Mybatis整合+RESTFUl+异常处理。这里我们使用SpringBoot做一些整合的基础案例,供大家学习与巩固。
包结构
包结构:
config:
MPconfig(拦截器,动态添加 limit )
dao:BookDao
service:IBookService
service.impl: IBookServiceImpl
controller: BookController
controller.utils:
Result(前后端数据协议) ,Code (状态码), ProjectExceptionAdvice(异常拦截器)
domain: Book
exception (异常类):
BusinessException(业务异常) ,SystemException(系统异常)
resource:application.yml(配置文件)
配置文件类型:propertise,xml,yml,yaml
环境搭建pom.xml坐标:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.5.13 com.itheima springboot_ssm_mp 0.0.1-SNAPSHOT 1.8 org.springframework.boot spring-boot-starter-web com.baomidou mybatis-plus-boot-starter 3.4.3 com.alibaba druid-spring-boot-starter 1.2.6 mysql mysql-connector-java 5.1.32 runtime org.projectlombok lombok org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
domainb:
Book:
//这里使用Lombok方式动态生产get/set方法
@Data // getter+setter
public class Book {
private Integer id;
private String type;
private String name;
private String description;
}
resoutces:
application.yml:
# 程序员使用的配置文件
# 设置端口
server:
port: 80
# 设置数据源的基本连接信息
spring:
datasource:
druid:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db
username: root
password: 1234
# mybatis plus 参数设置
mybatis-plus:
global-config:
db-config:
table-prefix: tbl_ # 建立映射规则,在所有映射表名前加上 tbl_
id-type: auto # id由数据库自增策略自增
configuration: # 配置日志策略 详细显示日志的方式
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
案例实现
dao:
BookDao:
@Mapper public interface BookDao extends BaseMapper{ }
service:
IBookService:
public interface IBookService extends IService{ //自定义的一些其它方法,注意方法名不要与父类中的方法名相同,否则会重写覆盖 // 尽量不要覆盖继承的方法,以防后面业务需求使用到,给自己带来一些不必要的麻烦 IPage getPage(Integer currentPage,Integer pageSize); IPage getPage(Integer currentPage,Integer pageSize, Book book); }
这里的业务层也继承MP业务层接口,自动实现基础的业务方法
service:
imlp:
IBookServiceImpl:
@Service public class IBookServiceImpl extends ServiceImplimplements IBookService { @Autowired private BookDao bookDao; @Override public IPage getPage(Integer currentPage, Integer pageSize) { IPage page = new Page<>(currentPage,pageSize); return bookDao.selectPage(page, null); } @Override public IPage getPage(Integer currentPage, Integer pageSize, Book book) { LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); //like的三个参数:1:需要一个布尔值,为true则加上该条件,否则不加该条件 // 2:需要实体类中的一个属性/字段,用来表明是哪个字段的条件 // 3: 需要一个Object ,用来表明需要满足的条件 lambdaQueryWrapper.like(book!=null,Book::getType,book.getType()); lambdaQueryWrapper.like(book!=null,Book::getName,book.getName()); lambdaQueryWrapper.like(book!=null,Book::getDescription,book.getDescription()); IPage page = new Page<>(currentPage,pageSize); return bookDao.selectPage(page, lambdaQueryWrapper); } }
controller:
BookController:
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private IBookService iBookService;
@GetMapping
public Result getAll(){
List list = iBookService.list();
Integer code =list != null ? Code.GET_OK:Code.GET_ERR;
String msg =list != null ? "查询成功":"查询失败,请重试";
return new Result(code,list,msg);
}
@GetMapping("/{id}")
public Result getByid(@PathVariable Integer id){ //id来源于请求路径
Book book = iBookService.getById(id);
Integer code = book !=null ? Code.GET_OK:Code.GET_ERR;
String msg = book!=null ? "查询成功":"查询失败,请稍后重试";
return new Result(code,book,msg);
}
@PostMapping
public Result Save(@RequestBody Book book){
boolean flag = iBookService.save(book);
String msg = flag ? "添加成功!":"添加失败";
return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag,msg);
}
@DeleteMapping("/{id}")
public Result Delete(@PathVariable Integer id){
boolean flag = iBookService.removeById(id);
String msg = flag ? "删除成功!":"删除失败";
return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag,msg);
}
@PutMapping
public Result Update(@RequestBody Book book){
boolean flag = iBookService.updateById(book);
String msg = flag ? "修改成功!":"修改失败";
return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag,msg);
}
// @GetMapping("/{currentPage}/{pageSize}")
// public Result getPage(@PathVariable Integer currentPage,@PathVariable Integer pageSize){
// IPage page = iBookService.getPage(currentPage, pageSize);
// Integer code = page != null ? Code.GET_OK:Code.GET_ERR;
// String msg = page != null ? "查询成功":"查询失败";
//
// //如果用户输入的当前页码值大于总页码值 将最大页码查询出来展示
// if(currentPage>page.getPages()){
// page = iBookService.getPage((int) page.getPages(), pageSize);
// }
// return new Result(code,page,msg);
// }
@GetMapping("/{currentPage}/{pageSize}")
public Result getPage(@PathVariable Integer currentPage,@PathVariable Integer pageSize,Book book){
//springmvc帮我们自动完成参数封装,所以这里的book中已经包含我们的参数
// System.out.println(book);
IPage page = iBookService.getPage(currentPage, pageSize,book);
Integer code = page != null ? Code.GET_OK:Code.GET_ERR;
String msg = page != null ? "查询成功":"查询失败";
//如果用户输入的当前页码值大于总页码值 将最大页码查询出来展示
if(currentPage>page.getPages()){
page = iBookService.getPage((int) page.getPages(), pageSize,book);
}
return new Result(code,page,msg);
}
}
controller:
utils:
Result:
**
* 前后端数据协议:将后端返回的所有类型全部封装为一种类型
*/
@Data //相当于getter+setter
@NoArgsConstructor //无参的构造方法
@AllArgsConstructor //全部参数的构造方法
public class Result {
private Integer code; //状态码
private Object data; //数据
private String msg; //提示信息
public Result(Integer flag) {
this.code = flag;
}
public Result(Integer flag, Object data) {
this.code = flag;
this.data = data;
}
}
controller:
utils:
Code:封装状态码
public class Code {
//增删改查状态码
public static final Integer SAVE_OK = 2011;
public static final Integer DELETE_OK =2021;
public static final Integer UPDATE_OK =2031;
public static final Integer GET_OK =2041;
//失败
public static final Integer SAVE_ERR =2010;
public static final Integer DELETE_ERR =2020;
public static final Integer UPDATE_ERR =2030;
public static final Integer GET_ERR =2040;
//系统异常
public static final Integer SYSTEM_ERR = 50001;
//业务异常
public static final Integer BUSINESS_ERR = 60002;
//系统位置异常
public static final Integer SYSTEM_UNKONW_ERR = 59999;
}
controller:
utils:
ProjectExceptionAdvice:异常拦截器
@RestControllerAdvice //advice:通知
public class ProjectExceptionAdvice {
@ExceptionHandler(SystemException.class) // 异常处理器 拦截System异常
public Result doSystemException(SystemException ex) { //收集异常的方法
//记录日志
//发送消息给运维
//发送邮件给开发人员
return new Result(ex.getCode(), null, ex.getMessage());
}
@ExceptionHandler(BusinessException.class) // 异常处理器 拦截Business异常
public Result doBusinessException(BusinessException ex) { //收集异常的方法
return new Result(ex.getCode(), null, ex.getMessage());
}
@ExceptionHandler(Exception.class) // 异常处理器 拦截所有异常
public Result doException(Exception ex) { //收集异常的方法 所有异常都会进来
//记录日志
//发送消息给运维
//发送邮件给开发人员
return new Result(Code.SYSTEM_UNKONW_ERR, null, "系统繁忙,请稍后再试!");
}
}
exception:
BusinessException:业务异常
public class BusinessException extends RuntimeException {
private Integer code; //异常编号
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public BusinessException(Integer code, String message) {
super(message);
this.code = code;
}
public BusinessException(Integer code,String message, Throwable cause ) {
super(message, cause);
this.code = code;
}
}
exception:
SystemException:系统异常
public class SystemException extends RuntimeException{
private Integer code; //异常编号
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public SystemException(Integer code, String message) {
super(message);
this.code = code;
}
public SystemException(Integer code,String message, Throwable cause ) {
super(message, cause);
this.code = code;
}
}
SpringbootSsmMpApplication:主引导类,boot程序启动的关键类,也是程序的入口,部分内容牵扯到运维相关知识,后面博客会持续更新
@SpringBootApplication
public class SpringbootSsmMpApplication {
public static void main(String[] args) {
//如果不希望 用户外程序外使用临时参数时 可以将语句改为 安全性高
// SpringApplication.run(SpringbootSsmMpApplication.class);
SpringApplication.run(SpringbootSsmMpApplication.class, args);
}
}
下面给出前端对应的Vue中的Ajax请求,方便大家对比学习
book.html
上面给出的消息提示全部引用后台系统中BookController中设置的消息,更贴近于真实开发场景,做到统一管理。



