mybatis常用方法整理

語言: CN / TW / HK

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這個列表進行迴圈,迴圈中每個物件表示名稱為item, 開始為(結束為),中間分割符為),(

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是否為空並且要有數量,然後進行遍歷,其實本質就是進行指令碼拼接,和我們平時寫法很相似。

2. 傳參物件為字串,多個查詢引數以逗號拼接

比如:

"張三,李四,王五"

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進行分割,其它基本相同。

3. sql語句複用

1. 複用一段sql語句,避免處處編寫

如需要重複使用的sql:

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

複用:

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

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

2. 複用sql動態填充屬性值

如需要別名的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的使用

如查詢學生名字中帶有某某字樣的學生:


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

5. 複雜返回物件resultMap的使用

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

查詢班級以及和班級關聯的學生和班主任。


<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類結構類似如下:


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

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

具體可以參考這篇文章:

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

分享到: