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

MySQL index coverage example analysis

This article describes MySQL index coverage with ...

Detailed code for implementing 3D tag cloud in Vue

Preview: Code: Page Sections: <template> &l...

How to determine if the Linux system is installed on VMware

How to determine whether the current Linux system...

IDEA configuration process of Docker

IDEA is the most commonly used development tool f...

js implements random roll call

This article shares the specific code of js to im...

Some notes on installing fastdfs image in docker

1. Prepare the Docker environment 2. Search for f...

Use of VNode in Vue.js

What is VNode There is a VNode class in vue.js, w...

Text mode in IE! Introduction to the role of DOCTYPE

After solving the form auto-fill problem discussed...

JavaScript programming through Matlab centroid algorithm positioning learning

Table of contents Matlab Centroid Algorithm As a ...

Correct use of MySQL partition tables

Overview of MySQL Partitioned Tables We often enc...

Detailed explanation of JavaScript upload file limit parameter case

Project scenario: 1. Upload file restrictions Fun...

MySQL 5.7.20 compressed version download and installation simple tutorial

1. Download address: http://dev.mysql.com/downloa...

Design reference WordPress website building success case

Each of these 16 sites is worth reading carefully,...

Installation and configuration tutorial of MySQL 8.0.16 under Win10

1. Unzip MySQL 8.0.16 The dada folder and my.ini ...