select
1.String 参数 作为if 条件
mybatis mapper.xml sql 当parameterType为String时 任何参数都必须为_parameter(比如原先为参数为key),若果没有_parameter,提示错误There is no getter of 'username' in java.lang.string
int pagingCount(String key);
<select id="pagingCount" resultType="int" parameterType="string"> SELECT count(1) FROM users WHERE 1=1 <if test="key != null and '' != key"> name = #{key} or username = #{key} </if> </select>
<select id="pagingCount" resultType="int" parameterType="string"> SELECT count(1) FROM users WHERE 1=1 <if test="_parameter != null and '' != _parameter"> and name = #{_parameter} or username = #{_parameter} </if> </select>
2. list 作为必须条件,otherwise 直接返回false ,结果集返回null
List<Role> queryRolesByOrgIds(@Param("ids") Set<Integer> ids);
<select id="queryRolesByOrgIds" resultMap="roleMap" parameterType="list" > select r.id,r.name,r.code,r.app_code,r.parent_id from ts_role r join tm_org_role ro on r.id = ro.role_id where <choose> <when test="ids!=null and ids.size>0"> ro.org_id in <foreach collection="ids" item="item" open="(" separator="," close=")"> #{item} </foreach> </when> <otherwise> false </otherwise> </choose> </select>