SpringBoot整合第三方技术

SpringBoot整合Junit

在这里插入图片描述

SpringBoot整合MyBatis

➠ 新建模块,选择Spring初始化,并配置模块相关基础信息
在这里插入图片描述

➠ 选择当前模块需要使用的技术集(MyBatis、MySQL)
在这里插入图片描述

➠ 创建代理类(数据层接口)

package com.GY.dao;
import com.GY.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

//创建代理类
@Mapper
public interface UserDao {
    @Select("select * from user where id=#{id}")
    public User getID(Integer id);

}

➠ 设置数据源参数

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC
    username: root
    password: 123456

此时我们测试dao接口(发现有大量报错信息)
在这里插入图片描述

报错信息大致如下
在这里插入图片描述

查阅后发现我们需要设置下时区,这里不是 springboot + mybatis 的问题,而是为了使MySQL JDBC驱动的版本与UTC时区配合使用( 全球标准时间),必须在连接字符串中明确指定serverTimezone (指定时区)
UTC代表的是全球标准时间 ,但是我们使用的时间是北京时区也就是东八区,领先UTC八个小时。

//北京时间东八区
serverTimezone=GMT%2B8 
//或者使用上海时间
serverTimezone=Asia/Shangha

🚸🚸注意事项:SpringBoot版本低于2.4.3(不含),Mysql 驱动版本大于8.0时,需要在url连接串中配置时区
url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC

➠ 修改application.yml中的配置信息
在这里插入图片描述

➠ 查询成功
在这里插入图片描述

基于SpringBoot实现ssm整合
➠ 选择当前模块需要使用的技术集(Spring Web、MyBatis、MySQL)

在这里插入图片描述

包层次结构
在这里插入图片描述

基于REST风格的控制器类

package com.GY.controller;
import com.GY.domain.User;
import com.GY.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    //添加
    @PostMapping
    public Result save(@RequestBody User user){
        boolean flag=userService.save(user);
        return new Result(flag?Code.SAVE_OK:Code.SAVE_ERR,flag) ;
    }

    //删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        boolean flag=userService.delete(id);
        return new Result(flag?Code.DELETE_OK:Code.DELETE_ERR,flag) ;
    }

    //修改
    @PutMapping
    public Result update(@RequestBody User user){
        boolean flag= userService.update(user);
        return new Result(flag?Code.UPDATE_OK:Code.UPDATE_ERR,flag) ;
    }

    //查询一个
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        User user=userService.getById(id);
        System.out.println(user);
        Integer code=user!=null?Code.GET_OK:Code.GET_ERR;
        String msg=user!=null?"":"数据查询失败,请重试";
        return new Result(code,user,msg);
    }

    //查询全部
    @GetMapping
    public Result getAll(){
        List<User> list=userService.getAll();
        Integer code=list!=null?Code.GET_OK:Code.GET_ERR;
        String msg=list!=null?"":"数据查询失败,请重试";
return new Result(code,list,msg);
    }
}

码值类
在这里插入图片描述

基于REST风格的控制器类

package com.GY.controller;
import com.GY.domain.User;
import com.GY.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    //添加
    @PostMapping
    public Result save(@RequestBody User user){
        boolean flag=userService.save(user);
        return new Result(flag?Code.SAVE_OK:Code.SAVE_ERR,flag) ;
    }

    //删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        boolean flag=userService.delete(id);
        return new Result(flag?Code.DELETE_OK:Code.DELETE_ERR,flag) ;
    }

    //修改
    @PutMapping
    public Result update(@RequestBody User user){
        boolean flag= userService.update(user);
        return new Result(flag?Code.UPDATE_OK:Code.UPDATE_ERR,flag) ;
    }

    //查询一个
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        User user=userService.getById(id);
        System.out.println(user);
        Integer code=user!=null?Code.GET_OK:Code.GET_ERR;
        String msg=user!=null?"":"数据查询失败,请重试";
        return new Result(code,user,msg);
    }

    //查询全部
    @GetMapping
    public Result getAll(){
        List<User> list=userService.getAll();
        Integer code=list!=null?Code.GET_OK:Code.GET_ERR;
        String msg=list!=null?"":"数据查询失败,请重试";
return new Result(code,list,msg);
    }
}

系统异常

package com.GY.exception;

public class SystemException extends RuntimeException{

    private Integer code;

    public Integer getCode() {
        return 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;
    }
}

业务异常

package com.GY.exception;

public class BusinessException extends RuntimeException{
    private Integer code;

    public Integer getCode() {
        return 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;
    }
}

异常处理类

package com.GY.controller;
import com.GY.exception.BusinessException;
import com.GY.exception.SystemException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ProjectExceptionAdvice {
    @ExceptionHandler(SystemException.class)
    public  Result doSystemException(SystemException e){
        return new Result(e.getCode(),null,e.getMessage());
    }

    @ExceptionHandler(BusinessException.class)
    public  Result doBusinessException(BusinessException e){
        return new Result(e.getCode(),null,e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public  Result doException(Exception e){
        return new Result(Code.SYSTEM_UNKNOW_ERR,null,"系统繁忙,请稍后重试");
    }
}

返回结果集封装
在这里插入图片描述

发送请求测试
在这里插入图片描述

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

new一个对象_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值