Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem of Update dynamic SQL comma by MyBatis

Shulou Source: shulou.com Published: 2022-06-01 23:59:42 09月27日 Update

This article mainly introduces "how MyBatis solves the problem of Update dynamic SQL comma". In daily operation, I believe many people have doubts about how to solve the problem of Update dynamic SQL comma in MyBatis. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the question of "how to solve the problem of Update dynamic SQL comma by MyBatis". Next, please follow the editor to study!

Update dynamic SQL comma problem

The most important project encountered the following situation: dynamic splicing of Update is needed in MyBatis. Since I was busy catching up with the project, I wrote it as follows, only to find that there was an exception in the system. If I wrote in this way, id=null would make an error.

UPDATE TABLE SET id= # {id,jdbcType=INTEGER}, name = # {name,jdbcType=VARCHAR} where id= # {id,jdbcType=INTEGER}

So I looked up the API and official documents of Mybatis online and found the following

Solution UPDATE TABLE id= # {id,jdbcType=INTEGER}, name = # {name,jdbcType=VARCHAR}, where id= # {id,jdbcType=INTEGER}

Node label:

The main function of trim is that you can add some prefixes (prefix) to the content contained in Trim or some suffixes (suffix) after the content contained in Trim.

You can also ignore some content in the first part of the content contained in Trim (prefixOverrides), or ignore some content in the end of the content contained in Trim (suffixOverrides).

This line of code means: add set in front of it and remove the last comma!

Note method 2: put the contents of the update condition tag in the tag

Mapper (Update) comma position update t_role name=# {name}, msg=# {msg}, type=# {type}, creator_id=# {creator_id}, level=# {level} where id=# {id}

Use can intelligently remove the last comma, very convenient!

At this point, the study on "how MyBatis solves the problem of Update dynamic SQL commas" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Comma content dynamic question Learning tag method more Project help practical next Code location prefix method function suffix comment official Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Xiaomi NVidia vpn Redmi