mysql选择,插入和删除java程序的工作,但更新无法正常工作

时间:2017-02-16 08:38:25

标签: java mysql sql-update mariadb jooq

我有一个主键id的表,选择,插入和删除查询所有工作都来自java程序,但更新查询不起作用,以便在重复更新时插入'(仅限当记录不存在时起作用,当记录存在时,更新不会起作用。

提交所有查询,我的mariadb版本为10.1.14。

提前感谢您的帮助!

所有查询在mysql-cli中运行良好。

表架构

+------------------+----------------------+------+-----+---------------------+-----------------------------+
| Field            | Type                 | Null | Key | Default             | Extra                       |
+------------------+----------------------+------+-----+---------------------+-----------------------------+
| id               | int(10) unsigned     | NO   | PRI | NULL                |                             |
| posng_valid_type | tinyint(3) unsigned  | YES  |     | NULL                |                             |
| longitude        | double(9,6)          | NO   |     | 0.000000            |                             |
| latitude         | double(9,6)          | NO   |     | 0.000000            |                             |
| heading          | smallint(6)          | NO   |     | 0                   |                             |
| altitude         | float(7,3)           | NO   |     | 0.000               |                             |
| gps_speed        | smallint(5) unsigned | NO   |     | 0                   |                             |
| sample_time      | timestamp            | NO   |     | 0000-00-00 00:00:00 |                             |
| update_time      | timestamp            | NO   |     | CURRENT_TIMESTAMP   | on update CURRENT_TIMESTAMP |
+------------------+----------------------+------+-----+---------------------+-----------------------------+

1。更新查询

update `status_position` 
set `status_position`.`id` = 3007,  
    `status_position`.`posng_valid_type` = 0, 
    `status_position`.`longitude` = 121.1921, 
    `status_position`.`latitude` = 31.2797, 
    `status_position`.`heading` = -1, 
    `status_position`.`altitude` = 0.0, 
    `status_position`.`gps_speed` = 0, 
    `status_position`.`sample_time` = timestamp '2017-02-15 03:52:23.0'   
where `status_position`.`id` = 3007;

2。更新重复查询

insert into `status_position` (
    `id`, 
    `posng_valid_type`, 
    `longitude`, 
    `latitude`, 
    `heading`, 
    `altitude`, 
    `gps_speed`, 
    `sample_time`
 ) values (
    2001, 
    0, 
    121.1921, 
    31.2797, 
    -1, 
    0.0, 
    0, 
    timestamp '2017-02-15 03:52:23.0'
) on duplicate key update 
    `status_position`.`id` = 2001, 
    `status_position`.`posng_valid_type` = 0, 
    `status_position`.`longitude` = 121.1921, 
    `status_position`.`latitude` = 31.2797, 
    `status_position`.`heading` = -1, 
    `status_position`.`altitude` = 0.0, 
    `status_position`.`gps_speed` = 0, 
    `status_position`.`sample_time` = timestamp '2017-02-15 03:52:23.0';

使用JOOQ生成查询2的

的java代码
public <R extends Record> void batchUpsertRecord(Table<R> table, List<R> records) throws PersistenceException {
        Connection conn = ConnectionPoolManager.INSTANCE.getConnection();

            try (DSLContext dslContext = DSL.using(conn, SQLDialect.MARIADB)) {
                List<InsertQuery> insertQueryList = new ArrayList<>();
                for (R record : records) {
                    InsertQuery<R> insert = dslContext.insertQuery(table);
                    insert.addRecord(record);
                    insert.onDuplicateKeyUpdate(true);
                    insert.addValuesForUpdate(mapOfChangedValues(record));
                    insertQueryList.add(insert);
                }
                dslContext.batch(insertQueryList).execute();
                conn.commit();
            } catch (SQLException e) {
                logger.error("Failed to upsert record into table({}).", table.getName(), e);
            } finally {
                ConnectionPoolManager.INSTANCE.closeConnection(conn, logger);
            }
    }

0 个答案:

没有答案