kotlin+JPA传参自定义分页

controller

package com.umh.doctorreferral.api.controller

import com.umh.doctorreferral.api.model.BookingDto
import com.umh.doctorreferral.api.model.BookingParam
import com.umh.doctorreferral.api.service.ApiBookingCaseService
import io.swagger.annotations.ApiResponse
import io.swagger.annotations.ApiResponses
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.web.bind.annotation.*
import java.util.*

/**
 * @Description
 * @Author hunagzp
 * @Date 2021/7/22 17:26
 *
 */
@RestController
@Tag(name = "预约记录API", description = "预约记录API")
class BookingController {

    @Autowired
    private lateinit var apiBookingCaseService : ApiBookingCaseService

    @Operation(summary = "获取已绑定预约记录", description = "获取已绑定预约记录")
    @GetMapping("/referral-cases/{referralCaseId}/bookings", params = ["page", "size"])
    @ApiResponses(
            ApiResponse(code = 200, message = "OK", response = BookingDto::class)
    )
    fun referralCaseBookings(@PathVariable referralCaseId: UUID, pageable: Pageable): Page<BookingDto> {
        return apiBookingCaseService.getBooking(referralCaseId,pageable)
    }

    
}

service

package com.umh.doctorreferral.api.service


import com.umh.doctorreferral.api.model.BookingDto
import com.umh.doctorreferral.core.model.doctorreferralcase.DoctorReferralCaseBookingMap
import com.umh.doctorreferral.core.model.doctorreferralcase.DoctorReferralCaseBookingMapRepository
import io.swagger.v3.oas.annotations.Operation
import org.slf4j.LoggerFactory
import org.springframework.beans.BeanUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Sort
import org.springframework.stereotype.Service
import java.util.*
import javax.persistence.EntityManager
import javax.persistence.PersistenceContext

/**
 * @Description
 * @Author hunagzp
 * @Date 2021/7/22 16:04
 *
 */
@Service
class ApiBookingCaseService {

    @Autowired
    private lateinit var doctorReferralCaseBookingMapRepository: DoctorReferralCaseBookingMapRepository

    @PersistenceContext
    private lateinit var em: EntityManager

    private val logger = LoggerFactory.getLogger(this.javaClass)

    @Operation(summary = "获取已绑定预约记录", description = "获取已绑定预约记录")
    fun getBooking(referralCaseId: UUID, pageable: Pageable): Page<BookingDto> {
        //定义分页类型
        val page : Pageable
        val orders = ArrayList<Sort.Order>()
        orders.add(Sort.Order(Sort.Direction.DESC, "createdDate"))
        page = PageRequest.of(pageable.pageNumber, pageable.pageSize, Sort.by(orders))
        //查找数据并分页
        val booking = doctorReferralCaseBookingMapRepository.pageBooking(referralCaseId,page)

        //return booking.map { convertToBookingDto(it) }   方法一
        //方法二
        return booking.map { BookingDto().apply {
            BeanUtils.copyProperties(it, this)
            this.referralCaseId = it.doctorReferralCaseId
        } }
    }
    fun convertToBookingDto(doctorReferralCaseBookingMap: DoctorReferralCaseBookingMap): BookingDto {
        return BookingDto().apply {
            BeanUtils.copyProperties(doctorReferralCaseBookingMap, this)
            //TODO bookingDate, bookingTime, clinicName, doctoeName, services
            this.referralCaseId = doctorReferralCaseBookingMap.doctorReferralCaseId
        }
    }




}
JpaRepository
package com.umh.doctorreferral.core.model.doctorreferralcase

import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import java.util.*

interface DoctorReferralCaseBookingMapRepository : JpaRepository<DoctorReferralCaseBookingMap, UUID> {

    @Query("select b from DoctorReferralCaseBookingMap b where b.doctorReferralCaseId=?1")
    fun pageBooking(referralCaseId: UUID, page: Pageable): Page<DoctorReferralCaseBookingMap>

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值