Mybatis查詢

 select查詢java

          resultType:返回的是一個集合,則是集合元素的類型sql

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

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

         引用mapper:<mapper namespace="跟mapper的接口進行綁定"></mapper>ide

         自定義某個JavaBean的規則        type自定義規則的Java類型            id:惟一id方便引用fetch

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

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

           <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  分步查詢:一、先按照員工id查詢員工信息

                                            二、根據查詢員工信息中的d_id值去部門表查詢出部門信息

                                            三、部門設置到員工中去

流程:使用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拼裝會有問題

一、給where後面加上1=1,之後的條件都and  xxx

二、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>

相關文章
相關標籤/搜索