搭建admin.service

发布于:2025-08-11 ⋅ 阅读:(14) ⋅ 点赞:(0)
dto

不分页:com.heima.common.dtos.ResponseResult

package com.heima.common.dtos;

import com.heima.common.enums.AppHttpCodeEnum;
import lombok.Data;

import java.io.Serializable;

/**
 * 通用的结果返回类
 * @param <T>
 */
@Data
public class ResponseResult<T> implements Serializable {

    private String host;

    private Integer code;

    private String errorMessage;

    private T data;

    public ResponseResult() {
        this.code = 0;
    }

    public ResponseResult(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    public ResponseResult(Integer code, String msg, T data) {
        this.code = code;
        this.errorMessage = msg;
        this.data = data;
    }

    public ResponseResult(Integer code, String msg) {
        this.code = code;
        this.errorMessage = msg;
    }

    public static ResponseResult ok() {
        return ok(null);
    }

    public static <T> ResponseResult ok(T data) {
        return new ResponseResult(0, data);
    }

    public static ResponseResult error(int code, String msg) {
        return new ResponseResult(code, msg);
    }

    public static ResponseResult error(AppHttpCodeEnum enums) {
        return new ResponseResult(enums.getCode(), enums.getErrorMessage());
    }

    public static ResponseResult error(AppHttpCodeEnum enums, String errorMessage) {
        return new ResponseResult(enums.getCode(), errorMessage);
    }
}

分页通用返回:com.heima.common.dtos.PageResult

package com.heima.common.dtos;

import lombok.Data;


@Data
public class PageResult<T> extends ResponseResult {

    
    private Long currentPage;
    private Long size;
    private Long total;
    private List<T> data;
    

    public PageResult(Long currentPage, Long size, Long total, List<T> data)
    {
        this.currentPage = currentPage;
        this.size = size;
        this.total = total;
        this.data = data;
    }

}
通用的请求dtos
package com.heima.common.dtos;

import lombok.Setter;

@Setter
public class PageRequestDto {
    protected Long size;
    protected Long page;

    public Long getSize() {
        if (this.size == null || this.size <= 0 || this.size > 100) {
            setSize(10L);
        }
        return size;
    }

    public Long getPage() {
        if (this.page == null || this.page <= 0) {
            setPage(1L);
        }
        return page;
    }
}
通用的异常枚举
public enum AppHttpCodeEnum {

    // 成功段0
    SUCCESS(0,"操作成功"),
    // 登录段1~50
    NEED_LOGIN(1,"需要登录后操作"),
    LOGIN_PASSWORD_ERROR(2,"密码错误"),
    // TOKEN50~100
    TOKEN_INVALID(50,"无效的TOKEN"),
    TOKEN_EXPIRE(51,"TOKEN已过期"),
    TOKEN_REQUIRE(52,"TOKEN是必须的"),
    // SIGN验签 100~120
    SIGN_INVALID(100,"无效的SIGN"),
    SIG_TIMEOUT(101,"SIGN已过期"),
    // 参数错误 500~1000
    PARAM_REQUIRE(500,"缺少参数"),
    PARAM_INVALID(501,"无效参数"),
    PARAM_IMAGE_FORMAT_ERROR(502,"图片格式有误"),
    SERVER_ERROR(503,"服务器内部错误"),
    UPDATE_ERROR(504,"数据更新异常"),
    TEXT_ILLEGAL(505,"文本内容不符合规范" )
    // 数据错误 1000~2000
    DATA_EXIST(1000,"数据已经存在"),
    AP_USER_DATA_NOT_EXIST(1001,"ApUser数据不存在"),
    DATA_NOT_EXIST(1002,"数据不存在"),
    // 数据错误 3000~3500
    NO_OPERATOR_AUTH(3000,"无权限操作");

    int code;
    String errorMessage;

    AppHttpCodeEnum(int code, String errorMessage){
        this.code = code;
        this.errorMessage = errorMessage;
    }

