欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

關(guān)于MyBatis10種超好用的寫(xiě)法(收藏)

 更新時(shí)間:2020年08月03日 10:29:57   作者:程序員麥冬  
這篇文章主要介紹了關(guān)于MyBatis10種超好用的寫(xiě)法(收藏),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

用來(lái)循環(huán)容器的標(biāo)簽forEach,查看例子

foreach元素的屬性主要有item,index,collection,open,separator,close。

  • item:集合中元素迭代時(shí)的別名
  • index:集合中元素迭代時(shí)的索引
  • open:常用語(yǔ)where語(yǔ)句中,表示以什么開(kāi)始,比如以'('開(kāi)始
  • separator:表示在每次進(jìn)行迭代時(shí)的分隔符
  • close 常用語(yǔ)where語(yǔ)句中,表示以什么結(jié)束

在使用foreach的時(shí)候最關(guān)鍵的也是最容易出錯(cuò)的就是collection屬性,該屬性是必須指定的,但是在不同情況下,該屬性的值是不一樣的,主要有一下3種情況:

  • 如果傳入的是單參數(shù)且參數(shù)類(lèi)型是一個(gè)List的時(shí)候,collection屬性值為list .
  • 如果傳入的是單參數(shù)且參數(shù)類(lèi)型是一個(gè)array數(shù)組的時(shí)候,collection的屬性值為array .
  • 如果傳入的參數(shù)是多個(gè)的時(shí)候,我們就需要把它們封裝成一個(gè)Map了,當(dāng)然單參數(shù)也可以封裝成map,實(shí)際上如果你在傳入?yún)?shù)的時(shí)候,在MyBatis里面也是會(huì)把它封裝成一個(gè)Map的,map的key就是參數(shù)名,所以這個(gè)時(shí)候collection屬性值就是傳入的List或array對(duì)象在自己封裝的map里面的key.

針對(duì)最后一條,我們來(lái)看一下官方說(shuō)法:

注意 你可以將一個(gè) List 實(shí)例或者數(shù)組作為參數(shù)對(duì)象傳給 MyBatis,當(dāng)你這么做的時(shí)候,MyBatis 會(huì)自動(dòng)將它包裝在一個(gè) Map 中并以名稱(chēng)為鍵。List 實(shí)例將會(huì)以“l(fā)ist”作為鍵,而數(shù)組實(shí)例的鍵將是“array”。
所以,不管是多參數(shù)還是單參數(shù)的list,array類(lèi)型,都可以封裝為map進(jìn)行傳遞。如果傳遞的是一個(gè)List,則mybatis會(huì)封裝為一個(gè)list為key,list值為object的map,如果是array,則封裝成一個(gè)array為key,array的值為object的map,如果自己封裝呢,則colloection里放的是自己封裝的map里的key值

//mapper中我們要為這個(gè)方法傳遞的是一個(gè)容器,將容器中的元素一個(gè)一個(gè)的
//拼接到xml的方法中就要使用這個(gè)forEach這個(gè)標(biāo)簽了
public List<Entity> queryById(List<String> userids);

//對(duì)應(yīng)的xml中如下
 <select id="queryById" resultMap="BaseReslutMap" >
   select * FROM entity
   where id in 
   <foreach collection="userids" item="userid" index="index" open="(" separator="," close=")">
       #{userid}
   </foreach>
 </select>

concat模糊查詢(xún)

//比如說(shuō)我們想要進(jìn)行條件查詢(xún),但是幾個(gè)條件不是每次都要使用,那么我們就可以
//通過(guò)判斷是否拼接到sql中
 <select id="queryById" resultMap="BascResultMap" parameterType="entity">
  SELECT * from entity
  <where>
    <if test="name!=null">
      name like concat('%',concat(#{name},'%'))
    </if>
  </where>
 </select>

choose (when, otherwise)標(biāo)簽

choose標(biāo)簽是按順序判斷其內(nèi)部when標(biāo)簽中的test條件是否成立,如果有一個(gè)成立,則 choose 結(jié)束。當(dāng) choose 中所有 when 的條件都不滿(mǎn)則時(shí),則執(zhí)行 otherwise 中的sql。類(lèi)似于Java 的 switch 語(yǔ)句,choose 為 switch,when 為 case,otherwise 則為 default。

例如下面例子,同樣把所有可以限制的條件都寫(xiě)上,方便使用。choose會(huì)從上到下選擇一個(gè)when標(biāo)簽的test為true的sql執(zhí)行。安全考慮,我們使用where將choose包起來(lái),放置關(guān)鍵字多于錯(cuò)誤。

<!-- choose(判斷參數(shù)) - 按順序?qū)?shí)體類(lèi) User 第一個(gè)不為空的屬性作為:where條件 --> 
<select id="getUserList_choose" resultMap="resultMap_user" parameterType="com.yiibai.pojo.User"> 
  SELECT * 
   FROM User u  
  <where> 
    <choose> 
      <when test="username !=null "> 
        u.username LIKE CONCAT(CONCAT('%', #{username, jdbcType=VARCHAR}),'%') 
      </when > 
      <when test="sex != null and sex != '' "> 
        AND u.sex = #{sex, jdbcType=INTEGER} 
      </when > 
      <when test="birthday != null "> 
        AND u.birthday = #{birthday, jdbcType=DATE} 
      </when > 
      <otherwise> 
      </otherwise> 
    </choose> 
  </where>  
</select>

selectKey 標(biāo)簽

在insert語(yǔ)句中,在Oracle經(jīng)常使用序列、在MySQL中使用函數(shù)來(lái)自動(dòng)生成插入表的主鍵,而且需要方法能返回這個(gè)生成主鍵。使用myBatis的selectKey標(biāo)簽可以實(shí)現(xiàn)這個(gè)效果。

下面例子,使用mysql數(shù)據(jù)庫(kù)自定義函數(shù)nextval('student'),用來(lái)生成一個(gè)key,并把它設(shè)置到傳入的實(shí)體類(lèi)中的studentId屬性上。所以在執(zhí)行完此方法后,便可以通過(guò)這個(gè)實(shí)體類(lèi)獲取生成的key。

<!-- 插入學(xué)生 自動(dòng)主鍵--> 
<insert id="createStudentAutoKey" parameterType="liming.student.manager.data.model.StudentEntity" keyProperty="studentId"> 
  <selectKey keyProperty="studentId" resultType="String" order="BEFORE"> 
    select nextval('student') 
  </selectKey> 
  INSERT INTO STUDENT_TBL(STUDENT_ID, 
              STUDENT_NAME, 
              STUDENT_SEX, 
              STUDENT_BIRTHDAY, 
              STUDENT_PHOTO, 
              CLASS_ID, 
              PLACE_ID) 
  VALUES (#{studentId}, 
      #{studentName}, 
      #{studentSex}, 
      #{studentBirthday}, 
      #{studentPhoto, javaType=byte[], jdbcType=BLOB, typeHandler=org.apache.ibatis.type.BlobTypeHandler}, 
      #{classId}, 
      #{placeId}) 
</insert>

調(diào)用接口方法,和獲取自動(dòng)生成key

StudentEntity entity = new StudentEntity(); 
entity.setStudentName("黎明你好"); 
entity.setStudentSex(1); 
entity.setStudentBirthday(DateUtil.parse("1985-05-28")); 
entity.setClassId("20000001"); 
entity.setPlaceId("70000001"); 
this.dynamicSqlMapper.createStudentAutoKey(entity); 
System.out.println("新增學(xué)生ID: " + entity.getStudentId());

if標(biāo)簽

if標(biāo)簽可用在許多類(lèi)型的sql語(yǔ)句中,我們以查詢(xún)?yōu)槔J紫瓤匆粋€(gè)很普通的查詢(xún):

<!-- 查詢(xún)學(xué)生list,like姓名 --> 
<select id="getStudentListLikeName" parameterType="StudentEntity" resultMap="studentResultMap"> 
  SELECT * from STUDENT_TBL ST  
WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%') 
</select>

但是此時(shí)如果studentName為null,此語(yǔ)句很可能報(bào)錯(cuò)或查詢(xún)結(jié)果為空。此時(shí)我們使用if動(dòng)態(tài)sql語(yǔ)句先進(jìn)行判斷,如果值為null或等于空字符串,我們就不進(jìn)行此條件的判斷,增加靈活性。

參數(shù)為實(shí)體類(lèi)StudentEntity。將實(shí)體類(lèi)中所有的屬性均進(jìn)行判斷,如果不為空則執(zhí)行判斷條件。

<!-- 2 if(判斷參數(shù)) - 將實(shí)體類(lèi)不為空的屬性作為where條件 --> 
<select id="getStudentList_if" resultMap="resultMap_studentEntity" parameterType="liming.student.manager.data.model.StudentEntity"> 
  SELECT ST.STUDENT_ID, 
      ST.STUDENT_NAME, 
      ST.STUDENT_SEX, 
      ST.STUDENT_BIRTHDAY, 
      ST.STUDENT_PHOTO, 
      ST.CLASS_ID, 
      ST.PLACE_ID 
   FROM STUDENT_TBL ST  
   WHERE 
  <if test="studentName !=null "> 
    ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName, jdbcType=VARCHAR}),'%') 
  </if> 
  <if test="studentSex != null and studentSex != '' "> 
    AND ST.STUDENT_SEX = #{studentSex, jdbcType=INTEGER} 
  </if> 
  <if test="studentBirthday != null "> 
    AND ST.STUDENT_BIRTHDAY = #{studentBirthday, jdbcType=DATE} 
  </if> 
  <if test="classId != null and classId!= '' "> 
    AND ST.CLASS_ID = #{classId, jdbcType=VARCHAR} 
  </if> 
  <if test="classEntity != null and classEntity.classId !=null and classEntity.classId !=' ' "> 
    AND ST.CLASS_ID = #{classEntity.classId, jdbcType=VARCHAR} 
  </if> 
  <if test="placeId != null and placeId != '' "> 
    AND ST.PLACE_ID = #{placeId, jdbcType=VARCHAR} 
  </if> 
  <if test="placeEntity != null and placeEntity.placeId != null and placeEntity.placeId != '' "> 
    AND ST.PLACE_ID = #{placeEntity.placeId, jdbcType=VARCHAR} 
  </if> 
  <if test="studentId != null and studentId != '' "> 
    AND ST.STUDENT_ID = #{studentId, jdbcType=VARCHAR} 
  </if>  
