项目-砖头

1.mybatis配置

1.1 依赖

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

1.2 需要和@Configuration配和使用

@Configuration
@MapperScan("cn.skx.csmall.passport.mapper")
public class MybatisConfiguration {

    public MybatisConfiguration(){
        System.out.println("创建配置类: MybatisConfiguration" );
    }
}

1.3 xml中的配置

mybatis.mapper-locations=classpath/*.xml

1.4 properties配置

spring.datasource.url=jdbc:mysql://localhost:3306/mall_ams?useUnicode=true&characterEncoding=utf-8&serveTimezone=Asia/Shanhai
spring.datasource.username=root
spring.datasource.password=root

mybatis.mapper-locations=classpath:mapper/*.xml

2.Mvc统一异常处理

 2.1枚举

package cn.skx.csmall.passport.web;
/**
 * 枚举
 */
public enum ServiceCode {
    OK(20000),
    ERR_NOT_FOUND(40400),
    ERR_CONFLICT(40900);

    private Integer value;

    ServiceCode(Integer value){
        this.value = value;
    }

    public Integer getValue(){
         return value;
    }
}

2.2后端响应类

import cn.skx.csmall.product.ex.ServiceException;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;

/**
 * 后端处理响应数据,返回数据类
 */
@Data
public class JsonResult<T> implements Serializable {
    /**
     * 状态
     */
    @ApiModelProperty(value = "响应的状态码")
    private Integer state;
    /**
     * 失败的提示信息
     */
    //成功时没有这个,message 为 null时 不传
    //@JsonInclude(JsonInclude.Include.NON_NULL)
    @ApiModelProperty(value = "业务失败的信息")
    private String message;

    /**
     *操作成功时响应的数据
     * T 占位符
     */
    @ApiModelProperty(value = "操作成功时响应的数据")
    private T data;

    /**
     * 成功
     * 方法的重载
     * @return 指定的响应内容
     */
    public static JsonResult<Void> ok() {
        return ok(null);
    }

    //经停方法上需要加声明 同类上的声明
    public static <T> JsonResult<T> ok(T data) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.state = ServiceCode.OK.getValue();
        jsonResult.message = null;
        jsonResult.data = data;
        return jsonResult;
    }

    //自定义业务异常中有状态码和信息,所以可以简写
    //方法的重载
    public static JsonResult<Void> fail(ServiceException e) {
        return fail(e.getServiceCode(),e.getMessage());
    }

    /**
     * 失败
     * @param serviceCode 状态
     * @param message 内容
     * @return 指定的响应内容
     */
    public static JsonResult<Void> fail(ServiceCode serviceCode, String message) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.state = serviceCode.getValue();
        jsonResult.message = message;
        return jsonResult;
    }
}

2.3 创建业务异常类

package cn.skx.csmall.passport.ex;

import cn.skx.csmall.passport.web.ServiceCode;

/**
 * 业务异常
 * @author sun
 * @version 0.0.1
 */
public class ServiceException extends RuntimeException{

    private ServiceCode serviceCode;

    public ServiceCode getServiceCode() {
        return serviceCode;
    }

    public ServiceException(ServiceCode serviceCode,String message){
        super(message);
        this.serviceCode = serviceCode;
    }

}

2.4 全局接口异常处理类

package cn.skx.csmall.passport.ex.handler;

import cn.skx.csmall.passport.ex.ServiceException;
import cn.skx.csmall.passport.web.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局controller接口处理异常类
 * @author sun
 * @version 0.0.1
 */
@Slf4j
@RestControllerAdvice //单纯的使用 @controllerAdvice 响应的是文件名 + @ResponseBody 才可以返回字符串信息
public class GlobalExceptionHandler {

    public GlobalExceptionHandler(){
        System.out.println("全局异常处理器对象 : GlobalExceptionHandler");
    }

    /**
     * 老祖宗异常 兜底的 ,让这个系统永远不会报 500 错误,异常一定会有人处理
     * @param e 老祖宗异常
     * @return 报错信息
     */
    @ExceptionHandler
    public String handleThrowable(Throwable e){
        log.debug("捕获到Throwable:{}", e.getMessage());
        e.printStackTrace();
        return "服务器运行过程中出现未知错误,请联系系统管理员";
    }

    /**
     * 自己声明的异常处理
     * @param e 业务异常
     * @return 报错信息
     */
    @ExceptionHandler
    public JsonResult handleServiceException(ServiceException e){
        log.debug("捕获到ServiceException:{}", e.getMessage());
        return JsonResult.fail(e);
    }
}

2.5 业务层调用

String phone = adminAddNewDTO.getPhone();
int count = adminMapper.countByphone(phone);
if(count != 0){
    String message = "添加管理员失败,手机号【"+ phone +"】已经被占用";
    throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
}

 2.6 properties 配置,可以让返回数据 message 为空时 返回的json中就没有这个字段

spring.jackson.default-property-inclusion=non_null

 2.7 controller调用

