mybatis中Mapper.xml配置詳解


前言

我們知道,每一款框架產品在實際開發中,都是通過XML文件來培訓框架的相關流程的,MyBatis也不例外,主要有兩個配置文件:config.xml和Mapper.xml,當然,這兩種配置文件可以自定義文件名。

  • config.xml是全局配置文件,主要配置MyBatis的數據源(DataSource),事務管理(TransactionManager),以及打印SQL語句,開啟二級緩存,設置實體類別名等功能。
  • Mapper.xml的作用是什么?我們之前介紹過,MyBatis是"半自動"的ORM框架,即SQL語句需要開發者自定義,MyBatis的關注點在POJO與SQL之間的映射關系。那么SQL語句在哪里配置自定義呢?就在Mapper.xml中配置。

 

一、parameterType:參數數據類型

 (1)基本數據類型,通過id查詢User。

UserDAO:

//通過id查詢User
public User getById(int id);

UserDAO.xml:

<select id="getById" parameterType="int" resultType="com.southwind.entity.User">
   select * from user where id=#{id}
</select>

 

(2)String類型,通過name查詢User。

UserDAO:

//通過name查詢User
public User getByName(String name);

UserDAO.xml:

<select id="get2" parameterType="java.lang.String" resultType="com.southwind.entity.User">
   select * from user where name = #{name}
</select>

 

(3)包裝類,通過id查詢User。

UserDAO:

//統計id查詢User
public User getById(Integer id);

UserDAO.xml:

<select id="getById" parameterType="java.lang.Integer" resultType="com.southwind.entity.User">
   select * from user where id=#{id}
</select>

 

(4)多個參數,通過name和age查詢User。兩個參數分別是String類型和int類型,類型不一致,所以此時parameterType可以省略,通過參數下標取出參數值。

UserDAO:

//通過name和age查詢User
public User getByNameAge(int id,String name);

UserDAO.xml:

<select id="getByNameAge" resultType="com.southwind.entity.User">
   select * from user where name = #{0} and age = #{1}
</select>

  也可以param0,param1 或 args0,args1

 

(5)POJO,很顯然,當有多個參數時,一個一個寫太麻煩了,這時候我們可以將參數列表進行封裝,將封裝對象作為parameterType的值。

UserDAO:

//根據Usesr封裝對象查詢User
public User getByUser(User user);

UserDAO.xml:

<select id="getByUser" parameterType="com.southwind.entity.User" resultType="com.southwind.entity.User">
   select * from user where name = #{name} and age = #{age}
</select>

 

 

二、resultType:結果類型

(1)基本數據類型,統計User總數。

UserDAO:

//通過User總數量
public int getCount()
UserDAO.xml:
<select id="getCount" resultType="int">
   select count(*) from user
</select>

 

(2)包裝類,統計User總數。

UserDAO:

//通過User總數量
public Integer getCount();

UserDAO.xml:

<select id="getCount" resultType="java.lang.Integer">
   select count(*) from user
</select>

 

(3)String類型,根據id查詢User的name值。

UserDAO:

//根據id查詢User的name
public String getNameById(int id);

UserDAO.xml:

<select id="getNameById" parameterType="int" resultType="java.lang.String">
   select name from user where id = #{name}
</select>

 

(4)POJO,如通過id查詢User,上面已經介紹過了,這里就不再重復了。

 

 

三、級聯查詢

  (1)一對多

我們現在查詢的User是單表查詢,如果是多表關聯查詢,比如查詢Student同時級聯對應的Classes,如何處理呢?

使用resultType無法完成,我們以通過id查詢Student來舉例。

 

SQL:

select * from student as s,classes as c where s.cid = c.c_id and s.id = 1; 

 

查詢結果:

 

學生實體類Student:

package com.southwind.entity;

public class Student {
   private int id;
   private String name;
   private String address;
   private String tel;
   private int score;
   private Classes classes;

   public Classes getClasses() {
       return classes;
   }
   public void setClasses(Classes classes) {
       this.classes = classes;
   }
   public int getId() {
       return id;
   }
   public void setId(int id) {
       this.id = id;
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public String getAddress() {
       return address;
   }
   public void setAddress(String address) {
       this.address = address;
   }
   public String getTel() {
       return tel;
   }
   public void setTel(String tel) {
       this.tel = tel;
   }
   public int getScore() {
       return score;
   }
   public void setScore(int score) {
       this.score = score;
   }

}

 

班級實體類Classes:

package com.southwind.entity;

import java.util.List;

public class Classes {
   private int id;
   private String name;
   private List<Student> students;

