查询

下面的实例涉及了分页、Count计算等

     @Test
    fun testReadWithPage() {

        val book = TestBook()
        book.testName = "testnamevalue"

        val q = OQL.From(book).Limit(10, 2, true).Select().Where {
            cmp ->
            cmp.Comparer(book.testId, ">", "1")
        }.END

        val count = book.takePageCountAll()
        q.PageWithAllRecordCount = count

        val resultList = getDB().select<TestBook>(q)
        resultList?.forEach {
            println("条目 = ${it.testId},${it.testURL}, ${it.testName},${it.testCount}")
        }
    }

    其中:
Limit(10, 2, true)参数意义:10:每页10条,2:取第二页,true:执行精确分页计算,count值可以通过book.takePageCountAll()获取

count计算
|-----------------------------------------------------------|
|  finalSql      |  SELECT count(1) FROM                    |
|                |                                          |
|                |   (SELECT  *                             |
|                |                                          |
|                |  FROM `test_book`                        |
|                |                                          |
|                |       WHERE  `test_id` > '1') P_Count    |
|-----------------------------------------------------------|

分页
|-------------------------------------------------------|
|  finalSql      |  SELECT  *                           |
|                |                                      |
|                |  FROM `test_book`                    |
|                |                                      |
|                |       WHERE  `test_id` > '1'         |
|                |                                      |
|                |   LIMIT 10 , 10                      |
|-------------------------------------------------------|

动态查询,拼接OQL

下面是从实际项目中拿出来的一片,请参考

    @Resource
    lateinit var repo: HttpLogRepository

    //动态生成查询列表
   fun getDynamicUIData(
            pageSize: Int, pageCurrent: Int,orders: String?,request: HttpServletRequest
    ): PageVO<HttpLogs> {

        val model = HttpLogs()
        val q = OQL
                .From(model)
                .Limit(pageSize, pageCurrent, true)
                .Select() //select *
                .Where {
                    it ->
                    var cmp: OQLCompare = it.Comparer(model.id, ">", "1")
                    request.parameterNames.asSequence().forEach {
                        val param = request.getParameter(it)

                        //根据传入的查询字段,组合OQL查询语句
                        when (it) {
                            "source"     -> cmp = cmp AND cmp.Comparer(model.source, "=", param)
                            "ip"         -> cmp = cmp AND cmp.Comparer(model.ip, "=", param)
                            "httpMethod" -> cmp = cmp AND cmp.Comparer(model.httpMethod, "=", param)
                            "result"     -> cmp = cmp AND cmp.Comparer(model.result, "=", param)
                        }
                    }
                    cmp
                }
                 //orders="httpMethod desc,result asc"
                .OrderBy(orders) //本方法内部转换涉及的字段为参数化查询,不会有注入问题
                .END

        //查询
        val list = repo.select(q)

        //生成分页实体
        val pageInfo = PageInfo(list, model.takePageCountAll(), pageSize, pageCurrent)

        //转换为前端页面需要的PageVO
        val p = PageVO<HttpLogs>()
        p.list = list
        p.firstPage = pageInfo.isFirstPage
        p.lastPage = pageInfo.isLastPage
        p.pageNumber = pageInfo.pageNum
        p.totalPage = pageInfo.totalPages
        p.pageSize = pageInfo.pageSize
        p.totalRow = pageInfo.totalRows
        return p

    }

results matching ""

    No results matching ""