一、導入約束
為全局配置文件綁定dtd約束:
1)聯網會自動綁定
2)沒網的時候【/org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd】:解壓mybatis 的jar包然后在eclipse中綁定
window——preperances——XML Catalog ——Add——設置key為http://mybatis.org/dtd/mybatis-3-mapper.dtd,綁定本地文件位置location:
1 <!DOCTYPE mapper 2 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
二、mapper映射文件結構
<mapper namespace="mapper.buyrecordMapper">
<!--namespace 必須配置成接口的全限定名。Mybatis內部就是通過這講接口和mapepr.xml關聯起來的。接口中方法和mapper.xml 中的id對應。否則會報錯--> resultMap (自定義映射結果集) sql標簽 <select id=""></select>查詢標簽 <insert id=""></insert>插入標簽 <update id=""></update>更新標簽 <delete id=""></delete>刪除標簽 </mapper>
三、resultMap (自定義映射結果集)
<!--resultMap將數據庫中的列(字段名)和entity中屬性的名字建立映射關聯,id 這個resultMap的唯一標識,type表示要映射的實體--> <resultMap type="Goods" id="Map"> column:數據庫字段,property:映射屬性 id列:主鍵列,result普通列 <id column="id" property="id" /> <result column="name" property="name" /> <result column="flag" property="flag" /> 多表查詢: 一對一,多對一assonciation標簽,用javatype映射 一對多,collection標簽,用oftype映射 <association property="GoodsType" javaType="GoodsType"> <id column="id" property="id"/> <result column="tname" property="name"/> </association> <!--***********分割線**************--> <collection property="employees" ofType="employees"> <result column="ename" property="name" /> </collection> </resultMap>
四、sql標簽
<resultType="###"> <resultMap="###"> 二選一,沒配置或配置錯,執行程序必報錯
<select id="finduser" resultType="User"> select * from account ; </select> <insert id="adduser" parameterType="user"> insert into account(name,money) values(#{name},#{money}) </insert> <update id="updateuser" parameterType="user"> update account set name=#{name},money=#{money} where id=#{id} </update> <delete id="deluser" parameterType="int"> delete from account where id=#{id} </delete>
五、動態sql
< where >和< if >標簽
< where > : 主要用來替換sql語句中的where字段,他的作用主要是用來簡化sql語句中where條件判斷的書寫的
< if >:條件判斷標簽,配置屬性test=" 條件字符串 ",判斷是否滿足條件,滿足則執行,不滿足則跳過
1 <select id="deptif" resultType="dept" parameterType="dept"> 2 select * from dept 3 <where> 4 <if test="dname!=null and dname.trim()!=''"> 5 and dname like concat('%',#{dname},'%') 6 </if> 7 <if test="loc!=null and loc.trim()!=''"> 8 and loc=#{loc} 9 </if> 10 </where> 11 </select>
< set >標簽
< set > : 主要用來替換sql語句中的set字段,一般在update中使用。
1 <update id="update" parameterType="Dept"> 2 update dept 3 <set> 4 <if test="dname!=null"> 5 dname=#{dname}, 6 </if> 7 <if test="loc!=null"> 8 loc=#{loc} 9 </if> 10 </set> 11 where id=#{id} 12 </update>
< trim>標簽
< trim > : 是一個格式化的標記,可以完成set或者是where標記的功能
prefix:前綴
prefixoverride:去掉第一個and或者是or
1 <select id="depttrim" resultType="Dept" parameterType="Dept"> 2 select * from dept 3 <trim prefix="where" suffixOverrides="and"> 4 <if test="dname!=null and dname.trim()!=''"> 5 dname like concat('%',#{dname},'%') and 6 </if> 7 <if test="loc!=null and loc.trim()!=''"> 8 loc=#{loc} 9 </if> 10 </trim> 11 </select>
< choose >標簽
< where > : choose標簽是按順序判斷其內部when標簽中的test條件出否成立,如果有一個成立,則 choose 結束。
當 choose 中所有 when 的條件都不滿則時,則執行 otherwise 中的sql。
類似於Java 的 switch 語句,choose 為 switch,when 為 case,otherwise 則為 default。
1 <select id="deptchoose" resultType="Dept" parameterType="Dept"> 2 select * from dept 3 <where> 4 <choose> 5 <when test="loc!=null"> 6 loc=#{loc} 7 </when> 8 <when test="dname!=null and dname.trim()!=''"> 9 dname like concat('%',#{dname},'%') 10 </when> 11 <otherwise> 12 id=#{id} 13 </otherwise> 14 </choose> 15 </where> 16 </select>
<foreach>標簽
foreach標簽經常用於遍歷集合,構建in條件語句或者批量操作語句。
1 <select id="deptforeach" resultType="Dept"> 2 select * from dept 3 <where> 4 <if test="list!=null and list.size()>0"> 5 <foreach collection="list" separator="," open="id in(" 6 close=")" item="uid"> 7 #{uid} 8 </foreach> 9 </if> 10 </where> 11 </select>
<insert id="addProperties" parameterType="java.util.List"> INSERT INTO tdt_sync_instance_properties (name,instance_id) VALUES <foreach collection="list" item="prop" separator=","> (#{prop.name},#{prop.instance_id}) </foreach> ; </insert>