</select>

使用時(shí)比較靈活, new一個(gè)這樣的實(shí)體類(lèi),我們需要限制那個(gè)條件,只需要附上相應(yīng)的值就會(huì)where這個(gè)條件,相反不去賦值就可以不在where中判斷。

public void select_test_2_1() { 
  StudentEntity entity = new StudentEntity(); 
  entity.setStudentName(""); 
  entity.setStudentSex(1); 
  entity.setStudentBirthday(DateUtil.parse("1985-05-28")); 
  entity.setClassId("20000001"); 
  //entity.setPlaceId("70000001"); 
  List<StudentEntity> list = this.dynamicSqlMapper.getStudentList_if(entity); 
  for (StudentEntity e : list) { 
    System.out.println(e.toString()); 
  } 
}

if + where 的條件判斷

當(dāng)where中的條件使用的if標(biāo)簽較多時(shí),這樣的組合可能會(huì)導(dǎo)致錯(cuò)誤。我們以在3.1中的查詢(xún)語(yǔ)句為例子,當(dāng)java代碼按如下方法調(diào)用時(shí):

@Test 
public void select_test_2_1() { 
  StudentEntity entity = new StudentEntity(); 
  entity.setStudentName(null); 
  entity.setStudentSex(1); 
  List<StudentEntity> list = this.dynamicSqlMapper.getStudentList_if(entity); 
  for (StudentEntity e : list) { 
    System.out.println(e.toString()); 
  } 
}

如果上面例子,參數(shù)studentName為null,將不會(huì)進(jìn)行STUDENT_NAME列的判斷,則會(huì)直接導(dǎo)“WHERE AND”關(guān)鍵字多余的錯(cuò)誤SQL。

