Mybatis的mapper xml文件中的常用標簽
一、SQL語句標簽:
1、<!--查詢語句-->
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" > select </select>
2、<!--插入語句-->
<insert id="insert" parameterType="pojo.OrderTable" > insert into ordertable (order_id, cid, address, create_date, orderitem_id) values (#{orderId,jdbcType=VARCHAR},#{cid,jdbcType=VARCHAR}, #{address,jdbcType=VARCHAR}, #{createDate,jdbcType=TIMESTAMP}, #{orderitemId,jdbcType=VARCHAR}) </insert>
3、<!--刪除語句-->
<delete id="deleteByPrimaryKey" parameterType="java.lang.String" > delete from ordertable where order_id = #{orderId,jdbcType=VARCHAR} </delete>
4、<!--修改語句-->
<update id="updateByPrimaryKey" parameterType="pojo.OrderTable" > update ordertable set cid = #{cid,jdbcType=VARCHAR}, address = #{address,jdbcType=VARCHAR}, create_date = #{createDate,jdbcType=TIMESTAMP}, orderitem_id = #{orderitemId,jdbcType=VARCHAR} where order_id = #{orderId,jdbcType=VARCHAR} </update>
1》需要配置的屬性:
★id="xxxx" ——表示此段sql執行語句的唯一標識,也是接口的方法名稱【必須一致才能找到】
★parameterType="xxxx" ——表示該sql語句中需要傳入的參數, 類型要與對應的接口方法的類型一致【可選】
★resultMap="xxx"—— 定義出參,調用已定義的<resultMap>映射管理器的id值
★resultType="xxxx"——定義出參,匹配普通Java類型或自定義的pojo【出參類型若不指定,將為語句類型默認類型,如<insert>語句返回值為int】
2》注意: 至於為何<insert><delete><update> 語句的返回值類型為什么是int,有過JDBC操作經驗的朋友可能會有印象,增刪改操作實際上返回的是操作的條數。而Mybatis框架本身是基於JDBC的,所以此處也沿襲這種返回值類型。
3》傳參和取值:mapper.xml 的靈活性還體現在SQL執行語句可以傳參,參數類型通過parameterType= "" 定義
★取值方式1:#{value jdbcType = valuetype}:jdbcType 表示該屬性的數據類型在數據庫中對應的類型,如 #{user jdbcType=varchar} 等價於 String username;
★取值方式2:${value } : 這種方式不建議大量使用,可能會發送sql注入而導致安全性問題。一般該取值方式可用在非經常變化的值上,如orderby ${columnName};
二、sql片段標簽<sql>:通過該標簽可定義能復用的sql語句片段,在執行sql語句標簽中直接引用即可。這樣既可以提高編碼效率,還能有效簡化代碼,提高可讀性
1》 需要配置的屬性:id="" ———表示需要改sql語句片段的唯一標識
2》引用:通過<include refid="" />標簽引用,refid="" 中的值指向需要引用的<sql>中的id=""屬性
1、<!--定義sql片段-->
<sql id="orderAndItem"> o.order_id,o.cid,o.address,o.create_date,o.orderitem_id,i.orderitem_id,i.product_id,i.count </sql> <select id="findOrderAndItemsByOid" parameterType="java.lang.String" resultMap="BaseResultMap">
select
2、<!--引用sql片段-->
<include refid="orderAndItem" /> from ordertable o join orderitem i on o.orderitem_id = i.orderitem_id where o.order_id = #{orderId} </select>
三、映射管理器resultMap:映射管理器,是Mybatis中最強大的工具,使用其可以進行實體類之間的關系,並管理結果和實體類間的映射關系
1》需要配置的屬性:<resultMap id="" type=""></resutlMap>
id=""——表示這個映射管理器的唯一標識,外部通過該值引用;
type ="" ——表示需要映射的實體類;
2》 需要配置的參數:<id column = "" property= "" />
<id>標簽指的是:結果集中結果唯一的列【column】 和 實體屬性【property】的映射關系,
3》注意:<id>標簽管理的列未必是主鍵列,需要根據具體需求指定;
<result column= " " property=" " /> <result>標簽指的是:結果集中普通列【column】 和 實體屬性【property】的映射關系;
4》 需要維護的關系:所謂關系維護是值在主表查詢時將其關聯子表的結果也查詢出來
1)一對一關系
<assocation property = " " javaType=" ">
property = “ ”→ 被維護實體在宿主實體中的屬性名,
javaType = " "→ 被維護實體的類型
Orderitem.java
package pojo;
public class Orderitem {
private String orderitemId;
private String productId;
private Integer count;
private Product product;
從上方代碼段可以看出:Product 對象在 Orderitem 實體中以 product 屬性存在
Orderitemmapper.xml
<resultMap id="BaseResultMap" type="pojo.Orderitem" > <id column="orderitem_id" property="orderitemId" jdbcType="VARCHAR" /> <result column="product_id" property="productId" jdbcType="VARCHAR" /> <result column="count" property="count" jdbcType="INTEGER" />
★ <!-- 通過association 維護 一對一關系 -->
<association property="product" javaType="pojo.Product">
<id column="product_id" property="productId"/>
<result column="product_factroy" property="productFactroy"/>
<result column="product_store" property="productStore"/>
<result column="product_descript" property="productDescript"/>
</association>
</resultMap>
通過xml的配置可以看出,在resultMap映射管理器中,通過<association> 進行了維護,也就是在查詢Orderitem對象時,可以把關聯的Product對象的信息也查詢出來
2)一對多關系的維護
<collection property=" " ofType=" ">
property = “”→ 被維護實體在宿主實體中的屬性名
ofType=“ ”→是被維護方在宿主類中集合泛型限定類型
【由於在一對多關系中,多的一放是以List形式存在,因此ofType的值取用Lsit<?> 的泛型對象類型】
OrderTable.java public class OrderTable { private String orderId; private String cid; private String address; private Date createDate; private String orderitemId; private List<Orderitem> orderitemList ; } OrderTableMapper.xml; <resultMap id="BaseResultMap" type="pojo.OrderTable" > <!-- WARNING - @mbggenerated This element is automatically generated by MyBatis Generator, do not modify. This element was generated on Fri May 06 15:49:42 CST 2016. --> <id column="order_id" property="orderId" jdbcType="VARCHAR" /> <result column="cid" property="cid" jdbcType="VARCHAR" /> <result column="address" property="address" jdbcType="VARCHAR" /> <result column="create_date" property="createDate" jdbcType="TIMESTAMP" /> <result column="orderitem_id" property="orderitemId" jdbcType="VARCHAR" />
★ <!--維護一對多的關系 -->
<collection property="orderitemList" ofType="pojo.Orderitem"> <id column="orderitem_id" property="orderitemId"/> <result column="product_id" property="productId"/> <result column="count" property="count"/> </collection> </resultMap>
5》在resultMap 中需要注意兩點:
關聯關系的維護可以根據實體類之間的實際情況進行嵌套維護
<resultMap id="BaseResultMap" type="pojo.OrderTable" > <id column="order_id" property="orderId" jdbcType="VARCHAR" /> <result column="cid" property="cid" jdbcType="VARCHAR" /> <result column="address" property="address" jdbcType="VARCHAR" /> <result column="create_date" property="createDate" jdbcType="TIMESTAMP" /> <result column="orderitem_id" property="orderitemId" jdbcType="VARCHAR" />
★<!--維護一對多的關系 -->
<collection property="orderitemList" ofType="pojo.Orderitem"> <id column="orderitem_id" property="orderitemId"/> <result column="product_id" property="productId"/> <result column="count" property="count"/> <span style="white-space:pre"> </span><!--嵌套一對一關系--> <association property="customer" javaType="pojo.Customer"> <id column="cid" property="cid"/> <result column="cname" property="cname"/> </association> </collection> </resultMap>
關於出現重復列名的處理:在實際操作過程中,查詢到的結果可能會出現相同的列名,這樣會對映射到實體屬性帶來影響甚至出現報錯,那么對待這個問題可以通過對列取別名的方式處理
四:常用的動態語句標簽:通過動態sql標簽可以進行條件判斷,條件遍歷等操作從而滿足結果的需要
1》<where> : 使用其可以代替sql語句中的where關鍵字,一般防止在條件查詢的最外層
2》<if >:條件判斷標簽,配置屬性test=" 條件字符串 ",判斷是否滿足條件,滿足則執行,不滿足則跳過
<select id="findOrderItemDetail" parameterType="pojo.Orderitem" resultMap="BaseResultMap"> select orderitem.orderitem_id,product.* from orderitem,product <where> <if test="orderitemId!=null and orderitemId!=''"> and orderitem.orderitem_id = #{orderitemId} </if> <if test="productId!=null and productId!=''"> and orderitem.product_id = #{productId} </if> <if test="count!=null"> and orderitem.count = #{count} </if> </where> </select> 3》<set>:常用於<update>更新語句中,替代 sql中的“set”關鍵字,特別是在聯合<if>進行判斷是,可以有效方式當某個參數為空或者不合法是錯誤的更新到數據庫中 <update id="updateByPrimaryKeySelective" parameterType="pojo.Orderitem" > update orderitem <set > <if test="productId != null" > product_id = #{productId,jdbcType=VARCHAR}, </if> <if test="count != null" > count = #{count,jdbcType=INTEGER}, </if> </set> where orderitem_id = #{orderitemId,jdbcType=VARCHAR} </update>
4》<choose><when></when><otherwise></otherwise>
</choose> 標簽組:也是一個用於條件判斷的標簽組,和<if>的不同之處在於條件從<choose>進入,去匹配<when>中的添加,一旦匹配馬上結束;若到找不到匹配項,將執行<other>中的語句;可以理解為<if>是 && 關系 <choose>是 || 關系
<!-- 查詢學生list,like姓名、或=性別、或=生日、或=班級,使用choose -->
<select id="getStudentListChooseEntity" parameterType="StudentEntity" resultMap="studentResultMap"> SELECT * from STUDENT_TBL ST <where> <choose> <when test="studentName!=null and studentName!='' "> ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%') </when> <when test="studentSex!= null and studentSex!= '' "> AND ST.STUDENT_SEX = #{studentSex} </when> <when test="studentBirthday!=null"> AND ST.STUDENT_BIRTHDAY = #{studentBirthday} </when> <when test="classEntity!=null and classEntity.classID !=null and classEntity.classID!='' "> AND ST.CLASS_ID = #{classEntity.classID} </when> <otherwise> </otherwise> </choose> </where> </select>
5》<foreach>標簽:該標簽的作用是遍歷集合類型的條件
foreach元素的屬性主要有 item,index,collection,open,separator,close。
item表示集合中每一個元素進行迭代時的別名.
index指 定一個名字,用於表示在迭代過程中,每次迭代到的位置.
open表示該語句以什么開始,separator表示在每次進行迭代之間以什么符號作為分隔 符.
close表示以什么結束
用來循環 collection : 用來指定循環的數據的類型 可以填的值有:array,list,map item
<delete id="deleteByPriKeys" parameterType="java.lang.String"> delete from product where product_Id in <foreach collection="list" item="productId" open="(" separator="," close=")"> #{productId,jdbcType = VARCHAR} </foreach> </delete>