溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

mybatis有哪些常用方法

發(fā)布時間:2021-07-10 16:15:42 來源:億速云 閱讀:291 作者:chen 欄目:編程語言

這篇文章主要講解了“mybatis有哪些常用方法”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“mybatis有哪些常用方法”吧!

mybatis常用方法整理

1. 批量插入

java 接口:

Integer insertList(List<Student> list);

xml:

<insert id="insertList" parameterType="List">
    insert into student (
    ID, NAME, BIRTH, SEX
    ) values
    <foreach collection="list" item="item" index="index" open="(" close=")" separator="),(">
        #{item.id}, #{item.name}, #{item.birth}, #{item.sex}
    </foreach>
</insert>

其中使用foreach對list這個列表進(jìn)行循環(huán),循環(huán)中每個對象表示名稱為item, 開始為(結(jié)束為),中間分割符為),(。

2. 查找條件為in的情況

where限制為in的均可適用。

1. 傳參為List對象

java:

List<Student> getStudentListByNames(@Param("names") List<String> names);

xml:

<select id="getStudentListByNames" resultType="com.demo.domain.Student">
    select * from student a
    <where>
        <if test="names != null and names > 0">
            a.NAME in
            <foreach collection="names" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if> 
    </where>
</select>

我們先用test判斷names是否為空并且要有數(shù)量,然后進(jìn)行遍歷,其實本質(zhì)就是進(jìn)行腳本拼接,和我們平時寫法很相似。

2. 傳參對象為字符串,多個查詢參數(shù)以逗號拼接

比如:

"張三,李四,王五"

java:

List<Student> getStudentsByNames(@Param("names") String names);

xml:

<select id="getStudentsByNames" resultType="com.demo.domain.Student">
    select * from student a
    <where>
        <if test="names != null and names != ''">
            a.NAME in
            <foreach item="item" index="index" collection="names.split(',')" open="("
                         separator="," close=")">
                    #{item}
                </foreach>
        </if> 
    </where>
</select>

和之前的例子差不多,但是判斷條件改了,因為不是列表了,還有就是collection中使用split進(jìn)行分割,其它基本相同。

3. sql語句復(fù)用

1. 復(fù)用一段sql語句,避免處處編寫

如需要重復(fù)使用的sql:

<sql id="Base_Column_List">
    ID, NAME, BIRTH, SEX
</sql>

復(fù)用:

select 
<include refid="Base_Column_List"/>
from student where id=#{id}

include中的refid填充值就是上面sqlid的屬性值。

2. 復(fù)用sql動態(tài)填充屬性值

如需要別名的sql:

<sql id="Alias_Column_List">
    ${alias}.ID, ${alias}.NAME, ${alias}.BIRTH, ${alias}.SEX
</sql>

使用t1別名:

select 
<include refid="Alias_Column_List">
    <property name="alias" value="t1"></property>
</include>
from student t1 where t1.id=#{id}

4. like的使用

如查找學(xué)生名字中帶有某某字樣的學(xué)生:

select 
<include refid="Base_Column_List"/>
from student where name like "%"#{name}"%"

5. 復(fù)雜返回對象resultMap的使用

如學(xué)生student有屬性:id, name, class_id . 班級class有屬性:id, class_name, class_teacher_id . 老師teacher(這里我們假設(shè)只有班主任,一個班級只有一個班主任)有屬性: id, teacher_name .

查詢班級以及和班級關(guān)聯(lián)的學(xué)生和班主任。

<select id="selectClassDescInfos" resultMap="classDescInfos">

select t1.id as class_id, t1.class_name, t2.id as teacher_id, t2.teacher_name, t3.id as student_id, t3.name as student_name 
from class t1 
left join teacher t2 on t1.class_teacher_id = t2.id
left join student t3 on t1.id = t3.class_id
where class_id = #{classId}

</select>
<resultMap id="classDescInfos"
               type="com.demo.vo.ClassDescInfos">
       <id column="class_id" property="classId"></id>
            <result column="class_name" property="className"/>    
        <association property="teacher" javaType="com.demo.vo.Teacher">
            <id column="teacher_id" property="teacherId"></id>
            <result column="teacher_name" property="teacherName"/>    
        </association>
        <collection property="students" javaType="ArrayList"
                    ofType="com.demo.vo.Student">
            <id column="student_id" property="studentId"></id>
            <result column="student_name" property="studentName"/>    
        </collection>

其中com.demo.vo.ClassDescInfos的java類結(jié)構(gòu)類似如下:

private String classId;
private String className;
private Teacher teacher;
private List<Student> students;

這樣上述查詢就可以返回一個這樣的對象,都幫我們處理好了。

具體可以參考這篇文章:

https://www.cnblogs.com/rollenholt/p/3365866.html

感謝各位的閱讀,以上就是“mybatis有哪些常用方法”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對mybatis有哪些常用方法這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI