更新

针对Entity:

1、默认只更新有变动的属性值,主键必须有值

    @Test
    fun updateEntity() {
        val tb = TestBook()
        tb.testId = "50"
        tb.testName = "test"
        getDB().update(tb)
    }

|--------------------------------------------------------------|
|  finalSql      |  UPDATE `test_book` SET                     |
|                |                                             |
|                |   `updated_at`='2017-05-02T21:25:53.379' ,  |
|                |                                             |
|                |   `updated_by`='zhangsanfeng' ,             |
|                |                                             |
|                |   `test_name`='test'                        |
|                |                                             |
|                |  WHERE 1=1                                  |
|                |                                             |
|                |    And `test_id`='50'                       |
|--------------------------------------------------------------|

2、更新所有属性

    @Test
    fun updateEntityAllFields() {
        val tb = TestBook()
        tb.testId = "50"
        tb.testName = "test"
        getDB().update(tb,saveChangedOnly = false)
    }


|--------------------------------------------------------------|
|  finalSql      |  UPDATE `test_book` SET                     |
|                |                                             |
|                |   `updated_at`='2017-05-02T21:24:45.200' ,  |
|                |                                             |
|                |   `updated_by`='zhangsanfeng' ,             |
|                |                                             |
|                |   `test_name`='test' ,                      |
|                |                                             |
|                |   `test_url`='null' ,                       |
|                |                                             |
|                |   `test_count`='null' ,                     |
|                |                                             |
|                |   `created_by`='null' ,                     |
|                |                                             |
|                |   `created_at`='null'                       |
|                |                                             |
|                |  WHERE 1=1                                  |
|                |                                             |
|                |    And `test_id`='50'                       |
|--------------------------------------------------------------|

针对OQL:

3、通过OQL控制更新情况,只更新testName、testURL两个字段

    @Test
    fun testUpdate() {
        var book2 = TestBook()
        book2.testName = "abcUpdateOQLWidthKeys"
        book2.testURL = "UpdateOQLWidthKeys"
        var q = OQL.From(book2).Update(book2.testName, book2.testURL)
                .Where {
                    cmp ->
                    cmp.Comparer(book2.testId, ">", "990")
                }
                .END
        getDB().update(q)
    }


|--------------------------------------------------------------|
|  finalSql      |  UPDATE `test_book` SET                     |
|                |                                             |
|                |   `test_name`='abcUpdateOQLWidthKeys' ,     |
|                |                                             |
|                |   `test_url`='UpdateOQLWidthKeys' ,         |
|                |                                             |
|                |   `updated_at`='2017-05-02T21:23:57.354' ,  |
|                |                                             |
|                |   `updated_by`='zhangsanfeng'               |
|                |                                             |
|                |       WHERE  `test_id` > '990'              |
|--------------------------------------------------------------|

results matching ""

    No results matching ""