MyBatis?Mapper.XML?標(biāo)簽使用小結(jié)
直接將值返回給對(duì)象
<select id="list" resultType="com.vipsoft.base.entity.UserInfo"> SELECT Id,Title FROM User </select>
如果字段和屬性名不一致時(shí),通過 resultMap 做映射
<resultMap id="StudentResult" type="com.mybatis3.domain.Student"> <id column="stud_id" property="studId" jdbcType="VARCHAR"/> <result column="user_name" property="name" jdbcType="VARCHAR"/> <result column="email" property="email" jdbcType="VARCHAR" /> <result column="phone" property="phone" jdbcType="VARCHAR" /> <result column="status" property="status" jdbcType="INTEGER" /> <result column="create_time" property="createTime" jdbcType="TIMESTAMP" /> </resultMap> <select id="findAllStudents" resultMap="StudentResult" > SELECT * FROM STUDENTS </select>
實(shí)體引用另一個(gè)實(shí)體的查詢結(jié)果返回
<resultMap type="Course" id="CourseResult">
<id column="course_id" property="courseId"/>
<result column="name" property="name"/>
<result column="description" property="description"/>
<result column="start_date" property="startDate"/>
<result column="end_date" property="endDate"/>
</resultMap>
<resultMap type="Tutor" id="TutorResult">
<id column="tutor_id" property="tutorId"/>
<result column="tutor_name" property="name"/>
<result column="email" property="email"/>
<collection property="courses" resultMap="CourseResult"/>
</resultMap>
<select id="findTutorById" parameterType="int" resultMap="TutorResult">
SELECT T.TUTOR_ID, T.NAME AS TUTOR_NAME, EMAIL, C.COURSE_ID, C.NAME, DESCRIPTION, START_DATE, END_DATE
FROM TUTORS T LEFT OUTER JOIN ADDRESSES A ON T.ADDR_ID=A.ADDR_ID
LEFT OUTER JOIN COURSES C ON T.TUTOR_ID=C.TUTOR_ID
WHERE T.TUTOR_ID=#{tutorId}
</select>choose 該方式適用于多個(gè)條件中選擇一個(gè)滿足條件的來生成sql
<select id="searchCourses" parameterType="hashmap" resultMap="CourseResult">
SELECT * FROM COURSES
<choose>
<when test="searchBy == 'Tutor'">
WHERE TUTOR_ID= #{tutorId}
</when>
<when test="searchBy == 'CourseName'">
WHERE name like #{courseName}
</when>
<otherwise>
WHERE TUTOR start_date >= now()
</otherwise>
</choose>
</select>where 適用于從多個(gè)條件中選擇所有滿足條件的來構(gòu)成condtions,
| 符號(hào) | 小于 | 小于等于 | 大于 | 大于等于 | 和 | 單引號(hào) | 雙引號(hào) |
| 原符號(hào) | < | <= | > | >= | & | ' | " |
| 替換符號(hào) | < | <= | > | >= | & | ' | " |
<select id="searchCourses" parameterType="hashmap" resultMap="CourseResult">
SELECT * FROM COURSES
<where>
<if test=" tutorId != null ">
TUTOR_ID= #{tutorId}
</if>
<if test="courseName != null">
AND name like #{courseName}
</if>
<if test="startDate != null">
AND start_date >= #{startDate} <!--大于等于 開始時(shí)間-->
</if>
<if test="endDate != null">
AND end_date <= #{endDate} <!--小于等于 結(jié)束時(shí)間-->
</if>
</where>
</select>foreach
<select id="selectUserByListId" parameterType="com.ys.vo.UserVo" resultType="com.ys.po.User">
select * from user
<where>
<!--
collection:指定輸入對(duì)象中的集合屬性.該屬性的值有三種:list,array,map,根據(jù)傳入的集合類型而設(shè)定該值。
item:每次遍歷生成的對(duì)象
index:當(dāng)前迭代的次數(shù)
open:開始遍歷時(shí)的拼接字符串
close:結(jié)束時(shí)拼接的字符串
separator:遍歷對(duì)象之間需要拼接的字符串
select * from user where 1=1 and id in (1,2,3)
-->
<foreach collection="list" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</where>
</select>select * from user where id=1 or id=2 or id=3
<select id="selectUserByListId" parameterType="com.ys.vo.UserVo" resultType="com.ys.po.User">
select * from user
<where>
<foreach collection="list" item="id" open="and (" close=")" separator="or">
id=#{id}
</foreach>
</where>
</select>< sql >標(biāo)簽
該標(biāo)簽主要定義復(fù)用的sql語(yǔ)句片段,在執(zhí)行的sql語(yǔ)句標(biāo)簽直接引用即可??梢蕴岣呔幋a效率、簡(jiǎn)化代碼和提高可讀性。
需要配置id熟悉,表示該sql片段的唯一標(biāo)識(shí)。
引用:通過<include refid=" " / >標(biāo)簽引用,refid的值就是< sql>的id屬性的值。
<sql id="Base_Column_List">
id, question, answer
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from java
where id = #{id,jdbcType=BIGINT}
</select><set> : 主要用來替換sql語(yǔ)句中的set字段,一般在update中使用。
<update>
update user
<set>
<if test="name != null and name.length()>0">name = #{name},</if>
<if test="age != null and age .length()>0">age = #{age },</if>
</set>
where id = #{id}
</update>批量更新,一次執(zhí)行多條SQL
<update>
<foreach collection="list" item="item" index="index" separator=";">
update user
<set>
<if test="name != null and name.length()>0">name = #{name},</if>
<if test="age != null and age .length()>0">age = #{age },</if>
</set>
<where>
<if test="id != null and id != ''">
AND id = #{id,jdbcType=VARCHAR}
</if>
<if test="userName != null and userName != ''">
<bind name="bindUserName" value="'%' + userName + '%'" />
AND UserName like #{bindUserName}
</if>
<if test="status != null">
AND Status = #{status,jdbcType=INTEGER}
</if>
</where>
</foreach>
</update><trim> : 是一個(gè)格式化的標(biāo)記,可以完成set或者是where標(biāo)記的功能。
select * from user
<trim prefix="WHERE" prefixoverride="AND |OR">
<if test="name != null and name.length()>0"> AND name=#{name}</if>
<if test="age != null and age.length()>0"> AND age=#{age}</if>
</trim>
假如說name和age的值都不為null的話打印的SQL為:select * from user where name = ‘xx' and age = ‘xx'
在where的后面是不存在第一個(gè)and的,上面兩個(gè)屬性的意思如下:
prefix:前綴
prefixoverride:去掉第一個(gè)and或者是orupdate user
<trim prefix="set" suffixoverride="," suffix=" where id = #{id} ">
<if test="name != null and name.length()>0"> name=#{name} , </if>
<if test="age!= null and age.length()>0"> age=#{age} , </if>
</trim>
假如說name和age的值都不為null的話打印的SQL為:update user set name=‘xx' , age=‘xx' where id=‘x'
在age='xx'的后面不存在逗號(hào),而且自動(dòng)加了一個(gè)set前綴和where后綴,上面三個(gè)屬性的意義如下,其中prefix意義如上:
suffixoverride:去掉最后一個(gè)逗號(hào)(也可以是其他的標(biāo)記,就像是上面前綴中的and一樣)
suffix:后綴choose & foreach list 有一個(gè)值,條件=,有多個(gè)值 in
<update id="updateStatus" parameterType="java.util.ArrayList" >
update User
set Status = 1,UpdateTime=(select GETDATE())
<choose>
<when test="idList != null and idList.size==1">
WHERE Id= #{id,jdbcType=VARCHAR}
</when>
<when test="idList != null and idList.size>1">
WHERE Id in
<foreach collection="idList" item="id" open="(" close=")" separator=",">
<if test="id != null and id != '' ">
#{id,jdbcType=VARCHAR}
</if>
</foreach>
</when>
<otherwise>
WHERE 1=0
</otherwise>
</choose>
</update>到此這篇關(guān)于MyBatis Mapper.XML 標(biāo)簽使用小結(jié)的文章就介紹到這了,更多相關(guān)MyBatis Mapper.XML 標(biāo)簽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot項(xiàng)目配置數(shù)據(jù)庫(kù)密碼加密相關(guān)代碼
這篇文章主要介紹了SpringBoot項(xiàng)目配置數(shù)據(jù)庫(kù)密碼加密的相關(guān)資料,本文介紹了在Springboot項(xiàng)目中配置數(shù)據(jù)庫(kù)連接時(shí)存在的安全問題,即用戶名和密碼以明文形式存儲(chǔ),容易泄露,提出了一種簡(jiǎn)單的加密方案,需要的朋友可以參考下2024-11-11
spring+mybatis 通過@ResponseBody返回結(jié)果中文亂碼的解決方法
下面小編就為大家分享一篇spring+mybatis 通過@ResponseBody返回結(jié)果中文亂碼的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12
MybatisPlus如何調(diào)用count函數(shù)
這篇文章主要介紹了MybatisPlus如何調(diào)用count函數(shù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
JMeter自定義日志與日志分析的實(shí)現(xiàn)
JMeter與Java程序一樣,會(huì)記錄事件日志,本文就介紹一下JMeter自定義日志與日志分析的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
java Socket無(wú)法完全接收返回內(nèi)容的解決方案
這篇文章主要介紹了java Socket無(wú)法完全接收返回內(nèi)容的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10

