分页查询
page()
分页查询数据,需要传两个参数,第几页和每页多少条
- @param
current
第几页 - @param
size
每页显示多少条 - (current: number = 1, size: number = 10)
page()
import { Db } from 'think-ts-db'
export default defineEventHandler(async (event) => {
const user = await Db('user', mysqlConfig).page(1, 10).select()
return user
})
import { Db } from 'think-ts-db'
export default defineEventHandler(async (event) => {
const user = await Db('user', mysqlConfig).page(1, 10).select()
return user
})
最终的SQL语句为:
SELECT * FROM user LIMIT 0, 10
SELECT * FROM user LIMIT 0, 10
常用技巧
import { Db } from 'think-ts-db'
export default defineEventHandler(async (event) => {
const { phone } = await readBody(event)
const m = Db('user', mysqlConfig)
if (phone) {
m.where('phone', phone)
}
const list = await m.page(1, 10).select()
const total = await m.count()
return {
total,
list
}
})
import { Db } from 'think-ts-db'
export default defineEventHandler(async (event) => {
const { phone } = await readBody(event)
const m = Db('user', mysqlConfig)
if (phone) {
m.where('phone', phone)
}
const list = await m.page(1, 10).select()
const total = await m.count()
return {
total,
list
}
})