先看图,大家看看这个界面,根据这个界面我们简单看一下,这个界面要写接口的话,接口需要实现分组查询、模糊检索、热门查询、最新查询、分页查询。但是我一行代码也没写。
假如我要写代码的话接口该怎么写?
举个相似的例子
要实现这样一个综合性的新闻查询接口,我们将通过Spring Boot和MyBatis来设计各个层次的代码,确保既能单独实现每种查询,也能灵活组合查询条件。以下是分步骤的实现方案:
通过一下代码,您可以实现以下功能:
- 查询全部数据(不传任何参数)
- 分组查询(通过
columnId
) - 标题模糊检索(通过
title
) - 热门查询(通过设置
hot=true
) - 最新查询(通过设置
latest=true
) - 分页查询(通过
page
和size
) - 以及这些条件的任意组合查询。
请根据您的实际数据库表结构调整SQL语句和实体类属性。文章来源:https://www.toymoban.com/news/detail-860819.html
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文章来源地址https://www.toymoban.com/news/detail-860819.html
到了这里,关于看看我如何用无极低码秒掉Springboot+mybatis组合的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!