   public List<Student> getStudents() {
       return students;
   }
   public void setStudents(List<Student> students) {
       this.students = students;
   }
   public int getId() {
       return id;
   }
   public void setId(int id) {
       this.id = id;
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }

}

 

MyBatis會自動將結果與實體類進行映射,將字段的值賦給對應的屬性,若字段名與屬性名一致,完成賦值,那么問題來了。

 

如果字段不一致怎么辦?

  • 如圖,id,name,address,tel,score屬性可以對應字段,classes屬性沒有對應的字段,准確的講,classes屬性需要對應的對象為c_id,c_name封裝起來的對象。
  • 此時,需要使用resultMap來完成映射。

 

StudentDAO:

//通過id查詢Student
public Student getById(int id);

 

StudentDAO.xml,使用association標簽配置classes級聯,因為一個Student只能對應一個Classes。

<resultMap type="student" id="studentMap">
   <id property="id" column="id"/>
   <result property="name" column="name"/>
   <result property="address" column="address"/>
   <result property="tel" column="tel"/>
   <result property="score" column="score"/>
   <!-- 映射classes屬性 -->
   <association property="classes" javaType="com.southwind.entity.Classes">
       <id property="id" column="c_id"/>
       <result property="name" column="c_name"/>
   </association>
</resultMap>

<select id="getById" parameterType="int" resultMap="studentMap">
   select * from student as s,classes as c where s.cid = c.c_id and s.id = #{id};
</select>

 

同理,反過來查詢Classes,將級聯的所有Student一並查詢。

ClassesDAO:

//根據id查詢Classes
public Classes getById(int id);

ClassesDAO.xml,使用collection標簽配置students級聯,因為一個Classes可以對應多個Student。

<resultMap type="classes" id="classesMap">
   <id property="id" column="c_id"/>
   <result property="name" column="c_name"/>
   <!-- 映射students屬性 -->
   <collection property="students" ofType="student">
       <id property="id" column="id"/>
       <result property="name" column="name"/>
       <result property="address" column="address"/>
       <result property="tel" column="tel"/>
       <result property="score" column="score"/>
   </collection>
</resultMap>

<select id="getById" parameterType="int" resultMap="classesMap">
   select * from classes as c,student as s where c.c_id = s.cid and c.c_id = #{id};
</select>

需要注意的是:association標簽,通過設置javaType屬性,映射實體類,

collection標簽,通過設置ofType屬性映射實體類。

 

 (2)多對多

多對多其實是雙向的一對多關系,我們用Customer和Goods來舉例,

一個Customer可以對應多個Goods,一個Goods也可以對應多個Customer,所以雙方都是用collection標簽設置級聯。

 

Customer:

package com.southwind.entity;

import java.util.List;

public class Customer {
   private int id;
   private String name;
   private List<Goods> goods;
   public int getId() {
       return id;
   }
   public void setId(int id) {
       this.id = id;
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public List<Goods> getGoods() {
       return goods;
   }
   public void setGoods(List<Goods> goods) {
       this.goods = goods;
   }
}

 

Goods:

package com.southwind.entity;

import java.util.List;

public class Goods {
   private int id;
   private String name;
   private List<Customer> customers;
   public int getId() {
       return id;
   }
   public void setId(int id) {
       this.id = id;
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public List<Customer> getCustomers() {
       return customers;
   }
   public void setCustomers(List<Customer> customers) {
       this.customers = customers;
   }  
}

 

CustomerDAO:

//根據id查詢Customer
public Customer getById(int id);

 

CustomerDAO.xml:

<resultMap type="customer" id="customerMap">
   <id property="id" column="c_id"/>
   <result property="name" column="c_name"/>
   <!-- 映射goods屬性 -->
   <collection property="goods" ofType="goods">
       <id property="id" column="g_id"/>
       <result property="name" column="g_name"/>
   </collection>
</resultMap>

<select id="getById" parameterType="int" resultMap="customerMap">
   select * from customer as c,goods as g,
   customer_goods as cg where c.c_id = cg.c_id
   and g.g_id =c g.g_id and c.c_id = #{id};
</select>

 

GoodsDAO:

//根據id查詢Goods
public Goods getById(int id);

 

GoodsDAO.xml:

<resultMap type="goods" id="goodsMap">
   <id property="id" column="g_id"/>
   <result property="name" column="g_name"/>
   <!-- 映射customers屬性 -->
   <collection property="customers" ofType="customer">
       <id property="id" column="c_id"/>
       <result property="name" column="c_name"/>
   </collection>
</resultMap>

<select id="getById" parameterType="int" resultMap="goodsMap">
   select * from customer as c,
   goods as g,customer_goods as cg
   where c.c_id = cg.c_id and g.g_id
   = cg.g_id and g.g_id = #{id};
</select>

 

mybatis中Mapper.xml配置詳解,小伙伴們加油~~~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM