Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize conditional insertion by mybatis dynamic splicing

Shulou Source: shulou.com Published: 2022-06-01 11:32:39 11月16日 Update

In this article Xiaobian introduces in detail "mybatis dynamic stitching how to achieve conditional insertion", detailed content, clear steps, details handled properly, hope that this article "mybatis dynamic stitching how to achieve conditional insertion" article can help you solve doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge bar.

Conditional insertion of dynamic stitching implementation has recently encountered a very real problem when working on a project.

That is, according to different scenarios, and then achieve conditional insertion, to put it bluntly, it is to insert data when the column has data, and automatically ignore the column when there is no data.

In fact, these are involved in mybatis's api, but I haven't seen it well before, and I didn't take a serious look at this piece of content until I used it recently.

Insert into base_department_staff status, group_id, staff_code, realname, create_time, phone, is_temporary email, user_id Sex, # {param.status}, # {param.groupId}, # {param.staffCode}, # {param.realname}, # {param.phone}, # {param.temporary} # {param.email} # {param.userId}, # {param.sex}

I believe that there is no need to say much about the trim in this, and it is often used to go to and from spaces. There are three attributes in this trim tag, prefix this indicates that you want to concatenate the prefix of sql, suffix this indicates the suffix of dynamic sql, and suffixOverrides this will help me remove the last extra comma.

Mybatis insert statement mybatis insert statement is usually written like this

The value range of useGeneratedKeys is true | the default value of false is false.

Meaning: sets whether to use the getGenereatedKeys method of JDBC to get the primary key and assign it to the domain model property of the keyProperty setting.

Always thought that useGeneratedKeys= "true" is to add the primary key to the mysql database, but in fact, after inserting the statement, the id is assigned to the entity bean, even after using insert, you can get the value using bean.getId (), if false,bean.getId () = null.

Whether true or false, the primary key is automatically generated by the data inserted by the database (provided that the primary key is automatically grown).

Read here, this "mybatis dynamic splicing how to achieve conditional insertion" article has been introduced, want to master the knowledge of this article also need to practice and use in order to understand, if you want to know more related articles, welcome to pay attention to the industry information channel.

Tags: Data dynamic condition content article time statement that is attribute database help different good-looking proper three no premise prefix suffix meaning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Docker Microsoft Shulou Information NVidia