Mybatis <if> tag bool Indicates that the value is false

Some of baidu’s articles hate, just CTRL + C and CTRL + V. And it doesn’t solve the problem.

Yesterday implemented a feature to find articles by their ID or alias.

At first, mybatis Example is used to query, and the parameter artName is judged. If it is a pure number, byId is used to query, otherwise by alias is used. Since the query of articles needs to be associated with the query of article classification labels, the select statement mapping method is chosen for the query, but without writing two query methods, the dynamic SQL in Mybatis is used.

/** **@paramArtName ID or alias *@paramById byId if true * otherwise by alias *@return* /
public Article selectByArtName(@Param(value = "artName") String artName,
                          @Param(value = "byId") Boolean byId);
Copy the code

Mapper defines a method with two parameters artName and byId. ArtName indicates an id or an alias, and byId indicates whether to query byId.

ById Boolean (); false; byId Boolean ()

<if test="byId ! = null">
    a.id = #{artName}
</if>
Copy the code

This mode works when byId=true, but does not work when byId is false. Then use the <choose> tag

<choose>
	<when test="byId">
		a.id = #{artName}
	</when>
	<otherwise>
		a.alias = #{artName}
	</otherwise>
</choose>
Copy the code

ById itself is a bool, so you don’t need to judge.

Note: byId should not be added with #{}, otherwise false will not be valid. If #{} is added, when comparing with other values,

#{byId} == ‘true’; #{byId} == ‘true’; Linkhashmap cannot be converted to String.