@ApiOperation(value = "删除相册")
@ApiOperationSupport(order = 300)
@ApiImplicitParam(name = "id" , value = "相册id",required = true,dataType = "long")
@PostMapping(value = {"/delete/{id:[0-9]+}"})
public JsonResult<Void> deleteById(@PathVariable Long id){
    albumService.deleteById(id);
    return JsonResult.ok();
}

/**
* 获取相册列表
* @return 相册集合
*/
@ApiOperation(value = "查询相册列表")
@ApiOperationSupport(order = 420)
@GetMapping("")
public JsonResult<List<AlbumListVo>> getList(){
    List<AlbumListVo> list = albumService.list();
    return JsonResult.ok(list);
}

3.空白的mapper.xml配置文件

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

<mapper namespace="cn.skx.csmall.passport.mapper.AdminMapper">

</mapper>

4. properties配置 多个时 使用那个配置文件

spring.profiles.active=dev

5. knife4j 在线api文档

5.1 xml依赖 

<!--api在线文档-->
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>2.0.9</version>
</dependency>

5.2 配置类

package cn.skx.csmall.passport.config;

import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

/**
 * Knife4j配置类
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Slf4j
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfiguration {

    /**
     * 【重要】指定Controller包路径
     */
    private String basePackage = "cn.skx.csmall.passport.controller";
    /**
     * 分组名称
     */
    private String groupName = "product";
    /**
     * 主机名
     */
    private String host = "http://java.tedu.cn";
    /**
     * 标题
     */
    private String title = "酷鲨商城在线API文档--商品管理";
    /**
     * 简介
     */
    private String description = "酷鲨商城在线API文档--商品管理";
    /**
     * 服务条款URL
     */
    private String termsOfServiceUrl = "http://www.apache.org/licenses/LICENSE-2.0";
    /**
     * 联系人
     */
    private String contactName = "Java教学研发部";
    /**
     * 联系网址
     */
    private String contactUrl = "http://java.tedu.cn";
    /**
     * 联系邮箱
     */
    private String contactEmail = "java@tedu.cn";
    /**
     * 版本号
     */
    private String version = "1.0.0";

    @Autowired
    private OpenApiExtensionResolver openApiExtensionResolver;

    public Knife4jConfiguration() {
        log.debug("加载配置类:Knife4jConfiguration");
    }

    @Bean
    public Docket docket() {
        String groupName = "1.0.0";
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .host(host)
                .apiInfo(apiInfo())
                .groupName(groupName)
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any())
                .build()
                .extensions(openApiExtensionResolver.buildExtensions(groupName));
        return docket;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .termsOfServiceUrl(termsOfServiceUrl)
                .contact(new Contact(contactName, contactUrl, contactEmail))
                .version(version)
                .build();
    }

}

5.3  properties配置

knife4j.enable=true

5.4 controller的使用

//类上
@Api(tags = "01. 管理员模块")
//方法上
@ApiOperation(value = "新增管理员")
@ApiOperationSupport(order = 100)//升序排列

//参数为单个
@ApiImplictParam(name = "id",value = "相册的id",required = true,dataType = "long")
//参数为多个
@ApiImplictParams({
    @ApiImplictParam(xxx),
    @ApiImplictParam(xxx),
    @ApiImplictParam(xxx)
})

5.5 登录的网址

http://localhost:9080/doc.html

 5.6 实体上需要加的注解

@ApiModelProperty(value = "名称",required = true,example = "示例的相册")

 5.7 后端接收的参数 是对象还是formData类型的

@RequestBody 此注解加上

在线文档调试界面 不会显示请求参数的输入框,提供的是一个JSON字符串供编辑

前端传递的数据类型是 FormData类型的

name=jack&age=18&sex=男

后端就不能使用@RequestBody注解 api调试就是输入框

前端 formData 数据类型 和 后端注解 @RequestBody 互斥

6.前端Vue脚手架按照 QS

6.1 安装指令

npm i qs -S

6.2 配置main.js

import qs from  'qs';

Vue.prototype.qs = qs;

6.3 使用

 let formData = this.qs.stringify(this.ruleForm);

7. Validation 用于检查请求参数

7.1 依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

7.2 controller中使用

在类上,方法上,方法参数前加上 @Validated 或者 @Valid 注解

 7.3 实体上的使用

一般是放在字段上实际底层代码这样的

@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})

 @NotNull(message = "添加相册失败,必须提叫相册名称")

7.4 快速失败配置类

package cn.skx.csmall.passport.config;

import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.HibernateValidator;
import org.hibernate.validator.constraintvalidation.HibernateConstraintValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.validation.Validation;

/**
 * validated 配置类
 * @author sun
 */
@Slf4j
@Configuration
public class ValidationConfiguration {

    public ValidationConfiguration(){
        log.debug("创建配置类:ValidationConfiguration");
    }

    @Bean
    public javax.validation.Validator validator(){
        return Validation.byProvider(HibernateValidator.class)
                .configure()
                .failFast(true)//快速失败
                .buildValidatorFactory()
                .getValidator();
    }
}

7.5 响应新增 BindException

