基于Springboot实现微信小程序(后端)

基于Springboot实现微信小程序(后端)

项目依赖

MyBatis FrameworkMySQL DriverLombok以及,Spring Web

<!-- pom.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.lysssyo</groupId>
    <artifactId>WXMiniProam</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>WXMiniProam</name>
    <description>WXMiniProam</description>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.6.13</spring-boot.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>net.sourceforge.htmlunit</groupId>
            <artifactId>htmlunit</artifactId>
            <version>2.55.0</version>
        </dependency>
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.8.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <configuration>
                    <mainClass>com.lysssyo.WxMiniProamApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

项目配置

#application.properties
# 应用服务 WEB 访问端口
server.port=80

#数据库连接
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://数据库IP/DatabaseForWX
spring.datasource.username=用户名
spring.datasource.password=密码

#开启mybatis的日志输出
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

#开启数据库表字段 到 实体类属性的驼峰映射
mybatis.configuration.map-underscore-to-camel-case=true

项目结构

在这里插入图片描述

WXCrawlingService是为了实现其他功能

代码实现

​ 后端其实较为简单,只需要把前端请求的数据给前端就好了,不用做过多的数据处理:

DocController:

@Slf4j
@RestController
public class DocController {
    @Autowired
    private DocService docService;
    
    @GetMapping("/all")
    public Result AllData(){
        log.info("查询所有数据");
        List<Doc> DocList=docService.AllData();
        return Result.success(DocList);
    }
    
    @GetMapping("/getPage/{style}")
    public Result PageData(@PathVariable Integer style){
        log.info("根据路径参数page查询对应的页面数据");
        List<Doc> DocList=docService.pageData(style);
        return Result.success(DocList);
    }
    
    @GetMapping("/getDoc/{id}")
    public Result getDocMain(@PathVariable Integer id){
        List<Doc> DocMain=docService.getDocMain(id);
        return Result.success(DocMain);
    }
}

DocServiceImpl:

@Service
public class DocServiceImpl implements DocService {
    @Autowired
    private DocMapper docMapper;
    public List<Doc> AllData() {
        List<Doc> docList = docMapper.AllData();
        return docList;
    }
    @Override
    public List<Doc> pageData(Integer style) {
        List<Doc> docList = docMapper.pageData(style);
        return docList;
    }
    @Override
    public List<Doc> getDocMain(Integer id) {
        List<Doc> docList = docMapper.getDoc(id);
        return docList;
    }
}

mapper:

@Mapper
public interface DocMapper {
    //查询表中所有数据
    @Select("select id,style,tittle,updateTime,createTime,DocMain from Doc")
    List<Doc> AllData();

    //根据参数style,查询页面数据
    @Select("select id,style,tittle,updateTime from Doc where style=#{style}")
    List<Doc> pageData(Integer style);

    //根据id,查询文档
    @Select("select id,style,tittle,updateTime,createTime,DocMain from Doc where id=#{id}")
    List<Doc> getDoc(Integer id);
}

pojo中的Doc:

public class Doc {
    private short id;//文档的id
    private short style;//文档的类型
    private String tittle;//文档的标题
    private String createTime;//文档的上传时间
    private String updateTime;//文档的修改时间
    private String DocMain;//文档的内容
}

请求与响应

1. 进入页面时发起网络请求

1.1 基本信息

请求路径:/getPage

请求方式:GET

接口描述:该接口用于进入小程序页面时的文档预览

1.2 请求参数

参数格式:路径参数

参数说明:

参数名类型是否必须备注
stylenumber必须页面的ID

请求参数样例:

/getPage/1

1.3响应数据

参数格式:application/json

参数说明:

参数名类型是否必须备注
codenumber必须响应码,1 代表成功,0 代表失败
msgstring非必须提示信息
dataobject[ ]必须返回的数据
|-idnumber必须文件对应的id
|- tittlestring必须文件名
|- updateTimestring必须修改时间

2. 跳转markdown页面时发起网络请求

2.1 基本信息

请求路径:/getDoc

请求方式:GET

接口描述:该接口用于获取markdown文件

2.2 请求参数

参数格式:路径参数

参数说明:

参数名类型是否必须备注
idnumber必须文档的id

请求参数样例:

/getDoc/1

2.3响应数据

参数格式:application/json

参数说明:

参数名类型是否必须备注
codenumber必须响应码,1 代表成功,0 代表失败
msgstring非必须提示信息
dataobject[ ]必须返回的数据
|-docMainstring必须markdown文件

数据库

​ 数据库中仅用存一张表:

在这里插入图片描述

CREATE TABLE Doc
(
    id INT AUTO_INCREMENT PRIMARY KEY comment '文档的id',
    style INT NOT NULL comment '文档的类型,0:前端,1:后端,2:算法,3:我的',
    title VARCHAR(50) NOT NULL comment '文档的标题',
    updateTime DATE NOT NULL comment '修改时间',
    createTime DATE NOT NULL comment '上传时间',
    DocMain MEDIUMTEXT NOT NULL comment '文档的主要内容'
)
    COMMENT='微信小程序数据库表';

​ 如果要上传新的文档,需要在数据库表中新增一行。

  • 25
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果您下载了本程序,但是该程序存在问题无法运行,那么您可以选择退款或者寻求我们的帮助(如果找我们帮助的话,是需要追加额外费用的)。另外,您不会使用资源的话(这种情况不支持退款),也可以找我们帮助(需要追加额外费用) 微信小程序是腾讯公司基于微信平台推出的一种轻量级应用形态,它无需用户下载安装即可在微信内直接使用。自2017年正式上线以来,小程序凭借其便捷性、易获取性和出色的用户体验迅速获得市场认可,并成为连接线上线下服务的重要桥梁。 小程序的核心特点包括: 零安装:用户只需通过微信扫一扫或搜索功能,即可打开和使用小程序,大大降低了用户的使用门槛和手机存储空间压力。 速度快:加载速度相较于传统的HTML5网页更快,依托于微信强大的基础设施,能够实现近乎原生应用的流畅体验。 跨平台兼容:开发者一次开发,即可在多种终端设备上运行,免除了复杂的适配工作,大大提高了开发效率。 社交属性强:小程序可以无缝嵌入微信生态,支持分享至聊天窗口、朋友圈等社交场景,有利于用户间的传播和裂变增长。 丰富接口能力:提供丰富的API接口,可调用微信支付、位置服务、用户身份识别等多种功能,方便企业进行商业服务的集成与拓展。 目前,微信小程序已经覆盖了电商购物、生活服务、娱乐休闲、教育学习、工具助手等多个领域,为数以亿计的用户提供便捷的服务入口,也为众多商家和开发者提供了新的商业模式和创业机会。随着技术的不断升级和完善,小程序已成为现代移动互联网生态中不可或缺的一部分。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值