SpirngBoot + Vue 前后端分离开发工具代码

✅作者简介:大家好,我是Leo,热爱Java后端开发者,一个想要与大家共同进步的男人😉😉

🍎个人主页:Leo的博客

💞当前专栏: Java从入门到精通

✨特色专栏: MySQL学习

🥭本文内容:SpirngBoot + Vue 前后端分离开发工具代码

🖥️个人小站 :个人博客,欢迎大家访问

📚个人知识库: Leo知识库,欢迎大家访问

1.MybatisPlus配置类

导入依赖

xml 复制代码
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
java 复制代码
package com.jerry.common.config.mp;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * ClassName: MybatisPlusConfig
 * Package: com.jerry.common.config.mp
 * Description:
 *
 * @Author gaoziman
 * @Create 2023-03-01 11:17
 * @Version 1.0
 */

@Configuration
@MapperScan("com.jerry.auth.mapper")
public class MybatisPlusConfig {

    /**
     * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> configuration.setUseDeprecatedExecutor(false);
    }			
}	

2.使用mybatisplus分页查询

第一种

java 复制代码
    /**
     * 条件分页查询
     *
     * @param page           当前页
     * @param pageSize       分页大小
     * @param sysRoleQueryVo 条件查询对象
     * @return
     */
    @ApiOperation("条件分页查询")
    @GetMapping("{page}/{pageSize}")
    private Result page(@PathVariable int page, @PathVariable int pageSize, SysRoleQueryVo sysRoleQueryVo) {

        // 1、创建 page 对象, 传递分页查询的参数
        Page<SysRole> sysRolePage = new Page<>(page, pageSize);

        // 2、构造分页查询条件, 判断条件是否为空,不为空进行封装
        LambdaQueryWrapper<SysRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String roleName = sysRoleQueryVo.getRoleName();
        if (!StringUtils.isEmpty(roleName)) {
            // 封装
            lambdaQueryWrapper.like(SysRole::getRoleName,roleName);
        }

        // 3、调用方法实现分页查询
        sysRoleService.page(sysRolePage, lambdaQueryWrapper);
        return Result.ok(sysRolePage);
    }

第二种

java 复制代码
 @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    @ApiOperation("条件分页查询")
    @GetMapping("/page/{pageNum}/{pageSize}")
    public Result pageQueryUser(@PathVariable Long pageNum,
                                @PathVariable Long pageSize,
                                SysUserQueryVo sysUserQueryVo) {
        //1 创建Page对象,传递分页相关参数
        //page 当前页  limit 每页显示记录数
        Page<SysUser> pageParam = new Page<>(pageNum,pageSize);

        //2 调用service方法
        IPage<SysUser>  page =  userService.selectByPage(pageParam,sysUserQueryVo);

        return Result.success(page);
    }
xml 复制代码
<select id="selectByPage" resultMap="sysUserMap">
      SELECT * FROM  sys_user
        <where>
            <if test="vo.keyword != '' and vo.keyword != null ">
                and (username like CONCAT('%',#{vo.keyword},'%')
                or name like CONCAT('%',#{vo.keyword},'%')
                or phone like CONCAT('%',#{vo.keyword},'%'))
            </if>
            <if test="vo.createTimeBegin != null and vo.createTimeBegin !=''">
                and create_time >= #{vo.createTimeBegin}
            </if>
            <if test="vo.createTimeEnd !=null and vo.createTimeEnd !=''">
                and create_time &lt;=#{vo.createTimeEnd}
            </if>
             and is_deleted = 0
        </where>
    </select>

3.统一返回的结果

java 复制代码
package com.trs.common.result;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 公共返回对象
 *
 * @author gaoziman
 * @date 2022/8/7
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result {
    /**
     * 状态码
     */
    private Integer code;
    /**
     * 提示信息
     */
    private String message;
    /**
     * 返回对象
     */
    private Object data;


    /**
     * 成功返回结果
     *
     * @param resultCodeEnum 提示信息
     * @return 返回成功
     */
    public static Result success(ResultCodeEnum resultCodeEnum) {
        return new Result(resultCodeEnum.getCode(), resultCodeEnum.getMessage(), null);
    }

    /**
     * 成功返回结果
     *
     * @param resultCodeEnum 提示信息
     * @param data 返回数据
     * @return 返回成功
     */
    public static Result success(ResultCodeEnum resultCodeEnum, Object data) {
        return new Result(resultCodeEnum.getCode(), resultCodeEnum.getMessage(), data);
    }

    /**
     * 失败返回结果
     *
     * @param resultCodeEnum 提示信息
     * @return 返回失败
     */
    public static Result error(ResultCodeEnum resultCodeEnum) {
        return new Result(resultCodeEnum.getCode(), resultCodeEnum.getMessage(), null);
    }

    /**
     * 失败返回结果
     *
     * @param message 提示信息
     * @return 返回失败
     */
    public static Result error(String message) {
        return new Result(500, message, null);
    }

    /**
     * 失败返回结果
     *
     * @param resultCodeEnum 提示信息
     * @param data 返回数据
     * @return 返回失败
     */
    public static Result error(ResultCodeEnum resultCodeEnum, Object data) {
        return new Result(resultCodeEnum.getCode(), resultCodeEnum.getMessage(), data);
    }

    /**
     * 失败返回结果
     *
     * @param message 提示信息
     * @return 返回失败
     */
    public static Result error(ResultCodeEnum resultCodeEnum, String message) {
        return new Result(resultCodeEnum.getCode(), resultCodeEnum.getMessage(), null);
    }


    /**
     * 失败返回结果
     *
     * @param code 状态码
     * @param message 提示信息
     * @return 返回失败
     */
    public static Result error(Integer code, String message) {
        return new Result(code, message, null);
    }
}
java 复制代码
package com.trs.common.result;

import lombok.Getter;

/**
 * 统一返回结果状态信息类
 *
 * @author 高自满
 */
@Getter
public enum ResultCodeEnum {

    SUCCESS(200,"成功"),
    FAIL(201, "失败"),
    SERVICE_ERROR(2012, "服务异常"),
    DATA_ERROR(204, "数据异常"),

    LOGIN_AUTH(401, "尚未登录,请重新登录"),
    PERMISSION(209, "没有权限"),

    LOGIN_MOBLE_ERROR(210,"登录认证失败" ),
    ACCOUNT_STOP(211,"账号已停用" ),
    TOKEN_ERR(212,"token异常"),
    PERMISSION_ERR(403,"权限不足,请联系管理员"),
    COMMENT_SUCCESS(200,"评论成功,请等待管理员审核" ),
    COMMENT_SUCCESS_S(200,"评论成功" ),
    COMMENT_ERROR(501,"评论失败" );

    private Integer code;

    private String message;

    private ResultCodeEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }
}

4.Redis配置类

导入依赖

xml 复制代码
	<!-- Redis依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
java 复制代码
package com.trs.blog.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * Redis配置类
 *
 * @author : gaoziman
 * @date  2023/5/2 10:32
 */
@Configuration
public class RedisConfig {

    /**
     * Redis序列化
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        // String类型key序列器
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        // String类型value序列器
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        // Hash类型kay序列器
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        // Hash类型value序列器
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        // 设置连接工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }
}

5.代码生成器

导入依赖

xml 复制代码
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-generator</artifactId>
        <version>3.4.1</version>
    </dependency>

    <dependency>
        <groupId>org.apache.velocity</groupId>
        <artifactId>velocity-engine-core</artifactId>
        <version>2.0</version>
    </dependency>
java 复制代码
package com.trs.serviceUtil.util;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * @author gaoziman
 * 代码生成器
 */
public class CodeGet {

    public static void main(String[] args) {

        // 1、创建代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 2、全局配置
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir("E:\\exer_code\\cisyam-blog\\blog-parent\\service-blog"+"/src/main/java");

        gc.setServiceName("%sService");	//去掉Service接口的首字母I
        gc.setAuthor("manman");
        gc.setOpen(false);
        mpg.setGlobalConfig(gc);

        // 3、数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/blog?serverTimezone=GMT%2B8&useSSL=false");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root");
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);

        // 4、包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.trs");
        pc.setModuleName("blog"); //模块名
        pc.setController("controller");
        pc.setService("service");
        pc.setEntity("pojo");
        pc.setMapper("mapper");
        mpg.setPackageInfo(pc);

        // 5、策略配置
        StrategyConfig strategy = new StrategyConfig();

        strategy.setInclude("week_view");

        strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略

        strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略
        strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作

        strategy.setRestControllerStyle(true); //restful api风格控制器
        strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符

        mpg.setStrategy(strategy);

        // 6、执行
        mpg.execute();
    }
}

6.工具类

拷贝工具类

java 复制代码
package com.trs.blog.util;

import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 集合对象复制工具类
 *
 * @author gaoziman
 * @date  2023/5/1 23:32
 */
public class BeanCopyUtil {
    /**
     * 复制集合
     *
     * @param source 复制的原对象集合
     * @param clazz 对象类型
     * @param <T> 泛型
     * @return 复制完成的集合
     */
    public static <T> List<T> copyList(List source, Class<T> clazz) {
        List<T> target = new ArrayList<>();
        if (!CollectionUtils.isEmpty(source)) {
            if (!CollectionUtils.isEmpty(source)) {
                for (Object c : source) {
                    T obj = copy(c, clazz);
                    target.add(obj);
                }
            }
        }
        return target;
    }

    /**
     * 复制对象
     *
     * @param source 复制的原对象
     * @param clazz 对象类型
     * @param <T> 泛型
     * @return 复制完成的新对象
     */
    public static <T> T copy(Object source, Class<T> clazz) {
        if (source == null) {
            return null;
        }
        T obj = null;
        try {
            obj = clazz.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        BeanUtils.copyProperties(source, obj);
        return obj;
    }


    /**
     * 将传来的Object对象转为指定的List集合
     *
     * @param object 传来的Object对象
     * @param clazz 指定类型
     * @param <T> 泛型
     * @return 指定的List集合
     */
    public static <T> List<T> castObjectToList(Object object, Class<T> clazz) {
        List<T> result = new ArrayList<>();
        // 如果传来的对象属于List集合
        if (object instanceof List<?>) {
            for (Object o : (List<T>) object) {
                result.add(clazz.cast(o));
            }
            return result;
        }
        return null;
    }
}

集合工具类

java 复制代码
package com.trs.blog.util;

import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 集合工具类
 *
 * @author gaoziman
 * @date  2023/5/1 23:32
 */
public class CollectionUtil {
    /**
     * 将传来的字符串数据转为整型集合
     *
     * @param str 字符串数据
     * @return 整型集合
     */
    public static List<Integer> stringToIntegerList(String str) {
        if (StringUtils.isEmpty(str)) {
            return null;
        }
        // 去除字符串两边的[]
        str = str.substring(1, str.length() - 1);
        // 根据 , 进行分割
        String[] split = str.split(",");
        List<Integer> result = new ArrayList<>();
        for (String s : split) {
            result.add(Integer.parseInt(s));
        }
        return result;
    }
}

IP地址工具类

java 复制代码
package com.trs.blog.util;

import com.alibaba.fastjson.JSON;
import eu.bitwalker.useragentutils.UserAgent;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;

import static com.trs.common.constant.CommonConst.LOCAL_HOST;

/**
 * IP地址工具类
 *
 * @author gaoziman
 * @date  2023/5/1 23:32
 */
public class IpUtil {



    /**
     * 获取当前IP地址以及IP所属源
     *
     * @return IP地址
     */
    public static String getCurIpAddress(HttpServletRequest request) {
        String ipAddress = null;
        try {
            ipAddress = request.getHeader("x-forwarded-for");
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                // 获取到本机的远程IP地址
                ipAddress = request.getRemoteAddr();
                if (LOCAL_HOST.equals(ipAddress)) {
                    // 根据网卡取本机配置的IP
                    InetAddress inet = null;
                    try {
                        inet = InetAddress.getLocalHost();
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    }
                    ipAddress = inet.getHostAddress();
                }
            }
            // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
            if (ipAddress != null && ipAddress.length() > 15) {
                // = 15
                if (ipAddress.indexOf(",") > 0) {
                    ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
                }
            }
        } catch (Exception e) {
            ipAddress = "";
        }
        return ipAddress;
    }

    /**
     * 解析ip地址
     *
     * @param ipAddress ip地址
     * @return 解析后的ip地址
     */
    public static String getIpSource(String ipAddress) {
        try {
            URL url = new URL("http://opendata.baidu.com/api.php?query=" + ipAddress + "&co=&resource_id=6006&oe=utf8");
            BufferedReader reader = new BufferedReader(new InputStreamReader(url.openConnection().getInputStream(), "utf-8"));
            String line = null;
            StringBuffer result = new StringBuffer();
            while ((line = reader.readLine()) != null) {
                result.append(line);
            }
            reader.close();
            Map map = JSON.parseObject(result.toString(), Map.class);
            List<Map<String, String>> data = (List) map.get("data");
            return data.get(0).get("location");
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 获取用户的访问设备
     *
     * @param request 请求
     * @return 用户访问设备
     */
    public static UserAgent getUserAgent(HttpServletRequest request){
        return UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
    }
}

JWT处理Token工具类

java 复制代码
package com.trs.blog.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Jwt处理Token工具类
 *  
 * @author gaoziman
 * @date  2023/5/1 23:32
 */
@Component
public class JwtTokenUtil {
    /**
     * 用户名的key
     */
    private static final String CLAIM_KEY_USERNAME = "sub";
    /**
     * JWT的创建时间
     */
    private static final String CLAIM_KEY_CREATED = "created";
    /**
     * 秘钥
     */
    @Value("${jwt.secret}")
    private String secret;
    /**
     * token失效时间
     */
    @Value("${jwt.expiration}")
    private Long expiration;

    /**
     * 根据用户信息生成token
     *
     * @param userDetails 用户信息
     * @return 生成的token
     */
    public String generateToken(UserDetails userDetails) {
        // 定义token中存储数据的载荷
        Map<String, Object> claims = new HashMap<>();
        // 1. 用户名
        claims.put(CLAIM_KEY_USERNAME, userDetails.getUsername());
        // 2. 签发时间
        claims.put(CLAIM_KEY_CREATED, new Date());
        // 签发token
        return generateToken(claims);
    }

    /**
     * 根据载荷生成token
     *
     * @param claims 载荷
     * @return 生成的token
     */
    private String generateToken(Map<String, Object> claims) {
        return Jwts.builder()
                // 1. 设置载荷
                .setClaims(claims)
                // 2. 设置失效时间
                .setExpiration(generateExpirationDate())
                // 3. 设置签名
                .signWith(SignatureAlgorithm.HS512, secret)
                // 签发token
                .compact();
    }

    /**
     * 生成token失效时间
     *
     * @return token的失效时间
     */
    private Date generateExpirationDate() {
        // token失效时间:当前系统时间 + 自己定义的时间
        return new Date(System.currentTimeMillis() + expiration * 1000);
    }

    /**
     * 从token中获取用户名
     *
     * @param token token
     * @return 当前token中存储的用户名
     */
    public String getUserNameFromToken(String token) {
        String userName;
        try {
            // 从token中获取到载荷
            Claims claims = getClaimsFromToken(token);
            // 通过载荷获取到用户名
            userName = claims.getSubject();
        } catch (Exception e) {
            // 如果出现异常则将 userName 设置为空
            userName = null;
        }
        return userName;
    }

    /**
     * 从token中获取载荷
     *
     * @param token token
     * @return token中的载荷
     */
    private Claims getClaimsFromToken(String token) {
        Claims claims = null;
        try {
            claims = Jwts.parser()
                    // 解密的秘钥
                    .setSigningKey(secret)
                    .parseClaimsJws(token)
                    .getBody();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return claims;
    }

    /**
     * 判断token是否可以被刷新
     *
     * @param token token
     * @return token是否可以被刷新
     */
    public boolean canRefresh(String token) {
        return !isTokenExpired(token);
    }

    /**
     * 判断token是否失效
     *
     * @param token token
     * @return 当前token是否失效
     */
    private boolean isTokenExpired(String token) {
        Date expiredDate = getExpiredDateFromToken(token);
        // 如果失效时间是在当前时间之前肯定是失效的
        return expiredDate.before(new Date());
    }

    /**
     * 获取token中的失效时间
     *
     * @param token token
     * @return 当前token中的失效时间
     */
    private Date getExpiredDateFromToken(String token) {
        // 从当前token中获取载荷
        Claims claims = getClaimsFromToken(token);
        // 从载荷中返回失效时间
        return claims.getExpiration();
    }

    /**
     * 刷新token
     *
     * @param token 用户携带的 token
     * @return 刷新后的 token
     */
    public String refreshToken(String token) {
        // 从当前token中获取载荷
        Claims claims = getClaimsFromToken(token);
        // 更新载荷中的失效时间改成当前时间
        claims.put(CLAIM_KEY_CREATED, new Date());
        // 重新生成token
        return generateToken(claims);
    }

    /**
     * 判断token是否有效
     *
     * @param token 用户携带的 token
     * @param userDetails 载荷
     * @return token 是否有效
     */
    public boolean validateToken(String token, UserDetails userDetails) {
        // 通过token是否已经过期、荷载中的用户属性与userDetails中的用户属性是否一致
        String userName = getUserNameFromToken(token);
        return userName.equals(userDetails.getUsername()) && !isTokenExpired(token);
    }
}

时间转化工具类

java 复制代码
package com.trs.blog.util;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 时间转化工具类
 *
 * @author gaoziman
 * @date  2023/5/1 23:32
 */
public class TimeUtil {
    /**
     * LocalDateTime 转 String
     *
     * @param time LocalDateTime
     * @return 对应的字符串
     */
    public static String localDateTimeToString(LocalDateTime time) {
        return time.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 获取当前时间(字符串格式)
     *
     * @return 当前时间
     */
    public static String getNowTime(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(new Date());
    }

    /**
     * 获取当前时间(字符串格式)
     *
     * @return 当前时间
     */
    public static String getNowDate(){
        SimpleDateFormat df = new SimpleDateFormat("MM-dd");
        return df.format(new Date());
    }

    /**
     * String 转 LocalDate
     *
     * @param time String
     * @return LocalDate
     */
    public static LocalDate stringToLocalDateDay(String time){
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return LocalDate.parse(time, df);
    }

    /**
     * String 转 LocalDate
     *
     * @param time String
     * @return LocalDate
     */
    public static LocalDate stringToLocalDate(String time){
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return LocalDate.parse(time, df);
    }

    /**
     * String 转 LocalDateTime
     *
     * @param time String
     * @return LocalDateTime
     */
    public static LocalDateTime stringToLocalDateTimeDay(String time){
        // 先转LocalDate ===> 再转LocalDateTime
        LocalDate localDate = stringToLocalDateDay(time);
        return localDate.atTime(LocalTime.now());
    }

    /**
     * String转LocalDateTime (yyyy-MM-dd HH:mm:ss格式)
     *
     * @param time String格式时间
     * @return LocalDateTime
     */
    public static LocalDateTime stringToLocalDateTime(String time){
        // 先转LocalDate ===> 再转LocalDateTime
        LocalDate localDate = stringToLocalDate(time);
        return localDate.atTime(LocalTime.now());
    }

    /**
     * LocalDate转String
     *
     * @param time LocalDate
     * @return String
     */
    public static String localDateToString(LocalDate time) {
        return time.format(DateTimeFormatter.ofPattern("MM-dd"));
    }

}

UUID生成工具类

java 复制代码
package com.trs.blog.util;

import java.util.UUID;

/**
 * UUID生成工具类
 *
 * @author gaoziman
 * @date  2023/5/1 23:32
 */
public class UuidUtil {

    public static String[] chars = new String[]{"a", "b", "c", "d", "e", "f",
            "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
            "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z"
    };

    /**
     * 获取短UUID
     *
     * @return 16位UUID
     */
    public static String getShortUuid() {
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UuidUtil.getUuid();
        for (int i = 0; i < 8; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            // 对62取余
            shortBuffer.append(chars[x % 0x3E]);
        }
        return shortBuffer.toString();

    }

    /**
     * 获得32位UUID
     */
    public static String getUuid() {
        String uuid = UUID.randomUUID().toString();
        //去掉"-"符号
        return uuid.replaceAll("-", "");
    }

}

MD5工具类

java 复制代码
package com.trs.common.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


public final class MD5 {

    public static String encrypt(String strSrc) {
        try {
            char hexChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
                    '9', 'a', 'b', 'c', 'd', 'e', 'f' };
            byte[] bytes = strSrc.getBytes();
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(bytes);
            bytes = md.digest();
            int j = bytes.length;
            char[] chars = new char[j * 2];
            int k = 0;
            for (int i = 0; i < bytes.length; i++) {
                byte b = bytes[i];
                chars[k++] = hexChars[b >>> 4 & 0xf];
                chars[k++] = hexChars[b & 0xf];
            }
            return new String(chars);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            throw new RuntimeException("MD5加密出错!!+" + e);
        }
    }

    public static void main(String[] args) {
        System.out.println(MD5.encrypt("123456"));
    }
}
 

7.knife4j文档

引入依赖

java 复制代码
<!--knife4j-->
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
</dependency>
java 复制代码
package com.trs.serviceUtil.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;

/**
 * knife4j配置信息
 */
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {

    @Bean
    public Docket adminApiConfig(){
        List<Parameter> pars = new ArrayList<>();
        ParameterBuilder tokenPar = new ParameterBuilder();
        tokenPar.name("token")
                .description("用户token")
                .defaultValue("")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();
        pars.add(tokenPar.build());
        //添加head参数end

        Docket adminApi = new Docket(DocumentationType.SWAGGER_2)
                .groupName("adminApi")
                .apiInfo(adminApiInfo())
                .select()
                //只显示admin路径下的页面
                .apis(RequestHandlerSelectors.basePackage("com.trs"))
                .paths(PathSelectors.regex("/.*"))
                .build()
                .globalOperationParameters(pars);
        return adminApi;
    }

    private ApiInfo adminApiInfo(){

        return new ApiInfoBuilder()
                .title("后台管理系统-API文档")
                .description("本文档描述了个人博客后台管理系统接口定义")
                .version("1.0")
                .contact(new Contact("manman", "http://manamn.space", "2942894660@qq.com"))
                .build();
    }


}

8.文件上传

导入依赖

xml 复制代码
<!-- 阿里云OSS文件上传开始 -->
        <!-- 阿里云 OSS -->
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>3.14.1</version>
        </dependency>
        <!--日期时间工具-->
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.10.14</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <!-- 阿里云OSS文件上传结束 -->
yml 复制代码
#阿里云文件上传
aliyun:
  oss:
    endpoint: ************
    keyid: ************
    keysecret:  ************
    bucketname: ************

配置类读取配置文件

java 复制代码
package com.trs.demo.oss;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * oss属性
 */
@Data
@Component
@ConfigurationProperties(prefix = "aliyun.oss")
public class OssProperties {
    private String endPoint;
    private String keyId;
    private String keySecret;
    private String bucketName;
}

Controller

java 复制代码
package com.trs.demo.controller;

import com.trs.demo.service.FileService;
import com.trs.demo.util.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
@RequestMapping("/oss/file")
public class OSSController {
    @Resource
    private FileService fileService;

    /**
     * 文件上传
     *
     * @param file
     * @param module
     * @return
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile file, String module) {
        //返回上传到oss的路径
        String url = fileService.upload(file, module);
        return Result.ok(url).message("文件上传成功");
    }
}

Service

java 复制代码
package com.trs.demo.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.trs.demo.oss.OssProperties;
import com.trs.demo.service.FileService;
import org.apache.commons.io.FilenameUtils;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * @author : gaoziman
 * @description :
 * @date 2023/5/8 9:47
 */
@Service
public class FileServiceImpl  implements FileService {
    /**
     * oss属性
     */
    @Resource
    private OssProperties ossProperties;

    /**
     * 文件上传
     *
     * @param file   文件上传对象
     * @param module 文件夹名称
     * @return {@link String}
     */
    @Override
    public String upload(MultipartFile file, String module) {
        //获取地域节点
        String endPoint = ossProperties.getEndPoint();
        //获取AccessKeyId
        String keyId = ossProperties.getKeyId();
        //获取AccessKeySecret
        String keySecret = ossProperties.getKeySecret();
        //获取BucketName
        String bucketName = ossProperties.getBucketName();
        try {
            //创建OSSClient实例
            OSS ossClient = new OSSClientBuilder().build(endPoint, keyId,
                    keySecret);
            //上传文件流
            InputStream inputStream = file.getInputStream();
            //获取旧名称
            String originalFilename = file.getOriginalFilename();
            //获取文件后缀名
            String extension = FilenameUtils.getExtension(originalFilename);
            //将文件名重命名
            String newFileName = UUID.randomUUID().toString()
                    .replace("-", "") + "." + extension;
            //使用当前日期进行分类管理
            String datePath = new DateTime().toString("yyyy/MM/dd");
            //构建文件名
            newFileName = module + "/" + datePath + "/" + newFileName;
            //调用OSS文件上传的方法
            ossClient.putObject(bucketName, newFileName, inputStream);
            //关闭OSSClient
            ossClient.shutdown();
            //返回文件地址
            return "https://" + bucketName + "." + endPoint + "/" + newFileName;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 删除文件
     *
     * @param url url
     */
    @Override
    public void deleteFile(String url) {
        //获取地域节点
        String endPoint = ossProperties.getEndPoint();
        //获取AccessKeyId
        String keyId = ossProperties.getKeyId();
        //获取AccessKeySecret
        String keySecret = ossProperties.getKeySecret();
        //获取BucketName
        String bucketName = ossProperties.getBucketName();
        try {
            //创建OSSClient实例
            OSS ossClient = new OSSClientBuilder().build(endPoint, keyId, keySecret);
            //组装文件地址
            String host = "https://" + bucketName + "." + endPoint + "/";
            //获取文件名称
            String objectName = url.substring(host.length());
            //删除文件
            ossClient.deleteObject(bucketName, objectName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
相关推荐
Justinc.几秒前
CSS3新增边框属性(五)
前端·css·css3
fruge8 分钟前
纯css制作声波扩散动画、js+css3波纹催眠动画特效、【css3动画】圆波扩散效果、雷达光波效果完整代码
javascript·css·css3
neter.asia17 分钟前
vue中如何关闭eslint检测?
前端·javascript·vue.js
~甲壳虫17 分钟前
说说webpack中常见的Plugin?解决了什么问题?
前端·webpack·node.js
光影少年36 分钟前
vue2与vue3的全局通信插件,如何实现自定义的插件
前端·javascript·vue.js
As977_37 分钟前
前端学习Day12 CSS盒子的定位(相对定位篇“附练习”)
前端·css·学习
susu108301891140 分钟前
vue3 css的样式如果background没有,如何覆盖有background的样式
前端·css
Ocean☾41 分钟前
前端基础-html-注册界面
前端·算法·html
Rattenking41 分钟前
React 源码学习01 ---- React.Children.map 的实现与应用
javascript·学习·react.js
Dragon Wu43 分钟前
前端 Canvas 绘画 总结
前端