SQL语句中where 1=1 的用处

SQL语句中where 1=1 的用处

主要是用于动态拼接sql语句,解决有多个条件的时候where和and相接的冲突。

	<select id="selectModelPage"
		resultType="cn.com.befery.dataai.po.Model">
		SELECT
		model.`model_id`,
		model.`model_name` as modelName,
		model.`status`as status,
		t.`training_name`as trainingName,
		ap.`application_name` as applicationName,
		model.`create_date` as createDate
		FROM
		ai_model model
		LEFT JOIN ai_training t
		ON t.`training_id` = model.`training_id`
		LEFT JOIN ai_application ap
		ON ap.`application_id` = t.`application_id`
		<where>
			 1 = 1 
			<if test="model.modelName != null and model.modelName != ''">
				and model.`model_name` like '%${model.modelName}%'
			</if>
			order by ${model.order} ${model.orderdir}	
		</where>
	</select>
  • 加上 where 1=1 的话,很多条件可以很自由的拼接在下面

至于说影响性能之类的,暂时没研究。

原文地址:https://www.cnblogs.com/senup/p/12574134.html