這時(shí)我們可以使用where動(dòng)態(tài)語(yǔ)句來(lái)解決。這個(gè)“where”標(biāo)簽會(huì)知道如果它包含的標(biāo)簽中有返回值的話,它就插入一個(gè)‘where'。此外,如果標(biāo)簽返回的內(nèi)容是以AND 或OR 開(kāi)頭的,則它會(huì)剔除掉。

上面例子修改為:

<!-- 3 select - where/if(判斷參數(shù)) - 將實(shí)體類(lèi)不為空的屬性作為where條件 --> 
<select id="getStudentList_whereIf" resultMap="resultMap_studentEntity" parameterType="liming.student.manager.data.model.StudentEntity"> 
  SELECT ST.STUDENT_ID, 
      ST.STUDENT_NAME, 
      ST.STUDENT_SEX, 
      ST.STUDENT_BIRTHDAY, 
      ST.STUDENT_PHOTO, 
      ST.CLASS_ID, 
      ST.PLACE_ID 
   FROM STUDENT_TBL ST  
  <where> 
    <if test="studentName !=null "> 
      ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName, jdbcType=VARCHAR}),'%') 
    </if> 
    <if test="studentSex != null and studentSex != '' "> 
      AND ST.STUDENT_SEX = #{studentSex, jdbcType=INTEGER} 
    </if> 
    <if test="studentBirthday != null "> 
      AND ST.STUDENT_BIRTHDAY = #{studentBirthday, jdbcType=DATE} 
    </if> 
    <if test="classId != null and classId!= '' "> 
      AND ST.CLASS_ID = #{classId, jdbcType=VARCHAR} 
    </if> 
    <if test="classEntity != null and classEntity.classId !=null and classEntity.classId !=' ' "> 
      AND ST.CLASS_ID = #{classEntity.classId, jdbcType=VARCHAR} 
    </if> 
    <if test="placeId != null and placeId != '' "> 
      AND ST.PLACE_ID = #{placeId, jdbcType=VARCHAR} 
    </if> 
    <if test="placeEntity != null and placeEntity.placeId != null and placeEntity.placeId != '' "> 
      AND ST.PLACE_ID = #{placeEntity.placeId, jdbcType=VARCHAR} 
    </if> 
    <if test="studentId != null and studentId != '' "> 
      AND ST.STUDENT_ID = #{studentId, jdbcType=VARCHAR} 
    </if> 
  </where>  
</select>

if + set實(shí)現(xiàn)修改語(yǔ)句

當(dāng)update語(yǔ)句中沒(méi)有使用if標(biāo)簽時(shí),如果有一個(gè)參數(shù)為null,都會(huì)導(dǎo)致錯(cuò)誤。

當(dāng)在update語(yǔ)句中使用if標(biāo)簽時(shí),如果前面的if沒(méi)有執(zhí)行,則或?qū)е露禾?hào)多余錯(cuò)誤。使用set標(biāo)簽可以將動(dòng)態(tài)的配置SET 關(guān)鍵字,和剔除追加到條件末尾的任何不相關(guān)的逗號(hào)。使用if+set標(biāo)簽修改后,如果某項(xiàng)為null則不進(jìn)行更新,而是保持?jǐn)?shù)據(jù)庫(kù)原值。

如下示例:

<!-- 4 if/set(判斷參數(shù)) - 將實(shí)體類(lèi)不為空的屬性更新 --> 
<update id="updateStudent_if_set" parameterType="liming.student.manager.data.model.StudentEntity"> 
  UPDATE STUDENT_TBL 
  <set> 
    <if test="studentName != null and studentName != '' "> 
      STUDENT_TBL.STUDENT_NAME = #{studentName}, 
    </if> 
    <if test="studentSex != null and studentSex != '' "> 
      STUDENT_TBL.STUDENT_SEX = #{studentSex}, 
    </if> 
    <if test="studentBirthday != null "> 
      STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday}, 
    </if> 
    <if test="studentPhoto != null "> 
      STUDENT_TBL.STUDENT_PHOTO = #{studentPhoto, javaType=byte[], jdbcType=BLOB, typeHandler=org.apache.ibatis.type.BlobTypeHandler}, 
    </if> 
    <if test="classId != '' "> 
      STUDENT_TBL.CLASS_ID = #{classId} 
    </if> 
    <if test="placeId != '' "> 
      STUDENT_TBL.PLACE_ID = #{placeId} 
    </if> 
  </set> 
  WHERE STUDENT_TBL.STUDENT_ID = #{studentId};   
</update>

if + trim代替where/set標(biāo)簽

trim是更靈活的去除多余關(guān)鍵字的標(biāo)簽,他可以實(shí)踐where和set的效果。

trim代替where

<!-- 5.1if/trim代替where(判斷參數(shù)) -將實(shí)體類(lèi)不為空的屬性作為where條件--> 
<select id="getStudentList_if_trim" resultMap="resultMap_studentEntity"> 
  SELECT ST.STUDENT_ID, 
      ST.STUDENT_NAME, 
      ST.STUDENT_SEX, 
      ST.STUDENT_BIRTHDAY, 
      ST.STUDENT_PHOTO, 
      ST.CLASS_ID, 
      ST.PLACE_ID 
   FROM STUDENT_TBL ST  
  <trim prefix="WHERE" prefixOverrides="AND|OR"> 
    <if test="studentName !=null "> 
      ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName, jdbcType=VARCHAR}),'%') 
    </if> 
    <if test="studentSex != null and studentSex != '' "> 
      AND ST.STUDENT_SEX = #{studentSex, jdbcType=INTEGER} 
    </if> 
    <if test="studentBirthday != null "> 
      AND ST.STUDENT_BIRTHDAY = #{studentBirthday, jdbcType=DATE} 
    </if> 
    <if test="classId != null and classId!= '' "> 
      AND ST.CLASS_ID = #{classId, jdbcType=VARCHAR} 
    </if> 
    <if test="classEntity != null and classEntity.classId !=null and classEntity.classId !=' ' "> 
      AND ST.CLASS_ID = #{classEntity.classId, jdbcType=VARCHAR} 
    </if> 
    <if test="placeId != null and placeId != '' "> 
      AND ST.PLACE_ID = #{placeId, jdbcType=VARCHAR} 
    </if> 
    <if test="placeEntity != null and placeEntity.placeId != null and placeEntity.placeId != '' "> 
      AND ST.PLACE_ID = #{placeEntity.placeId, jdbcType=VARCHAR} 
    </if> 
    <if test="studentId != null and studentId != '' "> 
      AND ST.STUDENT_ID = #{studentId, jdbcType=VARCHAR} 
    </if> 
  </trim>   
</select>

trim代替set

<!-- 5.2 if/trim代替set(判斷參數(shù)) - 將實(shí)體類(lèi)不為空的屬性更新 --> 
<update id="updateStudent_if_trim" parameterType="liming.student.manager.data.model.StudentEntity"> 
  UPDATE STUDENT_TBL 
  <trim prefix="SET" suffixOverrides=","> 
    <if test="studentName != null and studentName != '' "> 
      STUDENT_TBL.STUDENT_NAME = #{studentName}, 
    </if> 
    <if test="studentSex != null and studentSex != '' "> 
      STUDENT_TBL.STUDENT_SEX = #{studentSex}, 
    </if> 
    <if test="studentBirthday != null "> 
      STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday}, 
    </if> 
    <if test="studentPhoto != null "> 
      STUDENT_TBL.STUDENT_PHOTO = #{studentPhoto, javaType=byte[], jdbcType=BLOB, typeHandler=org.apache.ibatis.type.BlobTypeHandler}, 
    </if> 
    <if test="classId != '' "> 
      STUDENT_TBL.CLASS_ID = #{classId}, 
    </if> 
    <if test="placeId != '' "> 
      STUDENT_TBL.PLACE_ID = #{placeId} 
    </if> 
  </trim> 
  WHERE STUDENT_TBL.STUDENT_ID = #{studentId} 
</update>

foreach

對(duì)于動(dòng)態(tài)SQL 非常必須的,主是要迭代一個(gè)集合,通常是用于IN 條件。List 實(shí)例將使用“l(fā)ist”作為鍵,數(shù)組實(shí)例以“array” 做為鍵。

foreach元素是非常強(qiáng)大的,它允許你指定一個(gè)集合,聲明集合項(xiàng)和索引變量,它們可以用在元素體內(nèi)。它也允許你指定開(kāi)放和關(guān)閉的字符串,在迭代之間放置分隔符。這個(gè)元素是很智能的,它不會(huì)偶然地附加多余的分隔符。

注意:你可以傳遞一個(gè)List實(shí)例或者數(shù)組作為參數(shù)對(duì)象傳給MyBatis。當(dāng)你這么做的時(shí)候,MyBatis會(huì)自動(dòng)將它包裝在一個(gè)Map中,用名稱(chēng)在作為鍵。List實(shí)例將會(huì)以“l(fā)ist”作為鍵,而數(shù)組實(shí)例將會(huì)以“array”作為鍵。
這個(gè)部分是對(duì)關(guān)于XML配置文件和XML映射文件的而討論的。下一部分將詳細(xì)討論Java API,所以你可以得到你已經(jīng)創(chuàng)建的最有效的映射。

1.參數(shù)為array示例的寫(xiě)法

接口的方法聲明:

public List<StudentEntity> getStudentListByClassIds_foreach_array(String[] classIds);

動(dòng)態(tài)SQL語(yǔ)句:

<!-- 7.1 foreach(循環(huán)array參數(shù)) - 作為where中in的條件 --> 
<select id="getStudentListByClassIds_foreach_array" resultMap="resultMap_studentEntity"> 
  SELECT ST.STUDENT_ID, 
      ST.STUDENT_NAME, 
      ST.STUDENT_SEX, 
      ST.STUDENT_BIRTHDAY, 
      ST.STUDENT_PHOTO, 
      ST.CLASS_ID, 
      ST.PLACE_ID 
   FROM STUDENT_TBL ST 
   WHERE ST.CLASS_ID IN  
   <foreach collection="array" item="classIds" open="(" separator="," close=")"> 
    #{classIds} 
   </foreach> 
</select>

測(cè)試代碼,查詢(xún)學(xué)生中,在20000001、20000002這兩個(gè)班級(jí)的學(xué)生:

@Test 
public void test7_foreach() { 
  String[] classIds = { "20000001", "20000002" }; 
  List<StudentEntity> list = this.dynamicSqlMapper.getStudentListByClassIds_foreach_array(classIds); 
  for (StudentEntity e : list) { 
    System.out.println(e.toString()); 
  } 
}

2.參數(shù)為list示例的寫(xiě)法

接口的方法聲明:

public List<StudentEntity> getStudentListByClassIds_foreach_list(List<String> classIdList);

動(dòng)態(tài)SQL語(yǔ)句:

<!-- 7.2 foreach(循環(huán)List<String>參數(shù)) - 作為where中in的條件 --> 
<select id="getStudentListByClassIds_foreach_list" resultMap="resultMap_studentEntity"> 
  SELECT ST.STUDENT_ID, 
      ST.STUDENT_NAME, 
      ST.STUDENT_SEX, 
      ST.STUDENT_BIRTHDAY, 
      ST.STUDENT_PHOTO, 
      ST.CLASS_ID, 
      ST.PLACE_ID 
   FROM STUDENT_TBL ST 
   WHERE ST.CLASS_ID IN  
   <foreach collection="list" item="classIdList" open="(" separator="," close=")"> 
    #{classIdList} 
   </foreach> 
</select>

測(cè)試代碼,查詢(xún)學(xué)生中,在20000001、20000002這兩個(gè)班級(jí)的學(xué)生:

@Test 
public void test7_2_foreach() { 
  ArrayList<String> classIdList = new ArrayList<String>(); 
  classIdList.add("20000001"); 
  classIdList.add("20000002"); 
  List<StudentEntity> list = this.dynamicSqlMapper.getStudentListByClassIds_foreach_list(classIdList); 
  for (StudentEntity e : list) { 
    System.out.println(e.toString()); 
  } 
}

sql片段標(biāo)簽<sql>:通過(guò)該標(biāo)簽可定義能復(fù)用的sql語(yǔ)句片段,在執(zhí)行sql語(yǔ)句標(biāo)簽中直接引用即可。這樣既可以提高編碼效率,還能有效簡(jiǎn)化代碼,提高可讀性

