看看我如何用无极低码秒掉Springboot+mybatis组合


先看图,大家看看这个界面,根据这个界面我们简单看一下,这个界面要写接口的话,接口需要实现分组查询、模糊检索、热门查询、最新查询、分页查询。但是我一行代码也没写。

​​​​在这里插入图片描述

假如我要写代码的话接口该怎么写?

举个相似的例子

要实现这样一个综合性的新闻查询接口,我们将通过Spring Boot和MyBatis来设计各个层次的代码,确保既能单独实现每种查询,也能灵活组合查询条件。以下是分步骤的实现方案:

通过一下代码,您可以实现以下功能:

  • 查询全部数据(不传任何参数)
  • 分组查询(通过columnId
  • 标题模糊检索(通过title
  • 热门查询(通过设置hot=true
  • 最新查询(通过设置latest=true
  • 分页查询(通过pagesize
  • 以及这些条件的任意组合查询。

请根据您的实际数据库表结构调整SQL语句和实体类属性。

1. 实体类 (News.java)

package com.example.news.entity;

import java.util.Date;

public class News {
    private Long id;
    private String title;
    private String content;
    private Long columnId; // 栏目ID
    private Integer views; // 浏览量
    private Date publishTime; // 发布时间

    // Getter and Setter methods
}

2. MyBatis Mapper 接口 (NewsMapper.java)

package com.example.news.mapper;

import com.example.news.entity.News;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface NewsMapper {
    @SelectProvider(type = NewsSqlBuilder.class, method = "buildSearchQuery")
    List<News> searchNewsWithConditions(Map<String, Object> params);

    @Select("SELECT * FROM news ORDER BY views DESC LIMIT #{limit}")
    List<News> findHotNews(@Param("limit") int limit);

    @Select("SELECT * FROM news ORDER BY publish_time DESC LIMIT #{limit}")
    List<News> findLatestNews(@Param("limit") int limit);
}

class NewsSqlBuilder {
    public static String buildSearchQuery(Map<String, Object> params) {
        StringBuilder sql = new StringBuilder("SELECT * FROM news WHERE 1=1");
        
        if (params.containsKey("title")) {
            sql.append(" AND title LIKE CONCAT('%', #{title}, '%')");
        }
        if (params.containsKey("columnId")) {
            sql.append(" AND column_id = #{columnId}");
        }
        if (params.containsKey("isLatest")) {
            sql.append(" ORDER BY publish_time DESC");
        } else if (params.containsKey("isHot")) {
            sql.append(" ORDER BY views DESC");
        }
        if (params.containsKey("page") && params.containsKey("size")) {
            int offset = (Integer) params.get("page") * (Integer) params.get("size");
            sql.append(" LIMIT #{offset}, #{size}");
        }
        
        return sql.toString();
    }
}

3. 新闻服务类 (NewsService.java)

package com.example.news.service;

import com.example.news.entity.News;
import com.example.news.mapper.NewsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class NewsService {
    private final NewsMapper newsMapper;

    @Autowired
    public NewsService(NewsMapper newsMapper) {
        this.newsMapper = newsMapper;
    }

    public List<News> searchNews(String title, Long columnId, boolean isLatest, boolean isHot, int page, int size) {
        Map<String, Object> params = new HashMap<>();
        params.put("title", title);
        params.put("columnId", columnId);
        params.put("isLatest", isLatest);
        params.put("isHot", isHot);
        if (page > 0 && size > 0) {
            params.put("page", (page - 1));
            params.put("size", size);
        }
        return newsMapper.searchNewsWithConditions(params);
    }

    // 其他方法如findHotNews, findLatestNews...
}

4. 控制器类 (NewsController.java)

package com.example.news.controller;

import com.example.news.entity.News;
import com.example.news.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class NewsController {
    private final NewsService newsService;

    @Autowired
    public NewsController(NewsService newsService) {
        this.newsService = newsService;
    }

    @GetMapping("/news")
    public List<News> searchNews(
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "columnId", required = false) Long columnId,
            @RequestParam(value = "latest", defaultValue = "false") boolean latest,
            @RequestParam(value = "hot", defaultValue = "false") boolean hot,
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "size", defaultValue = "10") int size) {
        return newsService.searchNews(title, columnId, latest, hot, page, size);
    }

    // 可以添加单独的路由来直接调用findHotNews和findLatestNews
}

这时候我们来看我如何不写一行代码实现以上代码实现的接口,用下面这一段就可以实现如上代码实现的功能了


select
===
select id ,title as t,latest cTime,views from d_chart where 1=1 
∮byid 
  and id='#byid#'
∮bytitle 
  and title like'%#bytitle#%'
∮bygid
  and groupid =#bygid#
∮byhot
  order by views #byhot#
∮bynew
  order by latest #bynew#
;

没错就是这么神奇,无极低码通过自定义sql引擎模板实现低代码开发。这样的代码,开发简单,维护简单,调试简单,会sql1分钟就可以学会服务编写。更多使用教程请关注无极低码https://wheart.cn

最近更新

  1. TCP协议是安全的吗?

    2024-04-24 00:56:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-24 00:56:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-24 00:56:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-24 00:56:03       18 阅读

热门阅读

  1. mingw静态链接(libgcc_s_seh-1,libstdc++-6和 libwinpthread-1)

    2024-04-24 00:56:03       12 阅读
  2. [Android]设置尺寸时的单位选择

    2024-04-24 00:56:03       13 阅读
  3. Qt:实现TCP同步与异步读写消息

    2024-04-24 00:56:03       12 阅读
  4. 题解:力扣704/35/34

    2024-04-24 00:56:03       12 阅读
  5. FineBI概述

    2024-04-24 00:56:03       11 阅读
  6. Hdu3118 Arbiter【二进制枚举建二分图】

    2024-04-24 00:56:03       11 阅读
  7. Flutter第十弹 ScrollView滚动组件

    2024-04-24 00:56:03       16 阅读