首頁 > Java > java教程 > 主體

sql映射檔的實例教程

零下一度
發布: 2017-07-17 15:15:58
原創
1620 人瀏覽過

Sql映射檔案

MyBatis 真正的力量是在映射語句中。和對等功能的jdbc來比價,映射文件節省很多的代碼量。 MyBatis的建構就是聚焦在sql的。

sql映射檔有以下幾個頂層元素:(依序)

cache配置給定命名空間的快取。

cache-ref從其他命名空間引用快取配置。

resultMap最複雜,也是最有力量的元素,用來描述如何從資料庫結果集中來載入你的物件。

parameterMap已經被廢棄了!老式風格的參數映射。內聯參數是首選,這個元素可能在將來被移除。

sql可以重複使用的SQL區塊,也可以被其他語句引用。

insert映射插入語句。

update映射更新語句。

delete映射刪除語句。

select映射查詢語句。

MyBatis的建構就是聚焦在SQL的,使其遠離於普通的方式。

SQL映射檔案有很少的幾個頂層元素(按照它們應該被定義的順序): 

>mapper:映射檔案的根元素節點,只有一個屬性namespace命名空間,用於區分不同的mapper,全域唯一,namespace綁定的DAO介面全名稱,即面向介面程式設計。這裡的mapper就相當於介面的實作類別。 

  cache - 設定給定命名空間的快取。

cache-ref – 從其他命名空間引用快取配置。

resultMap – 最複雜,也是最有力量的元素,用來描述如何從資料庫結果集中來載入你的物件。

parameterMap – 已經被廢棄了!老式風格的參數映射。內聯參數是首選,這個元素可能在將來被移除。這裡不會記錄。

sql – 可以重複使用的SQL區塊,也可以被其他語句引用。

insert – 映射插入語句

update – 映射更新語句

delete – 映射刪除語句

select – 映射查詢語句

一:使用select完成但條件查詢

      使用工具idea和mysql資料庫

     建立實體類別

#
public class student {private int stuId;private String  stuName;private grade getGrade;private  int stuAge;public grade getGetGrade() {return getGrade;
    }public void setGetGrade(grade getGrade) {this.getGrade = getGrade;
    }public int getStuAge() {return stuAge;
    }   public student(int id,String name){

   }   public student(){}public void setStuAge(int stuAge) {this.stuAge = stuAge;
    }public int getStuId() {return stuId;
    }public void setStuId(int stuId) {this.stuId = stuId;
    }public String getStuName() {return stuName;
    }public void setStuName(String stuName) {this.stuName = stuName;
    }
}
登入後複製

使用select完成條件查詢

    一:先設定mapper使用resultType

<!--模糊查询   使用resultType返回结果集-->    
    <select id="getAllStudentByLike" parameterType="String" resultType="stu">* from student where stuName like CONCAT('%',#{stuName},'%'</select>
登入後複製

 

測試類別

#
 public  void Test() throws IOException {

        studentDao dao = MyBatis.getSessionTwo().getMapper(studentDao.class);
        List<student> list = dao.getAllStudentByLike("z");for (student item:list) {
            System.out.println("----------"+item.getStuName());
        }
}
登入後複製

#另外parameterType支援的複雜型別除了javaBean之外,還包括Map類型

即修改Mapper

 <!--模糊查询-->
    <select id="getAllStudentByLike" parameterType="Map" resultType="stu">select * from student where stuName like CONCAT('%',#{stuName},'%')</select>
登入後複製

 

然後再測試類別裡建立一個HashMap集合直接作為方法參數即可

studentDao dao = MyBatis.getSessionTwo().getMapper(studentDao.class);
        Map<String,String> userMap = new HashMap<String, String>();
        userMap.put("stuName","z");
        List<student> list = dao.getAllStudentByLike(userMap);for (student item:list) {
            System.out.println("----------"+item.getStuName());
        }
登入後複製

不過map集合的key值必須和類別中的欄位名稱相同。

二:使用resultMap完成兩表查詢

       例如學生表裡關聯班級表的主鍵id,如果使用resultType只能展示其id但在實際中往往關注的是班級名稱,所有需要使用resultMap映射自訂結果。

<resultMap id="studentMap" type="entity.student">
<id property="stuId" column="stuId"></id>
        <result property="stuName" column="stuName"></result>
        <result property="gradeName" column="gradeName">
       
    </resultMap>

//sql语句
登入後複製
select * from student,grade
登入後複製

resultType直接表示傳回類型,包含基礎型別和複雜資料型別

resultMap則是對外部resultMap的引用,對應resultMap的id 表示回傳結果對應到哪一個resultMap。 :他的應用場景是:資料庫欄位資訊與物件屬性不一致或需要做複雜的聯合查詢以便自由控制映射結果 。

 

另外在 MyBatis的select元素中,resultType和resultMap本質上是一樣的,都是Map資料結構。但是 二者不能同時 存在。

三:使用resultMap的自動對映等級

     MyBatis中分為三個對應等級

     >NONE:禁止自動符合

     >PARTIAL;NONE:禁止自動符合

     >PARTIAL :(預設):自動符合所有屬性有內部巢狀(association,collection)的除外

     >FULL:自動符合所有

  在大設定中設定autoMappingBehavior

 <settings>   <!--设置resultMap的自动映射级别为Full(自动匹配所有)--><setting name="autoMappingBehavior" value="FULL" />   <!--FULL要大写··--> </settings>
登入後複製

 

設定autoMappingBehavior的值為FULL時就不需要設定resultMap下的節點,他會根據資料庫自動比對

四:使用update完成修改

     

 <update id="update">update student set stuName=#{0} where stuId=#{1}</update>
登入後複製

 

這裡使用佔位符比較簡單的一種作為參數,在測試類別中就直接填參就行了

 

五:使用映射複雜類型的屬性association

     前面的result只能對應到javaBean的某個「簡單類型」屬性,基礎資料類型和包裝類別等/

但要映射複雜類型的屬性時需要用到assocoation    複雜類別xing:即一個javaBean裡有另一個javaBean,但是association只處理一對一的關聯關係

        stuAge;

   。。。。。省略封装
登入後複製
 <resultMap id="studentMap" type="entity.student">
       <!-- <id property="stuId" column="stuId"></id>
        <result property="stuName" column="stuName"></result>-->
        <!--关联另一个 属性-->
        <association property="getGrade" javaType="grade">
       <!-- <id property="gradeId" javaType="Integer" column="gradeId"></id>
       <result property="gradeName" javaType="String" column="gradeName"></result>-->
        </association>
    </resultMap>
    <!--这里使用了自动匹配-->
登入後複製
###
<select id="getAllStudent" resultMap="studentMap"> SELECT * FROM student,grade WHERE student.stuGrade=grade.gradeId</select>
登入後複製
###

测试类里直接调用即可

六:前面说到association仅处理一对一的管理关系

    如果要处理一对多的关系,则需要使用collection,它与 association元素差不多,但它映射的属性是一个集合列表,即javaBean内部嵌套一个复杂数据类型属性。

javaBean

  private int gradeId;private String gradeName;private List<student> gatStudent;
登入後複製
 <resultMap id="gradeMap" type="grade">
        <!--<id property="gradeId" column="gradeId"></id>
        <result property="gradeName" column="gradeName"></result>-->
        <collection property="gatStudent" ofType="stu">
             <!-- <id property="stuId" column="stuId"></id>
              <result property="stuName" column="stuName"></result>-->
        </collection>
    </resultMap>
登入後複製
 <!--查询对应年级的student-->
    <select id="getAll" resultMap="gradeMap">   select * from student,grade where stuGrade = gradeId and gradeId=1
    </select>
登入後複製

以上是sql映射檔的實例教程的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!