    public int getCode() {
        return code;
    }

    public String getErrorMessage() {
        return errorMessage;
    }
}

搭建平台运营微服务admin

  • com.heima.${模块名称}为基础包名 如平台管理就是 com.heima.admin

pom.xml

<dependencies>
    <!-- 引入依赖模块 -->
    <dependency>
        <groupId>com.heima</groupId>
        <artifactId>heima-leadnews-model</artifactId>
    </dependency>
    <dependency>
        <groupId>com.heima</groupId>
        <artifactId>heima-leadnews-common</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
    </dependency>
     <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
</dependencies>
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

application.yml

server:
  port: 9001
spring:
  application:
    name: leadnews-admin
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/leadnews_admin?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false
    username: root
    password: root
mybatis-plus:
  # 设置Mapper接口所对应的XML文件位置,如果你在Mapper接口中有自定义方法,需要进行该配置
  #mapper-locations: classpath*:mapper/*.xml
  # 设置别名包扫描路径,通过该属性可以给包中的类注册别名
  #type-aliases-package: com.heima.admin.entity
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #输出sql日志

引导类

package com.heima.admin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@MapperScan("com.heima.admin.mapper")
public class AdminApplication {

    public static void main(String[] args) {
        SpringApplication.run(AdminApplication.class,args);
    }
    
    /**
     * MybatisPlus分页配置
     * @return
     */
    @Bean
    public MybatisPlusInterceptor paginationInterceptor() {

        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return  interceptor;
    }
}

admin-频道管理

ad_channel 频道表

package com.heima.admin.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 * <p>
 * 频道信息表
 * </p>
 *
 * @author HM
 * @since 2020-11-27
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class AdChannel extends Model<AdChannel> {

private static final long serialVersionUID=1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    /**
     * 频道名称
     */
    private String name;

    /**
     * 频道描述
     */
    private String description;

    /**
     * 是否默认频道
     */
    private Boolean isDefault;
    /**
     * 是否启用 0- 不启用 1-启用
     */
    private Boolean status;

    /**
     * 默认排序
     */
    private Integer ord;

    /**
     * 创建时间
     */
    private Date createdTime;


    @Override
    protected Serializable pkVal() {
        return this.id;
    }

}

频道列表查询Dto因为需要分页查询,要继承PageRequestDto

package com.heima.model.admin.dtos;

import com.heima.model.common.dtos.PageRequestDto;
import lombok.Data;

@Data
public class ChannelDto extends PageRequestDto {

    private Integer id;
    /**
     * 频道名称
     */
    private String name;

    /**
     * 频道描述
     */
    private String description;

    /**
     * 是否默认频道
     */
    private Boolean isDefault;

    private Boolean status;

    /**
     * 默认排序
     */
    private Integer ord;
}
持久层
package com.heima.admin.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.heima.model.admin.entity.AdChannel;

public interface AdChannelMapper extends BaseMapper<AdChannel> {
}
业务层
package com.heima.admin.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.entity.AdChannel;
import com.heima.common.dtos.PageResult;

public interface AdChannelService extends IService<AdChannel> {

    /**
     * 根据名称分页查询频道列表
     * @param dto
     * @return
     */
    PageResult<ChannelDto> findByPage(ChannelDto dto);
}
控制层
package com.heima.admin.controller.v1;

import com.heima.admin.service.AdChannelService;
import com.heima.common.dtos.PageResult;
import com.heima.model.admin.dtos.ChannelDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/channel")
public class ChannelController {

    @Autowired
    private AdChannelService channelService;

    /**
     * 分页查询
     * @param dto
     * @return
     */
    @PostMapping("/list")
    public PageResult<ChannelDto> findByPage(@RequestBody ChannelDto dto){
        return channelService.findByPage(dto);
    }
}


网站公告

今日签到

点亮在社区的每一天
去签到