需要配置的屬性:id="" >>>表示需要改sql語(yǔ)句片段的唯一標(biāo)識(shí)

引用:通過(guò)<include refid="" />標(biāo)簽引用,refid="" 中的值指向需要引用的<sql>中的id=“”屬性

<!--定義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 
<!--引用sql片段--> 
  <include refid="orderAndItem" /> 
  from ordertable o 
  join orderitem i on o.orderitem_id = i.orderitem_id 
  where o.order_id = #{orderId} 
 </select>

到此這篇關(guān)于關(guān)于MyBatis10種超好用的寫(xiě)法(收藏)的文章就介紹到這了,更多相關(guān)MyBatis 寫(xiě)法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring項(xiàng)目集成RabbitMQ及自動(dòng)創(chuàng)建隊(duì)列

    Spring項(xiàng)目集成RabbitMQ及自動(dòng)創(chuàng)建隊(duì)列

    這篇文章主要介紹了Spring項(xiàng)目集成RabbitMQ及自動(dòng)創(chuàng)建隊(duì)列,本文內(nèi)容分別在Spring(V5.2.6)和Spring Boot(V2.5.14)兩個(gè)項(xiàng)目中經(jīng)過(guò)了驗(yàn)證,需要的朋友可以參考下
    2024-02-02
  • idea out目錄與target目錄的區(qū)別詳解

    idea out目錄與target目錄的區(qū)別詳解

    這篇文章主要介紹了idea out目錄與target目錄的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-02-02
  • Spring MVC參數(shù)校驗(yàn)詳解(關(guān)于`@RequestBody`返回`400`)

    Spring MVC參數(shù)校驗(yàn)詳解(關(guān)于`@RequestBody`返回`400`)

    這篇文章主要介紹了Spring MVC參數(shù)校驗(yàn)的相關(guān)資料,主要是針對(duì)`@RequestBody`返回`400`的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-08-08
  • Java并發(fā)編程中構(gòu)建自定義同步工具

    Java并發(fā)編程中構(gòu)建自定義同步工具

    這篇文章主要介紹了Java并發(fā)編程中構(gòu)建自定義同步工具,本文講解了可阻塞狀態(tài)依賴(lài)操作的結(jié)構(gòu)、有界緩存實(shí)現(xiàn)基類(lèi)示例、阻塞實(shí)現(xiàn)方式一:拋異常給調(diào)用者、阻塞實(shí)現(xiàn)方式二:通過(guò)輪詢(xún)和休眠、阻塞實(shí)現(xiàn)方式三:條件隊(duì)列等內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • Springboot配置suffix指定mvc視圖的后綴方法

    Springboot配置suffix指定mvc視圖的后綴方法

    這篇文章主要介紹了Springboot配置suffix指定mvc視圖的后綴方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • SpringBoot如何訪問(wèn)本地圖片

    SpringBoot如何訪問(wèn)本地圖片

    這篇文章主要介紹了SpringBoot如何訪問(wèn)本地圖片問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • spring注解@Import用法詳解

    spring注解@Import用法詳解

    這篇文章主要介紹了spring注解@Import用法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • Java設(shè)計(jì)模式之建造者模式

    Java設(shè)計(jì)模式之建造者模式

    這篇文章介紹了Java設(shè)計(jì)模式之建造者模式,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-09-09
  • Java分代垃圾回收策略原理詳解

    Java分代垃圾回收策略原理詳解

    這篇文章主要介紹了Java分代垃圾回收策略原理詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • 用Java實(shí)現(xiàn)全國(guó)天氣預(yù)報(bào)的api接口調(diào)用示例

    用Java實(shí)現(xiàn)全國(guó)天氣預(yù)報(bào)的api接口調(diào)用示例

    查詢(xún)天氣預(yù)報(bào)在APP中常用的一個(gè)常用功能,本文實(shí)例講述了java調(diào)用中國(guó)天氣網(wǎng)api獲得天氣預(yù)報(bào)信息的方法。分享給大家供大家參考。
    2016-10-10

最新評(píng)論