mybatis中的动态SQL
一、使用动态SQL完成多条件查询的元素:
1.if:利用if实现简单的条件选择;
2.choose(when,otherwise):相当于java中的switch语句,通常与when和otherwise搭配;
3.where:简化SQL语句中where的条件判断;
4.set:解决动态更新语句;
5.trim:可以灵活的去除多余的关键字;
6.foreach:迭代一个集合,通常用于in条件
二、动态SQL的元素实例:
1.if:处理条件
<select id="getUserList" resultMap="UserRoleResult">
select u.*,r.roleName from smbms_user u,smbms_role r where u.userid = r.id
<if test="id!=null">
and u.userid = #{id}
</if>
<if test="username!=null and username!=''">
and u.username like CONCAT('%',#{username},'%')
</if>
</select>
2.where:处理where和and
<select id="getUserList" resultMap="UserRoleResult">
select * from smbms_user
<where>
<if test="id!=null">
and u.userid = #{id}
</if>
<if test="username!=null and username!=''">
and u.username like CONCAT('%',#{username},'%')
</if>
</where>
</select>
<
3.trim
select id="getUserList" resultMap="UserRoleResult">
select * from smbms_user
<trim prefix="where" prefixOverrides="and | or">
<if test="id!=null">
and userid = #{id}
</if>
<if test="username!=null and username!=''">
and username like CONCAT('%',#{username},'%')
</if>
</trim>
</select>
//1.prefix属性:前缀,作用通过自动识别是否有返回值,在trim包含的内容上加上前缀,如案例的where;
//2.suffix属性:后缀,与前缀相同;
//3.prefixOverrides属性:对于trim包含内容的首部进行指定内容的忽略 4.suffixOverrides属性:对于trim包含内容的首部进行指定内容的忽略
4.使用if+set进行数据的更新操作
<update id="modify" parameterType="cn.smbms.user.dao.addUserMapper">
update smbms_user
<set>
<if test="user_name !=null">user_name=#{user_name},</if>
<if test="userpassword!=null">userpassword=#{userpassword},</if>
<if test="useremail!=null"> useremail=#{useremail},</if>
<if test="username!=null">username=#{username},</if>
<if test="sex!=null">sex=#{sex},</if>
<if test="brithday!=null">brithday=#{brithday},</if>
<if test="usercode!=null">usercode=#{usercode}</if>
</set>
where userid=#{userid}
</update>
5.choose(when、otherwise)
<select id="getUserList_choose" resultType="cn.smbms.pojo.user.User">
select * from smbms_user where 1=1
<choose>
<when test="username!=null and username!=''">
and username like CONCAT('%',#{username},'%')
</when>
<when test="userid!=null">
and userid =#{userid}
</when>
<when test="usercode!=null and usercode!=''">
and usercode like CONCAT('%',#{usercode},'%')
</when>
<otherwise>
and YEAR(brithday) = YEAR(#{brithday})
</otherwise>
</choose>
</select>
Choose一般与when和otherwise一起使用
When:子元素属性有test,满足when的条件则会输出when中的内容;
Otherwise:当所有的条件都不满足时就会调至otherwise条件中执行判断;
三、foreach的一些子元素的解释:
1.item:表示集合中每个元素进行迭代时的别名;
2.index:指定一个名称,用于表示在迭代过程中,每次迭代到的位置;
3.open:表示该语句什么时候开始(既然是in语句,就是从‘(’开始);
4.separator:表示每次迭代时以什么符号作为分隔符;
5.close:表示该语句什么时候结束,在in语句中就是以‘)’结束;
6.collection:最关键比切最容易出错的属性,不同情况下,该属性的value也是不同,主要有以下三种:
第一:若入参为单参数切参数类型是一个list的时候,collection属性值为list;
第二:若入参为单参数且为数据类型是一个数组的时候,collection属性的值为array;
第三:若入参为多参数,就需要把他们封装为一个Map进行处理;
四、foreach的代码示例如下:
<select id="getUserByUserid_foreach_array" resultMap="userMapByUserRole">
select * from smbms_user where userid in
<foreach collection="array" item="userid" open="(" close=")" separator=",">
#{userid}
</foreach>
</select>
五、mybatis的入参方式不同进行foreach:
注意:此部分代码没有传上去,具体的可以联系我,我发于想了解的人
1.入参为数组:
PublicList<User>getUserByUserid_foreach_array(Integer[] userid);
2.入参为List
publicList<User>getUserByUserid_foreach_list(List<Integer> userid);
3.入参为Map
/**
* 查找指定性别和用户角色的所有信息
* @param conditionMap
* @return
*/
publicList<User>getUserByConditionMap_foreach_map(Map<String, Object> conditionMap);
六、mybatis的分页功能:
Mysql的分页功能是基于内存的分页,即查出来所有记录,再按照起始位置和页面容量取出结果;