MyBatis framework of SSM [SSM]: Dynamic SQL

One of a powerful feature of MyBatis has always been its Dynamic SQL capabilities. Dynamic SQL elements and use JSTL or other similar similar XML-based text processor. (Note: The following are examples of other configurations and Part consistent)

1、if:

Sometimes the data query conditions, where the need to use to define a plurality of conditions, but some conditions may accept incoming values ​​therein when calling the sql command, at this time if you need to use the label used to dynamically splice ,example 1:

<select id="selByPriPro" resultType="flower">
	select * from flower where 1 = 1 
	<if test="price != null and price != ''">
		and price = #{price}
	</if>
	<if test="production != null and production != ''">
		and production = #{production}
	</if>
</select>

The results at this time not to pass parameters involved in mass production as follows, if the label is evident advantages:
Here Insert Picture Description
Here Insert Picture Description

2、choose, when, otherwise:

Sometimes we do not want all of the conditions, on the contrary we want to choose only one case among many options. And Java in the switch statement is similar, MyBatis offers a choose, when, otherwise a set of tags to meet the requirement. Example 2:

<select id="selByPriPro" resultType="flower">
	select * from flower where  1 = 1
	<choose>
		<when test="production != null and production != ''">
			and production = #{production}
		</when>
		<otherwise>
			and price = #{price}
		</otherwise>
	</choose>
</select>

Two cases are as follows:
Here Insert Picture Description
Here Insert Picture Description

3、where:

In Example 1 sql statement with the "where 1 = 1" to avoid syntax errors, but this is no reason to compare the size of 1 and 1 also consume computer resources, for the program, a computer is a valuable resource, so there is no way to avoid this problem, the answer is yes, you can use where the label. Example 3:

<select id="selByPriPro" resultType="flower">
	select * from flower
	<where>
		<if test="price != null and price != ''">
			and price = #{price}
		</if>
		<if test="production != null and production != ''">
			and production = #{production}
		</if>
	</where>
</select>

At this time, to the production and transmission price participation result does not pass the following parameters:
Here Insert Picture Description
Here Insert Picture Description

4、trim:

trim tab is used to: if there if a child tag to meet the conditions within the trim tab and then stitching the fragments if sql statement when it is added in front of the specified content, or delete the beginning of the sql fragment of specified content. 3 embodiment may be modified, for example, Example 4:

<select id="selByPriPro" resultType="flower">
	select * from flower
	<trim prefix="where" prefixOverrides="and">
		<if test="price != null and price != ''">
			and price = #{price}
		</if>
		<if test="production != null and production != ''">
			and production = #{production}
		</if>
	</trim>
</select>

At this time, to the production and the price is not involved in mass transfer parameters were as follows:
Here Insert Picture Description
Here Insert Picture Description
Note: TRIM tag attribute describes

  • prefix: Adds the specified content at the beginning of the first fragment of a sql returned;
  • prefixOverrides: Before adding a prefix to specify content for sql fragment of a return, first remove the sql opening segment specified content;
  • suffix: Adds the specified content sql returned last segment end;
  • suffixOverrides: Before adding a prefix to specify content for sql last segment of a return, first delete the sql fragment end of the specified content;

5、bind:

bind a single label, which is used to make further modifications to the accepted parameters to meet the needs of sql statement, when used in the fuzzy query parameter stitching "%" acceptable. Example 5:

<select id="selByPriPro" resultType="flower">
	<bind name="pattern" value="'%' + production + '%'"/>
	select * from flower where production like #{pattern}
</select>

The "Asia" as a parameter passed to the production results are as follows:
Here Insert Picture Description

6、foreach:

foreach tag is used to iterate the contents of the collection, each traversal of traversal time to modify the data spliced ​​into the sql statement, commonly used in the query. Example 6:

<select id="selByIn" resultType="flower">
	select * from flower where name in 
	<foreach collection="names" open="(" close=")" separator="," item="name" index="index"> 
		#{name}
	</foreach>
</select>

Included with "petunia", "zinnia", "Sakura" List of collective test results are as follows:
Here Insert Picture Description
Note: foreach tag attribute description

  • collection: receiving a set of parameters;
  • open: traversing specified before the start of the first stitching content;
  • close: traversing the end of the specified content on after stitching;
  • separator: delimiter designated content of each pass splicing;
  • item: variable data assigned to each pass specified;
  • index: recording the number of times traversed, starting from 0.

7、set:

Different from the other tags, the tag is designed to modify the SET data provided tag, the tag may be used SET column contains dynamically updated, without updating does not contain. Example 7:

<update id="upd" parameterType="map">
	update flower 
	<set>
		<trim suffixOverrides=",">
			<if test="name != null">name = #{name}, </if>
			<if test="price != null">price = #{price}, </if>
			<if test="production != null">production = #{production},</if>
		</trim>
	</set>
	where id = #{id}
</update>

Test No. 4 spent modify the information, the results are as follows:
Here Insert Picture Description

Published 128 original articles · won praise 17 · views 2736

Guess you like

Origin blog.csdn.net/qq_43705275/article/details/104066258