@ExceptionHandler
public JsonResult HandleBindException(BindException e){
    log.debug("捕获到BindException:{}", e.getMessage());
    //以下两行代码,如果有多种错误,将随机获取其中一种错误的信息,并相应
    String mssage = e.getFieldError().getDefaultMessage();
    return JsonResult.fail(ServiceCode.ERR_BAD_REQUEST, mssage);

    /*StringBuilder stringBuilder = new StringBuilder();
    List<FieldError> fieldErrors = e.getFieldErrors();
    for(FieldError fieldError : fieldErrors){
        stringBuilder.append(fieldError.getDefaultMessage());
    }
    return JsonResult.fail(ServiceCode.ERR_BAD_REQUEST, stringBuilder.toString());*/
}

8. 生成二维码

 8.1 java代码的依赖

<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.4.1</version>
</dependency>

<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.4.1</version>
</dependency>

8.2 代码

@Test
void contextLoads() {
    String text = "https://www.baidu.com"; // 要生成二维码的文本内容
    int width = 300; // 二维码图片的宽度
    int height = 300; // 二维码图片的高度
    String format = "png"; // 二维码图片的格式

    // 设置二维码参数
    QRCodeWriter qrCodeWriter = new QRCodeWriter();
    BitMatrix bitMatrix;
    try {
        bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        // 将BitMatrix转换为BufferedImage
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                bufferedImage.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
            }
        }

        // 保存生成的二维码图片
        ImageIO.write(bufferedImage, format, new File("qrcode." + format));
        System.out.println("二维码生成成功!");
   } catch (WriterException | IOException e) {
        e.printStackTrace();
   }
}

9. excel的导出

9.1 依赖

<!--导出需要的依赖-->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version>
</dependency>

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>2.2.10</version>
</dependency>

9.2 代码

package com.example.erweima;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;

public class excel {
    public static void main(String[] args) {
        //二维数组
        String[][] data = {
                {"Name", "Age", "City"},
                {"John", "25", "New York"},
                {"Alice", "30", "London"}
        };
        String filePath = "output.xlsx";
        exportToExcel(data, filePath);
        System.out.println("Excel file exported successfully!");
    }

    public static void exportToExcel(String[][]data, String filePath){
        try (Workbook workbook = new XSSFWorkbook()) {
            Sheet sheet = workbook.createSheet("Sheet1");
            for (int i = 0; i < data.length; i++) {
                Row row = sheet.createRow(i);
                for (int j = 0; j < data[i].length; j++) {
                    Cell cell = row.createCell(j);
                    cell.setCellValue(data[i][j]);
                }
            }

            try (FileOutputStream outputStream = new FileOutputStream(filePath)) {
                workbook.write(outputStream);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

10.  excel的导入

10.1 依赖

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version>
</dependency>

<dependency>
    <groupId>net.sourceforge.jexcelapi</groupId>
    <artifactId>jxl</artifactId>
    <version>2.6.12</version>
</dependency>

10.2 代码

package com.example.erweima;

import org.apache.poi.ss.usermodel.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class ExcelImport {

    public static void main(String[] args) {

        //指定路径的文件
        String filePath = "D:\\project5\\erweima\\output.xlsx";

        try {
            FileInputStream fis = new FileInputStream(new File(filePath));

            Workbook workbook = WorkbookFactory.create(fis);
            Sheet sheet = workbook.getSheetAt(0); // 假设你要处理的是第一个工作表

            // 遍历行
            for (Row row : sheet) {
                // 遍历单元格
                for (Cell cell : row) {
                    String cellValue = "";
                    switch (cell.getCellType()) {
                        case STRING:
                            cellValue = cell.getStringCellValue();
                            break;
                        case NUMERIC:
                            cellValue = String.valueOf(cell.getNumericCellValue());
                            break;
                        // 其他类型的单元格处理方式可以根据需要添加
                    }
                    System.out.print(cellValue + " ");
                }
                System.out.println();
            }

            workbook.close();
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

11 cors 跨域

配置类

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

/**
 * 跨域配置
 * @author sun
 */

@Configuration
public class MyMvcConfiguration implements WebMvcConfigurer {

    public MyMvcConfiguration(){
        System.out.println("创建配置类MyMvcConfiguration");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .maxAge(3600)
                .allowedHeaders("*")
                .allowedMethods("*")
                .allowedOriginPatterns("*")
                .allowCredentials(true);
    }
}

12 密码加密 

12.1 pom.xml配置

<!--Spring Security 安全框架-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

12.2 创建全局唯一的配置类

/**
 * 把 BCryptPasswordEncoder() 交给容器管理
 * 默认就是单例的
 * 全局唯一
 */
@Slf4j
@Configuration
public class SecurityConfiguration {

    //只需要创建一个就好了
    @Bean
    public PasswordEncoder passwordEncoder(){
        log.debug("创建@Bean方法定义的对象:PasswordEncoder");
        return new BCryptPasswordEncoder();
    }
}

12.3 业务层使用

@Autowired
private PasswordEncoder passwordEncoder;

//TODO 取出原密码 进行加密.并把密文装回Admin对象中
String rawPassword = admin.getPassword();
String encodePassword = passwordEncoder.encode(rawPassword);
admin.setPassword(encodePassword);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值