Solve the splicing problem of deleting conditions in myBatis

Solve the splicing problem of deleting conditions in myBatis

I just learned mybatis today and did some simple additions, deletions, modifications and queries on the database. When performing a deletion operation, it is very simple to delete a single record, but it is a bit troublesome to splice the deletion conditions when deleting in batches. The following is a record of the method.

In SQL statements, when the deletion condition is not unique, we have two types of deletion SQL statements. One is to use or to concatenate the conditions in where, such as delete from table name where condition 1 or condition 2, and the other is to use in, such as delete from table name where element in ()

Use the first deletion statement to splice in mapping.xml in mybatis:

Use the second deletion statement to splice in mapping.xml in mybatis:

The above deletion operation is basically completed. However, there is a defect. When there is no element in the query condition idList, the records in the entire table ac01 will be deleted. Therefore, we need to improve the above method.

The second delete statement is improved:

After the first delete statement is improved:

At this point, the deletion operation is completed perfectly.

Additional knowledge: Pay attention to the and splicing conditions when querying myBatis

<!-- Add a condition-->

<sql id="queryjiachaAllPageId">
 <where>
  <if test="danweimc !=null and danweimc !=''">
  and jcb.danweimc like '%${danweimc}%'
  </if>
  <if test="ge_jianchaks !=null and ge_jianchaks !=''">
  and DATE_FORMAT(jcb.jianchaks, '%Y-%m-%d') >= #{ge_jianchaks}
  </if>
  <if test="le_jianchaks != null and le_jianchaks != ''">
  and DATE_FORMAT(jcb.jianchaks, '%Y-%m-%d') &lt;= #{le_jianchaks}
  </if>
  
  <if test="jiancharxm !=null and jiancharxm !=''">
  and jcb.jiancharxm like '%${jiancharxm}%'
  </if>
  <if test="zhuangtai !=null and zhuangtai !=''">
  and yhb.zhuangtai = ${zhuangtai}
  </if>
  <if test="danweilb !=null and danweilb !=''">
  and yhb.yinhuanmc like '${danweilb}%'
  </if>
  <if test="jiedao != null and jiedao !='' and jiedao !='110105000000'">
  and jcb.jiedao=#{jiedao}
  </if>
  <if test="danweimcs != null and danweimcs !=''">
  and jcb.danweimc = #{danweimcs}
  </if>
 </where>
 </sql>

For example, the appeal questions either have "and" or "no" added. Please note that if there is an "and" or "no" query, an error will be reported.

The above article on solving the splicing problem of deleting conditions in myBatis is all the content that the editor shares with you. I hope it can give you a reference, and I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Mybatis-plus uses Condition to concatenate SQL statements
  • MyBatis dynamically concatenates SQL strings
  • MybatisPlus implements reverse concatenation of strings

<<:  Vue implements websocket customer service chat function

>>:  HTML form_PowerNode Java Academy

Recommend

Detailed tutorial on installing mysql-8.0.20 under Linux

** Install mysql-8.0.20 under Linux ** Environmen...

Record the whole process of MySQL master-slave configuration based on Linux

mysql master-slave configuration 1. Preparation H...

Do designers need to learn to code?

Often, after a web design is completed, the desig...

How to implement real-time polygon refraction with threejs

Table of contents Preface Step 1: Setup and front...

4 Ways to Quickly Teach Yourself Linux Commands

If you want to become a Linux master, then master...

How to install MySQL server community version MySQL 5.7.22 winx64 in win10

Download: http://dev.mysql.com/downloads/mysql/ U...

CSS3 achieves conic-gradient effect

grammar: background-image: conic-gradient(from an...

The scroll bar position is retained when scrolling the vant list component

The scroll bar position is retained when scrollin...