搞開發,寫SQL就夠了

Rocket-API

WEB開發框架。傳統業務需求,大部分邏輯簡單,咱們常說的CRUD,搬磚等,基本可以直接經過寫SQL來完成定製開發,關於這部分需求,ROCKET-API提供瞭解決方案git

取個例子:

表:user(id,name,sex,phone,create_time) 表:book(id,name) 表:user_book(user_id,book_id)sql

需求1:對用戶表進行分頁查詢,必填參數create_time時間範圍內,可選參數:name 模糊匹配
在ROCKET-API中實現爲:
sql = """
select id,name from user  where create_time between #{startTime} and #{endTime}
?{name,and name like concat('%',#{name},'%')}
"""
db.pager(sql)
完整ROCKET-API中截圖:

在這裏插入圖片描述 在這裏插入圖片描述

說明:api

  1. sql部分很像mybatis語法,#{}表示參數替換,?{}:表示可選參數替換(與mybatis if語法一致)
  2. db.pager() 將參數與sql進行綁定
  3. 而參數的來源爲直接是HTTP請求的參數:http://localhost:8088/user/page?startTime=2020-07-12 00:00:00&endTime=2020-10-12 00:00:00,省略參數接收,VO定義等
需求2:對用戶表進行分頁查詢,返回用戶姓名,用戶擁有的書籍(多個用逗號分隔),必填參數create_time時間範圍內,可選參數:name 模糊匹配
在ROCKET-API中實現爲:
sql = """
select t1.name,group_concat(t3.name) books
(
select id,name from user  where create_time between #{startTime} and #{endTime}
?{name,and name like concat('%',#{name},'%')}
) t1 
left join user_book t2 on t1.id = t2.user_id
left join book t3 on t3.id = t2.book_id
group by t1.id
"""
db.pager(sql)

說明:mybatis

  1. 能用SQL搞定的,就堅持不寫代碼

官方地址: https://gitee.com/alenfive/rocket-api框架

相關文章
相關標籤/搜索