<!--检查目录是否存在--> <selectid="isDirectoryExisting"parameterType="string"resultType="boolean"> SELECT EXISTS(SELECT 1 FROM directory WHERE directory_id=#{directoryId}) </select>
If the designated column has a datatype of CHAR or VARCHAR and contains a “0” or has a datatype of BIT, TINYINT, SMALLINT, INTEGER or BIGINT and contains a 0, a value of false is returned. If the designated column has a datatype of CHAR or VARCHAR and contains a “1” or has a datatype of BIT, TINYINT, SMALLINT, INTEGER or BIGINT and contains a 1, a value of true is returned.
if
1 2 3 4 5
<!-- 查询学生 list,like 姓名 --> <selectid="getStudentListLikeName"parameterType="StudentEntity"resultMap="studentResultMap"> SELECT * from STUDENT_TBL ST WHERE ST.STUDENT_NAME LIKE CONCAT('%', #{studentName}, '%') </select>
<!-- 查询学生list,like姓名 --> <selectid=" getStudentListLikeName "parameterType="StudentEntity"resultMap="studentResultMap"> SELECT * FROM STUDENT_TBL ST <iftest="studentName!=null and studentName!='' "> WHERE ST.STUDENT_NAME LIKE CONCAT('%', #{studentName}, '%') </if> </select>
此时,当 studentName 的值为 null 或 ‘’ 的时候,我们并不进行 where 条件的判断,所以查询结果是全部。
where
由于参数是 Java 的实体类,所以我们可以把所有条件都附加上,使用时比较灵活, new 一个这样的实体类,我们需要限制哪个条件,只需要附上相应的值就会 WHERE 中使用这个条件,相反不去赋值就可以不在 WHERE 中判断。
<where> 标签会知道如果它包含的标签中有返回值的话,它就插入一个 WHERE。此外,如果标签返回的内容是以 AND 或 OR 开头的,则它会剔除掉。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<!-- 查询学生list,like姓名,=性别、=生日、=班级,使用where,参数entity类型 --> <selectid="getStudentListWhereEntity"parameterType="StudentEntity"resultMap="studentResultMap"> SELECT * FROM student <where> <iftest="studentName!=null and studentName!='' "> name LIKE CONCAT('%', #{studentName},'%') </if> <iftest="studentSex!= null and studentSex!= '' "> AND gender = #{gender} </if> <iftest="studentBirthday!=null"> AND birthday = #{birthday} </if> <iftest="classEntity!=null and classEntity.classID !=null and classEntity.classID!='' "> AND class_id = #{classEntity.classID} </if> </where> </select>
<!-- 查询学生list,like姓名、或=性别、或=生日、或=班级,使用choose --> <selectid="getStudentListChooseEntity"parameterType="StudentEntity"resultMap="studentResultMap"> SELECT * from STUDENT_TBL ST <where> <choose> <whentest="studentName!=null and studentName!='' "> ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%') </when> <whentest="studentSex!= null and studentSex!= '' "> AND ST.STUDENT_SEX = #{studentSex} </when> <whentest="studentBirthday!=null"> AND ST.STUDENT_BIRTHDAY = #{studentBirthday} </when> <whentest="classEntity!=null and classEntity.classID !=null and classEntity.classID!='' "> AND ST.CLASS_ID = #{classEntity.classID} </when> <otherwise>
</otherwise> </choose> </where> </select>
foreach
注意,<foreach> 是循环,用来读取传入的 list 参数。批量处理时 parameterType 的类型必须要注意。<foreach> 标签中的 collection 属性表示传入的是什么集合类型,item 表示的是集合中的一个项,类似于
1 2 3 4
List<String> list; for (String str : list) { …… }
item 就相当于 str 的作用,用来遍历 collection
index 就是集合的索引
open 表示标签以什么开始
close 表示标签以什么结束
seprator 表示元素之间的间隔
1 2 3 4 5 6 7
<selectid="getStudentListByClassIDs"resultMap="studentResultMap"> SELECT * FROM STUDENT_TBL ST WHERE ST.CLASS_ID IN <foreachcollection="list"item="classId"open="("close=")"separator=","> #{classId} </foreach> </select>
批量删除
1 2 3 4 5 6
<deleteid="deleteBatchByXXX"parameterType="list"> DELETE FROM 表名 WHERE groupon_id IN <foreachcollection="list"item="item"open="("close =")"separator=","> #{item} </foreach > </delete >
publicvoidbatchUpdateStudentWithMap(){ List<Integer> ls = new ArrayList<Integer>(); for(int i = 2;i < 8;i++){ ls.add(i); } Map<String,Object> map = new HashMap<String,Object>(); map.put("idList", ls); map.put("name", "mmao789"); SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession(); session.insert("mybatisdemo.domain.Student.batchUpdateStudentWithMap",map); session.commit(); session.close(); }
1 2 3 4 5 6
<updateid="batchUpdateStudentWithMap"parameterType="java.util.Map" > UPDATE STUDENT SET name = #{name} WHERE id IN <foreachcollection="idList"index="index"item="item"open="("separator=","close=")"> #{item} </foreach> </update>
更新单条记录
1
UPDATE course SET name ='course1'WHERE id ='id1'
更新多条记录的同一个字段为同一个值
1
UPDATE course SET name ='course1'WHERE id in ('id1', 'id2', 'id3)
UPDATE course SET name =CASE id WHEN1THEN'name1' WHEN2THEN'name2' WHEN3THEN'name3' END, title =CASE id WHEN1THEN'New Title 1' WHEN2THEN'New Title 2' WHEN3THEN'New Title 3' END WHERE id IN (1,2,3)
这条 SQL 的意思是,如果 id 为 1,则 name 的值为 name1,title 的值为 New Title1;依此类推。 在Mybatis中的写法则如下:
<!-- [[3]] 使用 resultMap 映射,属性是另一个类的对象: association --> <selectid="selectFullUserById"parameterType="int"resultMap="userResultMap" > SELECT user.id as id, user.age as age, user.name as name, ui.id as user_info_id, ui.user_id as user_info_user_id, ui.telephone as user_info_telephone, ui.address as user_info_address FROM user ... </select>
<selectid="findPapersBySubjectAndNameFilter"resultMap="paperResultMap"> SELECT p.paper_id as paper_id, p.name as name, p.uuid_name as uuid_ame, p.original_name as original_name, kp.name as kp_name, kp.knowledge_point_id as kp_knowledge_point_id FROM paper AS p ... </select>