Mybatis查询

 select查询

          resultType:返回的是一个集合,则是集合元素的类型

          Map<String ,Object>  ====>resultType="map"

          Map<Integer,Employee>====>resultType="mybatis.bean.Employee"限定名)

         引用mapper:<mapper namespace="跟mapper的接口进行绑定"></mapper>

         自定义某个JavaBean的规则        type自定义规则的Java类型            id:唯一id方便引用

 <resultMap  type="mybatis.bean.Employee" id="MyEmp">

            <id   column="id"   property="id"/>

           <result  column="last_name"    property="LastName"/>

</resultMap>

id定义主键会有底层有优化、column指定那一列 property指定对应的javaBean属性

alter  table  tbl_employee  add    constraint   fk_emp_dept

foreign   key(d_id)   references   tbl_dept(id)    (添加外键约束)

select     *   from     tbl_employee   e,tbl_dept  d  where  e.d_id=d.id        and e.id=1

联合查询:级联属性封装  

association:可以指定联合的javabean对象

proeprty=”dept“:指定哪个属性是联合的对象

javaType=指定属性的类型

association  分步查询:1、先按照员工id查询员工信息

                                            2、根据查询员工信息中的d_id值去部门表查询出部门信息

                                            3、部门设置到员工中去

流程:使用select指定的方法传入column指定的这列参数的值、查出对象,并封装给property指定的属性

显示指定每个我们需要更改的配置的值,即使是默认的

<setting  name="lazyloadingEnabled"   value="true"/>

<setting  name="aggressivelazyLoading"  value="false"/>

延迟加载(按需加载)     

employee====》dept每次查询employee对象的时候,一起查询出来,部门信息在我们使用的时候再去查询

例:   select  d.id    d.id,    d.dept_name   dept_name,e.id   eid, e.last_name     last_name,e.email    email,e.gender   gender      

            from   tbl_dept         d              

           left    join          tbl_employee       e

           on      d.id=e.d_id

           where    d.id=1

分段查询

<!---collectionf:分段查询-->

<resultMap  type="mybatis.bean.Department"   id="myDeptStep">

                <id  column="id"   property="id"        />

                <id   column="dept_name"       property="departmentName"    />

                 <collection         property="emps"

                   select=”mybatis.dao.EmployeeMapperPlus.getEmpstep“        column="id"        fetchType="lazy"    </collection>

</resultMap>

 <select   id="getDeptByIdStep"            resultMap="myDeptStep">

               select   id,dept_name       from      tbl_dept      where     id=#{id}

</select>

discriminator        鉴别器:mybatis可以使用discriminator判断某列的值,然后根据某列的值改变封装行为。

动态SQL

查询的时候如果某些条件没带可能sql拼装会有问题

1、给where后面加上1=1,以后的条件都and  xxx

2、mybatis使用where标签来将所有的查询条件包括在内,mybatis就会将where标签中拼装的sql,多出来的and或者or去掉

IF用法:

<select       id="getEmpByConditionIf"      resultType="mybatis.bean.Employee">

        select       *         from             tbl_employee

where

         <if      test="id!=null">

                        id=#{id}

         </if>

        <if           test="lastName!=null             &amp;&amp;     lastName!=&quot;&quot;    ">

                          and      last_Name   like         #{lastName}

          </if>

          <if      test="email!=null       and     email.trim()!=&quot;&quot;     ">

                       and    email=#{email}

          </if>

           <if      test="gender==0   or  gender==1">

                       and   gender=#{gender}

            </if>

       </select>

Where用法

<select       id="getEmpsByConditionIf"            resultType="mybatis.bean.Employee">

                   select    *       from      tbl_employee

                            where   1=1

                     <if    test="id!=null">

                                and      id=#{id}

                     </if>

                      <if      test="lastName!=null    &amp;&amp;         lastName!=&quot;&quot;">

                                and        last_name      like     #{lastName}

                       </if>

                       <if   test="email!=null   and    email.trim()!=&quot;&quot;">

                                  and   email=#{email}      

                       </if>

                       <if   test="gender==0    or        gender==1">

                                       and             gender=#{gender}

                        </if>

             </select>

trim 用法       字符截取 

                 prefix=""  前缀:trim标签中是整个字符串拼串后的结果                    prefix给拼串后的整个字符串加一个前缀

                 prefixOverriders:去掉整个字符串前面多余的字符

                 suffix=”“   后缀:拼串后的整个字符串加一个后缀

                  suffixOverrides:去掉整个字符后面多余的字符

<select    id="getEmpsByconditiontrim"   resultType="mybatis.bean.Employee">

           select    *   from          tbl_employee

                   <trim         prefix="where"          suffoxOverrides="and">

                                 <if   test =”id!=null“>

                                             id=#{id}

                                  </if>

                                 <if   test="lastName!=null         &amp;&amp;                lastName!=&quot;&quot;">

                                               lastname       like  #{lastName}          and

                                 </if>

                                 <if        test="email!=null           and       email.trim()!=&quot;&quot;">

                                             email=#{email}               and

                                  </if>

                                <if           test="gender==0             or   gender==1">

                                              gender=#{gender}  

                               </if>

                          </trim>

                   <select>

choose(when,otherwise):分支选择switch-case

<select        id="getEmpsByConditionChoose"            resultType="mybatis.bean.Employee">

                 select   * from  tbl_employee

                         <where>

                                <choose>

                                            <when    test="id!=null">

                                                            id=#{id}

                                              </when>

                                             <when           test="lastName!=null">

                                                            last_name            like     #{lastname}

                                              </when>

                                            <when          test="email!=null">

                                                           email=#{email}

                                            </when>

                                            <otherwise>

                                                    gender==0

                                             </otherwise>                 

                                   </choose>

                           </where>

                       </select>

where(封装)  set

                <update     id="updateEmp">

                            update  tbl_employee

                                 set

                                    <if        test="lastname!=null">

                                                   last_name=#{lastname},

                                     </if>

                                   <if         test="email!=null">

                                                 email=#{email},

                                   </if>

                                 <if            test="gender!=null">

                                                     gender=#{gender}

                                </if>

                                 where  id=#{id}

                              </update>

collection:指定要遍历的集合(list类型的参数会特殊处理封装在map中,map的key:list)

item:将当前遍历出的元素赋值给指定的变量

separator:每个元素之间分隔符

open:遍历出所有结果拼接一个开始的字符

close:遍历出所有结果拼接一个开始的字符

index:索引(遍历list:index(索引) item当前值          index====>map的key,item==>map的值)

<select    id="getEmpsByConditionForeach"     resultType="mybatis.bean.Employee">

              select        *      from   tbl_employee   where   id    in

                   <foreach        collection="ids"    item="item_id"          separator=","            open="where   id    in   ("close") ">

                            #{item_id}

                       </foreach>

            </select>

批量保存

<select    id="getEmpsByConditionForeach"     resultType="mybatis.bean.Employee">

<insert       id="addEmps">

            insert   into    tbl_employee(last_name,email,gender,d_id)

           values

                 <foreach       collection="emps"     item="emp"       sepatator="">

                        (#{emp.lastName},#{emp.eamil},#{emp.gender},#{emp.dept.id})

                 </foreach>

</select>

 

posted @ 2019-07-10 14:42  繁春  阅读(967)  评论(0编辑  收藏  举报