后端-框架-MyBatis-动态SQL-choose

后端-框架-MyBatis-动态SQL-choose

public interface UserMapper{
	public List<User> getUserListByChoose(@Param("userName")String userName,
											@Param("userRole")Integer roleId,
											@Param("gender")Integer gender,
											@Param("creationDate")Date creationDate);
}

choose本质上等于switch

<select id="getUserListByChoose" resultMap="getUserListByChoose">
	 	select * from smbms_user as user where 1 = 1
	 		<choose>
	 			<when test="userName!=null and userName!=''">
	 				and user.userName like Concat('%',#{userName},'%')
	 			</when>
	 			<when test="userRole!=null">
	 				and user.userRole = #{userRole}
	 			</when>
	 			<when test="gender!=null">
	 				and user.gender = #{gender}
	 			</when>
	 			<otherwise>
	 				and YEAR(user.creationDate) = YEAR(#{creationDate})
	 			</otherwise>
	 		</choose>
	 </select>

猜你喜欢

转载自blog.csdn.net/qq_40925226/article/details/83418760
今日推荐