您的位置:首页 > 博客中心 > 数据库 >

Mybatis SQL语句查询

时间:2022-03-14 02:02

MyBatis中使用in查询时的注意事项 foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。

1. 当查询的参数只有一个时 
  findByIds(List<Long> ids)
 1.a 如果参数的类型是List, 则在使用时,collection属性要必须指定为 list
foreach元素的属性主要有 item,index,collection,open,separator,close。

item表示集合中每一个元素进行迭代时的别名.

index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置.

open表示该语句以什么开始,

separator表示在每次进行迭代之间以什么符号作为分隔 符.

close表示以什么结束.

在使用foreach的时候最关键的也是最容易出错的是collection属性,该属性是必须指定的,但是在不同的情况下,该属性的值是不一样的,主要分为三种情况

 

1:如果传入的是单参数且参数类型

 <select id="findByIdsMap" resultMap="BaseResultMap">
         Select
         <include refid="Base_Column_List" />
         from jria where ID in
                  <foreach item="item" index="index" collection="list" 
                         open="(" separator="," close=")">
                        #{item}
                </foreach>
  </select> 
 
 findByIds(Long[] ids)
 1.b 如果参数的类型是Array,则在使用时,collection属性要必须指定为 array
 
  <select id="findByIdsMap" resultMap="BaseResultMap">
                 select
                 <include refid="Base_Column_List" />
          from jria where ID in
                  <foreach item="item" index="index" collection="array" 
                         open="(" separator="," close=")">
                        #{item}
                </foreach>
  </select> 
 
2. 当查询的参数有多个时,例如 findByIds(String name, Long[] ids)
 这种情况需要特别注意,在传参数时,一定要改用Map方式, 这样在collection属性可以指定名称
         下面是一个示例
 
         Map<String, Object> params = new HashMap<String, Object>(2);
        params.put("name", name);
         params.put("ids", ids);
        mapper.findByIdsMap(params);
 
 <select id="findByIdsMap" resultMap="BaseResultMap">
                 select
                 <include refid="Base_Column_List" />
          from jria where ID in
                  <foreach item="item" index="index" collection="ids" 
                         open="(" separator="," close=")">
                        #{item}
                </foreach>
   </select> 
 
 
完整的示例如下:
例如有一个查询功能,Mapper接口文件定义如下方法:
List<Jria> findByIds(Long... ids);
使用 in 查询的sql拼装方法如下:
 
 <select id="findbyIds" resultMap="BaseResultMap">
                 select
                 <include refid="Base_Column_List" />
          from jria where ID in
                  <foreach item="item" index="index" collection="array" 
                         open="(" separator="," close=")">
                        #{item}
                </foreach>
  </select> 
    MyBatis动态多条件查询语句   <select id="searchpeople" parameterClass="people" resultClass="people"> select peopleid,peoplename,password,dept,peopletype,telephone,email,mphone,fax,description,allflag,vipid,vipname from people   <dynamic prepend="WHERE">                <isNotEmpty prepend="AND" property="peopleid">        (peopleid like #peopleid#)        </isNotEmpty>                <isNotEmpty prepend="AND" property="peoplename">        (peoplename like #peoplename#)        </isNotEmpty>              <isNotEmpty prepend="AND" property="dept">        (dept like #dept#)        </isNotEmpty>                <isNotEmpty prepend="AND" property="peopletype">        (peopletype like #peopletype#)        </isNotEmpty>                </dynamic>                 </select> <dynamic>内则是动态条件所相关的语句,里面填写的都是where相关的条件 对于这个动态的依赖于什么动词侧为 prepend="" 其中可以填写where也可以写group by和order by 对于要动态的判定条件是否存在则用以下标签: <isNotEmpty>意思则为当次条件不为空时执行其中语句 prepend="" 依赖约束, 值可以是 AND 也可以是OR property="" 就是对于这个条件所判定的取值字段 例如"dept"  这样上述的select语句就可以实现一个判定<=4个条件的sql实现语句,在应用中,在页面此处就可以设计4个输入框,用户只输入其中条件,动态实现查询 顺便提一句,mybaits一般导入的都是类的参数,所以要实现模糊查询,只需要在类->属性字段值的2边+上%就可以以 %字段值% 的形式传入实现模糊查询. 对于动态语句中的判定条件除了上面的<isNotEmpty>不为空,还有下面一些常用到的: <isGreaterThan prepend="and" property="" compareValue=""> 字段大于某个值 compareValue 比较值 <isGreateEqual> 大于等于比较 同上用法 <isEqual> 是否相等 同上用法 <isNotEqual> 是否不相等 同上用法 <isLessThan> 小于比较 同上用法 <isLessEqual> 小于等于比较 同上用法 总结一下mybatis select基本格式用法:                                 <select 导入导出参数>                    SQL查询语句              <dynamic prepend="依赖条件">                            <isNotEmpty prepend="下个条件的依赖值,and,or" property="判定字段">                                                          //判定条件语句                            (查询条件语句)                            </isNotEmpty>                            //另外一个判定条件语句                     </dynamic>               </select>           在MyBatis中当sql语句只需要一个String类型参数时,我们在sql语句中必须将参数改为_parameter才可以执行,不然会报错 There is no getter for property named ‘moduleCode‘ in ‘class java.lang.String  这个错误!   例如:  <select id="selectFtpConfigName" resultType="com.jzl.entity.platFormEntity.FtpConfig">         select * from ftpconfig where         <if test="_parameter != null and _parameter !=‘‘ ">             name like CONCAT(‘%‘,#{_parameter}, ‘%‘)         </if>       </select>

 

 

热门排行

今日推荐

热门手游